本文为霍格沃兹测试学院学院学员课程 AppCrawler 学习笔记。
自动遍历测试技术以及工具该如何选择和快速入门?经过对比和需求,最终选择测试架构师思寒大佬的 AppCrawler作为自动遍历测试的工具。以下就分享 AppCrawler自动遍历测试的一些最佳实践经验。
模板文件生成
运行命令java -jar appcrawler-2.4.0-jar-with-dependencies.jar --demo, 会在当前目录下生成一个 demo.yml文件,这个文件就是我们进行定制化的配置文件模板:
#执行命令生成demo.yaml模板配置文件$ java -jar appcrawler-2.4.0-jar-with-dependencies.jar --demo2019-12-01 21:33:35 INFO [AppCrawler$.86.main]----------------AppCrawler 2.4.0 [霍格沃兹测试开发学社特别纪念版]Appium 1.8.1 Java8 testedapp爬虫, 用于自动遍历测试. 支持Android和iOS, 支持真机和模拟器项目地址: https://github.com/seveniruby/AppCrawler--------------------------------2019-12-01 21:33:35 INFO [AppCrawler$.223.parseParams] use default appium address 47232019-12-01 21:33:35 INFO [AppCrawler$.230.parseParams] appium address = Some(http://127.0.0.1:4723/wd/hub)2019-12-01 21:33:35 INFO [AppCrawler$.242.parseParams] result directory = 201912012133352019-12-01 21:33:36 INFO [AppCrawler$.286.parseParams] you can read /Users/qinzhen/Documents/TestDev/AppCrawler/demo.yml for demo#查看配置文件已生成$ lsappcrawler-2.4.0-jar-with-dependencies.jardemo.yml
demo.yaml如下:
---pluginList: []saveScreen: truereportTitle: ""resultDir: "20191201213335"waitLoading: 500waitLaunch: 6000showCancel: truemaxTime: 10800maxDepth: 10capability:noReset: "true"fullReset: "false"appium: "http://127.0.0.1:4723/wd/hub"testcase:name: "Ceshiren AppCrawler"steps:- given: []when: nullthen: []xpath: "/*"action: "Thread.sleep(5000)"actions: []times: 0selectedList:- given: []when: nullthen: []xpath: "//*[contains(name(), 'Button')]"action: nullactions: []times: 0- given: []when: nullthen: []xpath: "//*[contains(name(), 'Text') and @clickable='true' and string-length(@text)<10]"action: nullactions: []times: 0- given: []when: nullthen: []xpath: "//*[@clickable='true']/*[contains(name(), 'Text') and string-length(@text)<10]"action: nullactions: []times: 0- given: []when: nullthen: []xpath: "//*[contains(name(), 'Image') and @clickable='true']"action: nullactions: []times: 0- given: []when: nullthen: []xpath: "//*[@clickable='true']/*[contains(name(), 'Image')]"action: nullactions: []times: 0- given: []when: nullthen: []xpath: "//*[contains(name(), 'Image') and @name!='']"action: nullactions: []times: 0- given: []when: nullthen: []xpath: "//*[contains(name(), 'Text') and @name!='' and string-length(@label)<10]"action: nullactions: []times: 0firstList: []lastList:- given: []when: nullthen: []xpath: "//*[@selected='true']/..//*"action: nullactions: []times: 0- given: []when: nullthen: []xpath: "//*[@selected='true']/../..//*"action: nullactions: []times: 0backButton:- given: []when: nullthen: []xpath: "Navigate up"action: nullactions: []times: 0triggerActions:- given: []when: nullthen: []xpath: "share_comment_guide_btn"action: nullactions: []times: 0xpathAttributes:- "name"- "label"- "value"- "resource-id"- "content-desc"- "instance"- "text"sortByAttribute:- "depth"- "list"- "selected"findBy: "default"defineUrl: []baseUrl: []appWhiteList: []urlBlackList: []urlWhiteList: []blackList:- given: []when: nullthen: []xpath: ".*[0-9]{2}.*"action: nullactions: []times: 0beforeRestart: []beforeElement:- given: []when: nullthen: []xpath: "/*"action: "Thread.sleep(500)"actions: []times: 0afterElement: []afterPage: []afterPageMax: 2tagLimitMax: 2tagLimit:- given: []when: nullthen: []xpath: "确定"action: nullactions: []times: 1000- given: []when: nullthen: []xpath: "取消"action: nullactions: []times: 1000- given: []when: nullthen: []xpath: "share_comment_guide_btn_name"action: nullactions: []times: 1000assertGlobal: []
执行参数
同样,还是以雪球 App 为例,以实际操作运行来解释配置文件中各个参数的含义和用法
capability:noReset: "false"fullReset: "false"appium: "http://127.0.0.1:4723/wd/hub"appPackage: com.xueqiu.androiappActivity: .view.WelcomeActivityAliasautomationName: uiautomator2autoGrantPermissions: true
这里再介绍两个很有趣也很有用的参数:
dontStopAppOnReset:true;这个参数允许我们在某个页面继续执行遍历,比如我们希望App先进入到某个页面后再进行遍历,或者当一个session结束后继续下一个session的时候我们希望不要杀死App重新执行,而是继续上一次结束的页面开始执行
ignoreUnimportantViews: 这个参数设置为true的时候可以忽略不重要的view,加速pageSource的加载,加快测试速度
执行参数
完整形态
允许我们以 given、when、then的形式指定操作,如果学习过 Java 的接口自动化框架 rest-assured话可以很容易理解这三个参数表达的含义:
具体写法如下:
testcase:name: "Ceshiren AppCrawler"steps:- when:xpath: //*action: driver.swipe(0.5, 0.8, 0.5, 0.2)- when:xpath: //*action: driver.swipe(0.5, 0.2, 0.5, 0.8)then:- //*[contains(@text, '美股')]
执行参数
简写形态
另外实际使用中我们会经常使用简写形态
xpath对应 when里面的 xpathaction对应 when里面的 action 具体写法如下:
- xpath: 自选action: clickthen:- //*[contains(@text, '美股')]

注:定位模式除了可以使用
xpath之外还可以使用正则和包含关系,只不过经常使用的是xpath,也更为严谨;
正则:使用^开头的就认定为正则,^确定$,^.*输入密码
包含:可以使用元素其中包含的内容进行定位; 密码,输入,请
这里以雪球首页搜索框输入 alibaba 的简单场景举例,在搜索前还需要处理掉升级弹框,修改完成如下:
testcase:name: "XueQiuTestDemo AppCrawler"steps:- { xpath: "//*[contains(@resource-id,'image_cancel')]", action: click }- xpath: home_searchaction: click- xpath: search_input_textaction: alibaba
如上的 testcase 写法还要多说几句:
运行效果:

selectedList: 遍历范围设定
这里如果想设置让其点击所有可点击的TextView和ImageView控件,修改完成如下:
selectedList:- xpath: //android.widget.ImageView[@clickable='true']- xpath: //*[@clickable='true' and contains(@class,"Text")]
lastList:- { xpath: text_yes, action: click }
triggerActions:- { xpath: android.widget.EditText, action: 123456abc, times: 2 }
tagLimitMax: 2
ListView类型的只点击一次; 修改完成如下 :
- { xpath: "//*[contains(@class, 'List')]//*", times: 1 }
到这里,appcrawler的基本语法和参数含义大致介绍了一遍,后面就是如何结合实际进行使用了。下一篇将进行详细的介绍。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号