在OC项目下实现SwiftMonkey

这周一直在研究iOS上的monkey测试,然后把SwiftMonkey跑了起来,虽然网上已经好多关于这个的资料,但是对自己来说,也是一个学习过程,于是决定把整个实现流程记录下来,重新梳理总结一下,也相当于重新学习一遍,加深一下印象吧。

一、SwiftMonkey简介

swiftmonkey是国外友人基于 XCUITesting 框架开发的 monkey 工具,是用swift语言写的,SwiftMonkey 把 XCTesting 的私有 API 拿出来用了,直接通过 XCEventGenerator 来模拟事件。

源码的GitHub的地址:https://github.com/zalando/SwiftMonkey

二、在项目中集成SwiftMonkey

1、下载SwiftMonkey源码到本地

2、使用finder打开需要集成的项目目录,然后把 SwiftMonkey(和SwiftMonkeyPaws,SwiftMonkeyPaws就是为了给各种操作增加UI效果而已,可以不添加)目录粘贴到你的项目目录下去。

3、使用Xcode打开需要集成的项目,然后把SwiftMonkey 和 SwiftMonkeyPaws的 xcodeproj 拖到xcode界面显示项目中去,如图1:



图1

4、点击Xcode的file->New->Target新建一个target,按默认勾选的iOS UI Testing Bundle,然后直接next就好了,如图2:



图2

5、下一步之后,因为基于 XCUITesting 框架的 monkey 工具是用swift编写的,所以在language选项选择swift,其他选项按默认的就好,如图3:



图3

6、然后在新建的target上,将 SwiftMonkey.framework 添加为target 的依赖,即在building phases下的target dependencies下,点击“+”添加即可。然后 build phase 那里添加 Copy Files,见图4和图5



图4

图5

7、把build settings里的build options的“always embed swift standard libraries”设置为yes。

8、打开新建target下的UITest.swift文件,替换如下代码;

import XCTest

import SwiftMonkey

class SwiftMonkeyExampleUITests: 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()

}

}

完成以上步骤之后,monkey的配置也就基本完成了,点击xcode的produck->test开始测试,然后再点击如图6的播放按钮开始运行monkey。



图6

三、总结

在开始搞这个的时候,由于之前完全没用过mac和xcode,之间的各种不知如何操作就不说了,然后因为我们的项目调了很多第三方库,我把项目代码拖回来之后,又要安装cocoapod,在公司的龟速网络下下了一个下午才搞定,然后又安装pod,唉...

然后一开始还是按照别人swift工程的导入情况去实现的,然后把SwiftMonkeyPaws也加进去了,但是SwiftMonkeyPaws有一段初始化代码需要添加,然后添加上去报错了才知道,在oc上引用swift 的代码,还需要搞桥接,鉴于各种蛋疼,于是就不要了。

所以,看别人的教程方法啊可能很简单,操作一步一步的,但是只有在实际操作过了,你才真正地能学到东西。



作者:特务兔爱吃ong菜
链接:http://www.jianshu.com/p/cacef26ec5ce
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值