(已开源) 基于 XCTestWD,swiftmonkey 二次开发,实现无需插桩的 iOS monkey 自动化工具 fastmonkey

打造一款快速高效且高度可复用的ios自动化测试工具 (qq群 :329915064)

优势

  1. 无需插桩!
  2. 高效率,每秒4-5个action!
  3. 轻量极简!

原理

先致敬两个开源工具;

  1. swiftmonkey
    https://github.com/zalando/SwiftMonkey
    因其直接使用了 苹果的私有api 如XCEventGenerator.tapAtTouchLocations 故执行速度极快。同时也用类似android monkey的事件生成机制,自身源源不断的产生如 tap,swipe,pinchIn等事件使其对被测app产生了极大的压力
    缺点:需要插桩

  2. XCTestWD
    https://github.com/macacajs/XCTestWD
    类似facebook wda的方式,在xcode中执行xcuitest时启动一个监听server,可通过外部发送命令来创建待测app对应session并启动app,同时也实现了dump tree 和依据xpath,id等来定位某个控件并操作这些控件

  3. 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()

update

#44楼
支持控件,点击序列,跳出监控
#172楼
总运行时间,总点击次数。action固定间隔,action随机间隔(暂时内编码,后续参数化)
#191楼
增加跳过server 跳过发请求机制,运行xctestwduitest 直接启动待测app直接跑monkey
#
支持xcode9, ios11
#445楼

关于登录输入用户密码等的业务操作

未来

  1. 实现基于控件的monkey
  2. crash收集上报
  3. 性能数据

社区兄弟的搭建扫盲贴
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