Swift Monkey整理

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,196评论 4 61
  • 嗯哼嗯哼蹦擦擦~~~ 转载自:https://github.com/Tim9Liu9/TimLiu-iOS 目录 ...
    philiha阅读 10,434评论 0 6
  • 前天是3.8女神节,人人都喊着“女神节快乐”。“嗨,女神~”,当别人这样叫你时,别当真,因为这往往是他人一句恭维和...
    宋颂grace阅读 4,168评论 0 5
  • 人生于世,不是生离,即是死别,或与自己,或同他人。 既然离别无法避免,不如干脆痛快些,告别的时候,无需像韩寒所...
    Manba小生阅读 3,496评论 0 1
  • 上一篇:【内容营销的作用以及注意事项】下一篇:【捆绑下载】 搜索引擎的优化   有了优质的内容,接下来要做的就是让...
    Null_Point阅读 4,816评论 0 1