博主信息
博文 462
粉丝 0
评论 0
访问量 96661
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
AppCrawler 自动遍历测试实践(二):定制化配置
霍格沃兹测开学社
原创
336人浏览过

本文为霍格沃兹测试学院学院学员课程 AppCrawler 学习笔记。

定制化配置

自动遍历测试技术以及工具该如何选择和快速入门?经过对比和需求,最终选择测试架构师思寒大佬的 AppCrawler作为自动遍历测试的工具。以下就分享 AppCrawler自动遍历测试的一些最佳实践经验。

模板文件生成

运行命令java -jar appcrawler-2.4.0-jar-with-dependencies.jar --demo, 会在当前目录下生成一个 demo.yml文件,这个文件就是我们进行定制化的配置文件模板:

  1. #执行命令生成demo.yaml模板配置文件
  2. $ java -jar appcrawler-2.4.0-jar-with-dependencies.jar --demo
  3. 2019-12-01 21:33:35 INFO [AppCrawler$.86.main]
  4. ----------------
  5. AppCrawler 2.4.0 [霍格沃兹测试开发学社特别纪念版]
  6. Appium 1.8.1 Java8 tested
  7. app爬虫, 用于自动遍历测试. 支持AndroidiOS, 支持真机和模拟器
  8. 项目地址: https://github.com/seveniruby/AppCrawler
  9. --------------------------------
  10. 2019-12-01 21:33:35 INFO [AppCrawler$.223.parseParams] use default appium address 4723
  11. 2019-12-01 21:33:35 INFO [AppCrawler$.230.parseParams] appium address = Some(http://127.0.0.1:4723/wd/hub)
  12. 2019-12-01 21:33:35 INFO [AppCrawler$.242.parseParams] result directory = 20191201213335
  13. 2019-12-01 21:33:36 INFO [AppCrawler$.286.parseParams] you can read /Users/qinzhen/Documents/TestDev/AppCrawler/demo.yml for demo
  14. #查看配置文件已生成
  15. $ ls
  16. appcrawler-2.4.0-jar-with-dependencies.jar
  17. demo.yml
  • 打开配置文件demo.yaml如下:
  1. ---
  2. pluginList: []
  3. saveScreen: true
  4. reportTitle: ""
  5. resultDir: "20191201213335"
  6. waitLoading: 500
  7. waitLaunch: 6000
  8. showCancel: true
  9. maxTime: 10800
  10. maxDepth: 10
  11. capability:
  12. noReset: "true"
  13. fullReset: "false"
  14. appium: "http://127.0.0.1:4723/wd/hub"
  15. testcase:
  16. name: "Ceshiren AppCrawler"
  17. steps:
  18. - given: []
  19. when: null
  20. then: []
  21. xpath: "/*"
  22. action: "Thread.sleep(5000)"
  23. actions: []
  24. times: 0
  25. selectedList:
  26. - given: []
  27. when: null
  28. then: []
  29. xpath: "//*[contains(name(), 'Button')]"
  30. action: null
  31. actions: []
  32. times: 0
  33. - given: []
  34. when: null
  35. then: []
  36. xpath: "//*[contains(name(), 'Text') and @clickable='true' and string-length(@text)<10]"
  37. action: null
  38. actions: []
  39. times: 0
  40. - given: []
  41. when: null
  42. then: []
  43. xpath: "//*[@clickable='true']/*[contains(name(), 'Text') and string-length(@text)<10]"
  44. action: null
  45. actions: []
  46. times: 0
  47. - given: []
  48. when: null
  49. then: []
  50. xpath: "//*[contains(name(), 'Image') and @clickable='true']"
  51. action: null
  52. actions: []
  53. times: 0
  54. - given: []
  55. when: null
  56. then: []
  57. xpath: "//*[@clickable='true']/*[contains(name(), 'Image')]"
  58. action: null
  59. actions: []
  60. times: 0
  61. - given: []
  62. when: null
  63. then: []
  64. xpath: "//*[contains(name(), 'Image') and @name!='']"
  65. action: null
  66. actions: []
  67. times: 0
  68. - given: []
  69. when: null
  70. then: []
  71. xpath: "//*[contains(name(), 'Text') and @name!='' and string-length(@label)<10]"
  72. action: null
  73. actions: []
  74. times: 0
  75. firstList: []
  76. lastList:
  77. - given: []
  78. when: null
  79. then: []
  80. xpath: "//*[@selected='true']/..//*"
  81. action: null
  82. actions: []
  83. times: 0
  84. - given: []
  85. when: null
  86. then: []
  87. xpath: "//*[@selected='true']/../..//*"
  88. action: null
  89. actions: []
  90. times: 0
  91. backButton:
  92. - given: []
  93. when: null
  94. then: []
  95. xpath: "Navigate up"
  96. action: null
  97. actions: []
  98. times: 0
  99. triggerActions:
  100. - given: []
  101. when: null
  102. then: []
  103. xpath: "share_comment_guide_btn"
  104. action: null
  105. actions: []
  106. times: 0
  107. xpathAttributes:
  108. - "name"
  109. - "label"
  110. - "value"
  111. - "resource-id"
  112. - "content-desc"
  113. - "instance"
  114. - "text"
  115. sortByAttribute:
  116. - "depth"
  117. - "list"
  118. - "selected"
  119. findBy: "default"
  120. defineUrl: []
  121. baseUrl: []
  122. appWhiteList: []
  123. urlBlackList: []
  124. urlWhiteList: []
  125. blackList:
  126. - given: []
  127. when: null
  128. then: []
  129. xpath: ".*[0-9]{2}.*"
  130. action: null
  131. actions: []
  132. times: 0
  133. beforeRestart: []
  134. beforeElement:
  135. - given: []
  136. when: null
  137. then: []
  138. xpath: "/*"
  139. action: "Thread.sleep(500)"
  140. actions: []
  141. times: 0
  142. afterElement: []
  143. afterPage: []
  144. afterPageMax: 2
  145. tagLimitMax: 2
  146. tagLimit:
  147. - given: []
  148. when: null
  149. then: []
  150. xpath: "确定"
  151. action: null
  152. actions: []
  153. times: 1000
  154. - given: []
  155. when: null
  156. then: []
  157. xpath: "取消"
  158. action: null
  159. actions: []
  160. times: 1000
  161. - given: []
  162. when: null
  163. then: []
  164. xpath: "share_comment_guide_btn_name"
  165. action: null
  166. actions: []
  167. times: 1000
  168. assertGlobal: []

执行参数

同样,还是以雪球 App 为例,以实际操作运行来解释配置文件中各个参数的含义和用法

  • capability 设置: 与 appium 完全一致,但要注意这里默认有 noReset: “true” 和 appium: “http://127.0.0.1:4723/wd/hub“ 属性,配置完成如下
  1. capability:
  2. noReset: "false"
  3. fullReset: "false"
  4. appium: "http://127.0.0.1:4723/wd/hub"
  5. appPackage: com.xueqiu.androi
  6. appActivity: .view.WelcomeActivityAlias
  7. automationName: uiautomator2
  8. autoGrantPermissions: true

这里再介绍两个很有趣也很有用的参数:

dontStopAppOnReset: true ;这个参数允许我们在某个页面继续执行遍历,比如我们希望App先进入到某个页面后再进行遍历,或者当一个session结束后继续下一个session的时候我们希望不要杀死App重新执行,而是继续上一次结束的页面开始执行

ignoreUnimportantViews: 这个参数设置为true的时候可以忽略不重要的view,加速pageSource的加载,加快测试速度

  • testcase: 用于启动APP后的基础测试用例

执行参数

完整形态

允许我们以 givenwhenthen的形式指定操作,如果学习过 Java 的接口自动化框架 rest-assured话可以很容易理解这三个参数表达的含义:

  • given : 所有的先决条件,给定一个条件,只有条件成立的时候才完成后面的操作(实际用的较少)
  • when : 先决条件成立后的行为,对什么事件做什么事情
  • then : 断言集合,事件结束后对结果断言

具体写法如下:

  1. testcase:
  2. name: "Ceshiren AppCrawler"
  3. steps:
  4. - when:
  5. xpath: //*
  6. action: driver.swipe(0.5, 0.8, 0.5, 0.2)
  7. - when:
  8. xpath: //*
  9. action: driver.swipe(0.5, 0.2, 0.5, 0.8)
  10. then:
  11. - //*[contains(@text, '美股')]

执行参数

简写形态

另外实际使用中我们会经常使用简写形态

  • 直接使用 xpath对应 when里面的 xpath
  • 直接使用 action对应 when里面的 action 具体写法如下:
  1. - xpath: 自选
  2. action: click
  3. then:
  4. - //*[contains(@text, '美股')]

注:定位模式除了可以使用 xpath之外还可以使用正则和包含关系,只不过经常使用的是 xpath,也更为严谨;

  • 正则:使用^开头的就认定为正则,^确定$^.*输入密码

  • 包含:可以使用元素其中包含的内容进行定位; 密码输入

这里以雪球首页搜索框输入 alibaba 的简单场景举例,在搜索前还需要处理掉升级弹框,修改完成如下:

  1. testcase:
  2. name: "XueQiuTestDemo AppCrawler"
  3. steps:
  4. - { xpath: "//*[contains(@resource-id,'image_cancel')]", action: click }
  5. - xpath: home_search
  6. action: click
  7. - xpath: search_input_text
  8. action: alibaba

如上的 testcase 写法还要多说几句:

  • 也可以使用 {} 将需要执行的事件包裹起来,元素定位符和操作 action 时间用逗号隔开
  • {} 内若使用 xpath 表达式的话需要加双引号
  • xpath 中直接写 id 或 text 文本信息,就会默认使用包含去查找
  • 需要点击的事件要明确指明 click,某则会报错;虽然思寒说过默认的 action 就是 click ,但是经过实测发现在 2.4.0 版本上必须指明 action:click ; 很可能是思寒本地使用的内部版本经过了优化更新还未来得及开源到 GitHub上。

运行效果:

selectedList: 遍历范围设定
这里如果想设置让其点击所有可点击的TextViewImageView控件,修改完成如下:

  1. selectedList:
  2. - xpath: //android.widget.ImageView[@clickable='true']
  3. - xpath: //*[@clickable='true' and contains(@class,"Text")]
  • firstList: 优先被点击
  • lastList:最后执行 设置其最后才执行”确定”按钮,修改完成如下:
  1. lastList:
  2. - { xpath: text_yes, action: click }
  • backButton: 当所有元素都被点击后默认后退控件定位
  • blackList:黑名单
  • triggerAction: 特定条件出发执行动作的设置;设置遇到重置密码输入框时输入123456abc两次,修改完成如下:
  1. triggerActions:
  2. - { xpath: android.widget.EditText, action: 123456abc, times: 2 }
  • tagLimitMax: 全局设置,同类型的最多点击的次数;这里设置为2次
  1. tagLimitMax: 2
  • tagLimit: 自定义控件类型的点击次数,这里设置对于ListView类型的只点击一次; 修改完成如下 :
  1. - { xpath: "//*[contains(@class, 'List')]//*", times: 1 }
  • maxDepth: 遍历的最大深度
  • assertGlobal:设置一个全局断言,例如可设置断言在当前App,如果包名不符合,就可能发生了崩溃,便会报错
    注:执行参数比配置文件优先级高

到这里,appcrawler的基本语法和参数含义大致介绍了一遍,后面就是如何结合实际进行使用了。下一篇将进行详细的介绍。

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学