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...
    肇東周閱讀 12,241評論 4 61
  • 嗯哼嗯哼蹦擦擦~~~ 轉載自:https://github.com/Tim9Liu9/TimLiu-iOS 目錄 ...
    philiha閱讀 5,018評論 0 6
  • 前天是3.8女神節,人人都喊著“女神節快樂”。“嗨,女神~”,當別人這樣叫你時,別當真,因為這往往是他人一句恭維和...
    宋頌grace閱讀 796評論 0 5
  • 人生于世,不是生離,即是死別,或與自己,或同他人。 既然離別無法避免,不如干脆痛快些,告別的時候,無需像韓寒所...
    Manba小生閱讀 544評論 0 1
  • 上一篇:【內容營銷的作用以及注意事項】下一篇:【捆綁下載】 搜索引擎的優化 ??有了優質的內容,接下來要做的就是讓...
    Null_Point閱讀 1,123評論 0 1