打造一款快速高效且高度可复用的 ios 自动化测试工具 (qq 群 :329915064)
先致敬两个开源工具;
swiftmonkey
https://github.com/zalando/SwiftMonkey
因其直接使用了 苹果的私有 api 如 XCEventGenerator.tapAtTouchLocations 故执行速度极快。同时也用类似 android monkey 的事件生成机制,自身源源不断的产生如 tap,swipe,pinchIn 等事件使其对被测 app 产生了极大的压力
缺点:需要插桩
XCTestWD
https://github.com/macacajs/XCTestWD
类似 facebook wda 的方式,在 xcode 中执行 xcuitest 时启动一个监听 server,可通过外部发送命令来创建待测 app 对应 session 并启动 app,同时也实现了 dump tree 和依据 xpath,id 等来定位某个控件并操作这些控件
Fastmonkey
Xcode9.0(https://github.com/zhangzhao4444/Fastmonkey)
Xcode8.3(https://github.com/zhangzhao4444/Fastmonkey/tree/xcode8.3)
结合两者优点并改造两者,在 XCTestWD 基础上实现一个 server 路由,外部命令时可引导启动 app 并执行 monkey
app 插桩增加小手的反馈,可以更直观的看到其点击效率! (可选,实际不插也可 monkey)
测试时会保存截图,及 appcrash log(此处植入广告!)
1.monkey 执行过程中跳出 app,如何跳回继续跑 monkey
增加一个定时执行的 action,检测当前 app,如果不是待测则重新 launcher(其时更好的应该时 reactive,但并没有找到类似 api)
public func addXCTestCheckCurrentApp(interval:Int, appname:String, application:XCUIApplication) {
addAction(interval:interval){ [weak self] in
let current = XCTestWDFindElementUtils.getAppName(underElement: root!)
let isRunning = application.running
if current == appname && isRunning {
for i in 0 ..< application.alerts.count {
let alert = application.alerts.element(boundBy: i)
...
}
}else{
application.launch()
self!.sleep(5)
2.登陆等业务流程如何解决
同样增加一个定时的 action,检测当某个关键点出现时,往事件队列中插入一个业务事件
public func addXCTestAppLogin(interval:Int, application:XCUIApplication) {
addAction(interval:interval){ [weak self] in
if root != nil{
let usage = "xpath"
let tag = "//XCUIElementTypeOther[@name='登录']/XCUIElementTypeTextField"
let element = try? XCTestWDFindElementUtils.filterElement(usingText: usage, withvalue: tag, underElement: root!)
...
if element != nil {
self?.addXCTestLoginAction(application: application)
}
public func addXCTestLoginAction(application:XCUIApplication) {
addAction(){ [weak self] in
if root == nil{
return
}
let usage = "xpath"
let username = "//XCUIElementTypeOther[@name='登录']/XCUIElementTypeTextField"
let passwd = "//XCUIElementTypeOther[@name='登录']/XCUIElementTypeSecureTextField"
let button = "//XCUIElementTypeOther[@name='登录']//XCUIElementTypeStaticText[@name='登录']"
var element = try? XCTestWDFindElementUtils.filterElement(usingText: usage, withvalue: username, underElement: root!)
if let element = element {
let value = "abc"
...
let semaphore = DispatchSemaphore(value: 0)
let numberOfTaps = 1
self!.sharedXCEventGenerator.tapAtTouchLocations(locations, numberOfTaps: UInt(numberOfTaps), orientation: orientationValue) {
semaphore.signal()
}
semaphore.wait()
#44 楼
支持控件,点击序列,跳出监控
#172 楼
总运行时间,总点击次数。action 固定间隔,action 随机间隔(暂时内编码,后续参数化)
#191 楼
增加跳过 server 跳过发请求机制,运行 xctestwduitest 直接启动待测 app 直接跑 monkey
#
支持 xcode9, ios11
#445 楼
社区兄弟的搭建扫盲贴
https://testerhome.com/topics/9810
另一扫盲贴
http://cdn2.jianshu.io/p/2cbdb50411ae
jenkins 部署案例
https://testerhome.com/topics/12672
* 基于 xcode9.2 部署
https://www.jianshu.com/p/373c14d014f2