当前位置:   article > 正文

iOS 测试 SwiftMonkey :iOS 上的 monkey

swiftmonkey

从前有种测试叫瞎点测试,哦不,随机测试。Android 上有 Monkey。我们以前用 Monkey 来跑 Android 机顶盒,跑出了一堆非常难解决的 kernel 问题,帮助还是很大的。市场上随便挑个 Android 应用,monkey 能跑个 30 分钟应该算是了不起了。苹果没有给 iOS 提供 Monkey。大概 iOS 应用质量比较好吧。。

好在很多无聊的人研究测试技术,于是就有了很多 iOS 上的 monkey 工具。比如基于 UIAutomation 的 monkey —— https://github.com/jonathanpenn/ui-auto-monkey。通过 copy from stackoverflow 和 copy from github 模式,我们也熟练掌握了 iOS 上的 monkey。不幸的是,iOS 和 Xcode 升级之后,UIAutomation 框架被砍掉了,于是很长时间 iOS 没有 monkey 的说法了。然后无聊的外国人又整了一个基于 XCUITesting 框架的 monkey 工具 —— https://github.com/zalando/SwiftMonkey。社区里早就有人用过了,但是都藏着掖着。那我是最近才知道,所以拿过来用用。

安装配置

现在每天都有人来问我 Appium 的问题,90%是安装配置问题,10%是不知所谓。所以想对所有创造自动化工具的人说,你解决了安装配置问题,你就已经成功了。

SwiftMonkey 的配置挺简单的。用 CocoaPods —— 这个我没试过:

  1. target "App" do
  2. pod "SwiftMonkeyPaws", "~> 1.0"
  3. end
  4. target "Tests" do
  5. pod "SwiftMonkey", "~> 1.0"
  6. end

手动安装

把 https://github.com/zalando/SwiftMonkey 下载下来。把 SwiftMonkey 和 SwiftMonkeyPaws 目录粘贴到你的项目目录下去。然后把他们两的 xcodeproj 拖到项目中去。

然后呢,把 SwiftMonkey.framework 添加为你的 test target 的依赖。在 test 的 build phase 那里添加 Copy Files。如图:

然后其实就可以用了。

对于 SwiftMonkeyPaws,这个玩意就是让你的事件会有一个熊掌的反馈,这个得放到 app 的 target 里去,因为是 app 使用的。放到 Embedded Binaries 即可,如图:

然后在你应用里任何地方导入 SwiftMonkeyPaws,初始化它就可以了。

  1. import SwiftMonkeyPaws
  2. var paws: MonkeyPaws?
  3. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  4. if CommandLine.arguments.contains("--MonkeyPaws") {
  5. paws = MonkeyPaws(view: window!)
  6. }
  7. return true
  8. }

SwiftMonkeyPaws的效果就是那些熊掌,如下图:

遇到的坑

  1. 原来是 objc 的代码能否使用? 

    1. 可以,只要新建一个 XCUITesting 的新项目即可。如图:

    然后在,在测试用例里加入代码:

    1. //
    2. // SwiftMonkeyExampleUITests.swift
    3. // SwiftMonkeyExampleUITests
    4. //
    5. // Created by Dag Agren on 07/11/2016.
    6. // Copyright © 2016 Zalando SE. All rights reserved.
    7. //
    8. import XCTest
    9. import SwiftMonkey
    10. class SwiftMonkeyExampleUITests: XCTestCase {
    11. override func setUp() {
    12. super.setUp()
    13. XCUIApplication().launch()
    14. }
    15. override func tearDown() {
    16. super.tearDown()
    17. }
    18. func testMonkey() {
    19. let application = XCUIApplication()
    20. // Workaround for bug in Xcode 7.3. Snapshots are not properly updated
    21. // when you initially call app.frame, resulting in a zero-sized rect.
    22. // Doing a random query seems to update everything properly.
    23. // TODO: Remove this when the Xcode bug is fixed!
    24. _ = application.descendants(matching: .any).element(boundBy: 0).frame
    25. // Initialise the monkey tester with the current device
    26. // frame. Giving an explicit seed will make it generate
    27. // the same sequence of events on each run, and leaving it
    28. // out will generate a new sequence on each run.
    29. let monkey = Monkey(frame: application.frame)
    30. //let monkey = Monkey(seed: 123, frame: application.frame)
    31. // Add actions for the monkey to perform. We just use a
    32. // default set of actions for this, which is usually enough.
    33. // Use either one of these but maybe not both.
    34. // XCTest private actions seem to work better at the moment.
    35. // UIAutomation actions seem to work only on the simulator.
    36. monkey.addDefaultXCTestPrivateActions()
    37. //monkey.addDefaultUIAutomationActions()
    38. // Occasionally, use the regular XCTest functionality
    39. // to check if an alert is shown, and click a random
    40. // button on it.
    41. monkey.addXCTestTapAlertAction(interval: 100, application: application)
    42. // Run the monkey test indefinitely.
    43. monkey.monkeyAround()
    44. }
    45. }

    同时,别忘记在 build setting 里勾选上 Swift 的标准库,如图:

    然后运行测试就可以了。

  2. 登录相关:
    这事情其实不难,因为 iOS 的这个 monkey 其实就是你自己写的测试用例嘛。在开始之间用 XCUITesting 搞定登陆的事情就可以了。申明,这个我没试过,我是理论家。

一句话原理

其实你用就可以了,知道原理干嘛呢?人糊涂一点过的好一点。SwiftMonkey 把 XCTesting 的私有 API 拿出来用了,直接通过 XCEventGenerator 来模拟事件。所以如果你的应用植入了 SwiftMonkey 千万不要拿去提交 AppStore。

总结

感谢老外,让我们 copy from stackoverflow 和 copy from github 走的更好。SwiftMonkey 真机和模拟器都可以使用,亲测。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/263791
推荐阅读
  

闽ICP备14008679号