Swift Monkey Test
基于框架:XCTest
代碼侵入:有
支持語(yǔ)言:OC,Swift
用例語(yǔ)言:Swift
官方鏈接:https://github.com/zalando/SwiftMonkey
接入方式:手動(dòng),pod(暫時(shí)無(wú)法成功既忆,稍后說(shuō)明)
優(yōu)點(diǎn):每秒action較多
缺點(diǎn):需要源碼驱负;由于事件完全隨機(jī),可能長(zhǎng)時(shí)間無(wú)法跳出某個(gè)頁(yè)面患雇,
安裝配置
手動(dòng)安裝
1.git上下載源碼https://github.com/zalando/SwiftMonkey 跃脊,把 SwiftMonkey 和 SwiftMonkeyPaws 目錄粘貼到你的項(xiàng)目目錄下去。然后把他們兩的 xcodeproj 拖到項(xiàng)目中去
2.Swift Monkey是基于XCTest,所以需要新建一個(gè)UI Test target且為其配置依賴苛吱。選擇Test target的Build Phases酪术,
a.Target Dependencies添加SwiftMonkey.framework;
b.Copy Files添加SwiftMonkey.framework,Destination選擇Frameworks
3.添加SwiftMonkeyPaws對(duì)原始app代碼有侵入性又谋,可以選擇性添加拼缝。添加后可以在界面上顯示點(diǎn)擊的位置。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
問(wèn)題:pod過(guò)程中彰亥,提示找不到SwiftMonkeyPaws,無(wú)法解決
添加UI Test Target
1.選擇對(duì)應(yīng)的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可控參數(shù)分析
操作行為的權(quán)重,通過(guò)不同行為的權(quán)重可以控制行為出現(xiàn)的概率
public func addDefaultXCTestPrivateActions() {
addXCTestTapAction(weight: 40) //點(diǎn)擊
addXCTestLongPressAction(weight: 1) //長(zhǎng)按
addXCTestDragAction(weight: 25) //拖動(dòng)
addXCTestPinchCloseAction(weight: 1) //捏(縮谐芴巍)
addXCTestPinchOpenAction(weight: 1) //捏(放大)
addXCTestRotateAction(weight: 1) //旋轉(zhuǎn)
//addXCTestOrientationAction(weight: 1) // TODO: Investigate why this does not work.
}
備注:
若app target為oc工程废酷,且需要調(diào)用SwiftMonkeyPaws,若出現(xiàn)如下錯(cuò)誤
dyld: Library not loaded: @rpath/libswiftCore.dylib
主要原因是oc工程無(wú)法直接動(dòng)態(tài)調(diào)用swift的庫(kù)抹缕,需要在app target進(jìn)行設(shè)置澈蟆,具體操作如下:
Build Settings -> Build Options -> "Embedded Content Contains Swift Code" 設(shè)為YES即可,默認(rèn)是NO