Swift Monkey整理

2017-09-11  本文已影响124人  龙猫六六

Swift Monkey Test

基于框架:XCTest
代码侵入:有
支持语言:OC,Swift
用例语言:Swift
官方链接:https://github.com/zalando/SwiftMonkey
接入方式:手动,pod(暂时无法成功,稍后说明)
优点:每秒action较多
缺点:需要源码;由于事件完全随机,可能长时间无法跳出某个页面,

安装配置

手动安装

1.git上下载源码https://github.com/zalando/SwiftMonkey ,把 SwiftMonkey 和 SwiftMonkeyPaws 目录粘贴到你的项目目录下去。然后把他们两的 xcodeproj 拖到项目中去

1.jpg

2.Swift Monkey是基于XCTest,所以需要新建一个UI Test target且为其配置依赖。选择Test target的Build Phases,
a.Target Dependencies添加SwiftMonkey.framework;
b.Copy Files添加SwiftMonkey.framework,Destination选择Frameworks

2.jpg

3.添加SwiftMonkeyPaws对原始app代码有侵入性,可以选择性添加。添加后可以在界面上显示点击的位置。app 的target的Enable Modules中添加SwiftMonkeyPaws的引用

3.jpg

SwiftMonkeyPaws添加到app代码中
swift工程添加

import SwiftMonkeyPaws

var paws: MonkeyPaws?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    if CommandLine.arguments.contains("--MonkeyPaws") {
        paws = MonkeyPaws(view: window!)
    }
    return true
}

oc工程添加(SwiftMonkeyPaws不在app工程中)

@import SwiftMonkeyPaws;
@interface AppDelegate ()
@property (nonatomic) MonkeyPaws *paws;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window.rootViewController = [[RecordVC alloc] init];
    _paws = [[MonkeyPaws alloc] initWithView:self.window tapUIApplication:YES];
    return YES;
}

oc工程添加(SwiftMonkeyPaws在app工程中)

#import "SwiftMonkeyPaws-Swift.h";
@interface AppDelegate ()
@property (nonatomic) MonkeyPaws *paws;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window.rootViewController = [[RecordVC alloc] init];
    _paws = [[MonkeyPaws alloc] initWithView:self.window tapUIApplication:YES];
    return YES;
}

pod安装

target "App" do
    pod "SwiftMonkeyPaws", "~> 1.0"
end

target "Tests" do
    pod "SwiftMonkey", "~> 1.0"
end

问题:pod过程中,提示找不到SwiftMonkeyPaws,无法解决

添加UI Test Target

1.选择对应的Target
2.新建UI Test Case Class,该基类为XCTestCase,Language为Swift
3.具体代码如下

//
//  Case.swift
//  BridgeMonkeyDemo
//
//  Created by jc on 2017/9/11.
//  Copyright © 2017年 jc. All rights reserved.
//

import XCTest
import SwiftMonkey

class Case: XCTestCase {
        
    override func setUp() {
        super.setUp()
        XCUIApplication().launch()
    }
    
    override func tearDown() {
        super.tearDown()
    }
    
    func testMonkey() {
        let application = XCUIApplication()
        
        // Workaround for bug in Xcode 7.3. Snapshots are not properly updated
        // when you initially call app.frame, resulting in a zero-sized rect.
        // Doing a random query seems to update everything properly.
        // TODO: Remove this when the Xcode bug is fixed!
        _ = application.descendants(matching: .any).element(boundBy: 0).frame
        
        // Initialise the monkey tester with the current device
        // frame. Giving an explicit seed will make it generate
        // the same sequence of events on each run, and leaving it
        // out will generate a new sequence on each run.
        let monkey = Monkey(frame: application.frame)
        //let monkey = Monkey(seed: 123, frame: application.frame)
        
        // Add actions for the monkey to perform. We just use a
        // default set of actions for this, which is usually enough.
        // Use either one of these but maybe not both.
        // XCTest private actions seem to work better at the moment.
        // UIAutomation actions seem to work only on the simulator.
        monkey.addDefaultXCTestPrivateActions()
        //monkey.addDefaultUIAutomationActions()
        
        // Occasionally, use the regular XCTest functionality
        // to check if an alert is shown, and click a random
        // button on it.
        monkey.addXCTestTapAlertAction(interval: 100, application: application)
        
        // Run the monkey test indefinitely.
        monkey.monkeyAround()
    }
}

Swift Monkey可控参数分析

操作行为的权重,通过不同行为的权重可以控制行为出现的概率

   public func addDefaultXCTestPrivateActions() {
        addXCTestTapAction(weight: 40)                        //点击
        addXCTestLongPressAction(weight: 1)               //长按
        addXCTestDragAction(weight: 25)                      //拖动
        addXCTestPinchCloseAction(weight: 1)              //捏(缩小)
        addXCTestPinchOpenAction(weight: 1)              //捏(放大)
        addXCTestRotateAction(weight: 1)                      //旋转
        //addXCTestOrientationAction(weight: 1) // TODO: Investigate why this does not work.
    }

备注:
若app target为oc工程,且需要调用SwiftMonkeyPaws,若出现如下错误
dyld: Library not loaded: @rpath/libswiftCore.dylib
主要原因是oc工程无法直接动态调用swift的库,需要在app target进行设置,具体操作如下:
Build Settings -> Build Options -> "Embedded Content Contains Swift Code" 设为YES即可,默认是NO

上一篇下一篇

猜你喜欢

热点阅读