iOS-SwiftMonkey测试

Android中的Monkey测试极大的帮助Android开发者保证了开发质量,iOS自身是没有Monkey测试的,最开始有基于UIAutomation 的 monkey 测试[https://github.com/jonathanpenn/ui-auto-monkey]. xCode7之后,UIAutomation被弃用,对应Monkey也随之淡化.当大家习惯Monkey测试,突然消失之后就会怀念,正所谓有需求就会有市场,老外用Swift基于XCUITesting框架开发新的Monkey工具SwiftMonkey.

Swift Monkey测试

SwiftMonkey基于Swift语言编写,按照SwiftMonkey提供的Demo,运行之后看到效果如下图所示:


SwiftMonkey.gif

SwiftMonkey实现测试需要SwiftMonkey和SwiftMonkeyPaws两个框架,如果我们的项目开始就是Swift语言编写的,那么使用起来就非常简单,主要过程如下:
①将Demo中SwiftMonkey和SwiftMonkeyPaws两个项目拖入自身的Swift项目中.

FlyElephant.png

②SwiftMonkey作为FrameWork加入UITest对应的Target文件中:


FlyElephant.png

③SwiftMonkeyPaws作为FrameWork加入App项目对应的Target中;


FlyElephant.png

④AppDelegate中加入Paws:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var paws: MonkeyPaws?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        paws = MonkeyPaws(view: window!)
        return true
    }

⑤UITest中加入Monkey测试代码:

override func setUp() {
        super.setUp()
        
        // Put setup code here. This method is called before the invocation of each test method in the class.
        
        // In UI tests it is usually best to stop immediately when a failure occurs.
        continueAfterFailure = false
        // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
        XCUIApplication().launch()

        // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        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()
    }

这段代码是SwiftMonkey的默认代码,addDefaultXCTestPrivateActions是调用Apple私有手势方法,addDefaultUIAutomationActions只在模拟器中有效,内部实现如下:

 public func addDefaultXCTestPrivateActions() {
        addXCTestTapAction(weight: 25)
        addXCTestLongPressAction(weight: 1)
        addXCTestDragAction(weight: 1)
        addXCTestPinchCloseAction(weight: 1)
        addXCTestPinchOpenAction(weight: 1)
        addXCTestRotateAction(weight: 1)
        //addXCTestOrientationAction(weight: 1) // TODO: Investigate why this does not work.
    }

默认执行点击,长按,拖拽,捏合,旋转,横竖屏切换操作,Weight代表的是时间间隔,如果觉得系统默认的操作过多,可以自行删减.

monkeyAround代表次数,默认如果不设置次数,会一直执行下去.

public func monkeyAround() {
        while true {
            actRandomly()
            actRegularly()
        }
    }
FlyElephant.gif

OC Monkey 测试

如果你的项目一开始就是Swift编写,或者已经全面迁移到Swift语言,恭喜你下面这段介绍可以忽略了.鉴于目前大部分App都是OC为主,如果直接按照Swift的套路去使用SwiftMonkey会遇到一些问题,有兴趣可以自行实践,以下是本人实战过程.

① 将Demo中SwiftMonkey和SwiftMonkeyPaws两个项目拖入自身的Swift项目中.

② 将Always Embed Swift Standard Libraries设置为YES.


Paste_Image.png

③ 正常的套路直接调用Moneky,MonkeyPaws类,但是事实上无法调用,折腾了很久,中间各种报错.

④ 简单粗暴的方式是将SwiftMonkey两个项目中的Swift拷贝到OC项目中:


FlyElephant.png

⑤ AppDelegate中代码实现:

@interface AppDelegate ()

@property (strong, nonatomic) MonkeyPaws *paws;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.paws = [[MonkeyPaws alloc] initWithView:self.window tapUIApplication:true];

    return YES;
}

⑤ UITest测试代码:

- (void)setUp {
    [super setUp];
    
    // Put setup code here. This method is called before the invocation of each test method in the class.
    
    // In UI tests it is usually best to stop immediately when a failure occurs.
    self.continueAfterFailure = NO;
    // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
    [[[XCUIApplication alloc] init] launch];
    
    // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

- (void)testExample {
    // Use recording to get started writing UI tests.
    [XCUIDevice sharedDevice].orientation = UIDeviceOrientationPortrait;
    // Use XCTAssert and related functions to verify your tests produce the correct results.
}

- (void)testMonkey {
    
    XCUIApplication *application = [XCUIApplication new];
    CGRect frame = [[application descendantsMatchingType:XCUIElementTypeAny] elementBoundByIndex:0].frame;
    
    Monkey *monkey = [[Monkey alloc] initWithSeed:123 frame:application.frame];
    
    [monkey addDefaultXCTestPrivateActions];
    [monkey addXCTestTapAlertActionWithInterval:100 application:application];
    [monkey monkeyAround];
}
FlyElephant.gif

如果项目中之前没有进行过Monkey测试,SwiftMonkey会帮我们测出一些以前开发中没有注意的问题.SwiftMonkey涉及调用XCTesting的私有API,不建议包含SwiftMonkey直接上传到AppStore.

参考资料
SwiftMonkey
SwiftMonkey :iOS 上的 monkey

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,937评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,503评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,712评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,668评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,677评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,601评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,975评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,637评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,881评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,621评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,710评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,387评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,971评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,947评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,189评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,805评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,449评论 2 342

推荐阅读更多精彩内容