Swift Monkey Test
基于框架:XCTest
代碼侵入:有
支持語言:OC,Swift
用例語言:Swift
官方鏈接:https://github.com/zalando/SwiftMonkey
接入方式:手動,pod(暫時無法成功,稍后說明)
優點:每秒action較多
缺點:需要源碼;由于事件完全隨機,可能長時間無法跳出某個頁面,
安裝配置
手動安裝
1.git上下載源碼https://github.com/zalando/SwiftMonkey ,把 SwiftMonkey 和 SwiftMonkeyPaws 目錄粘貼到你的項目目錄下去。然后把他們兩的 xcodeproj 拖到項目中去
2.Swift Monkey是基于XCTest,所以需要新建一個UI Test target且為其配置依賴。選擇Test target的Build Phases,
a.Target Dependencies添加SwiftMonkey.framework;
b.Copy Files添加SwiftMonkey.framework,Destination選擇Frameworks
3.添加SwiftMonkeyPaws對原始app代碼有侵入性,可以選擇性添加。添加后可以在界面上顯示點擊的位置。app 的target的Enable Modules中添加SwiftMonkeyPaws的引用
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