前言
AppCrawler是一个基于自动遍历的app爬虫工具. 支持android和iOS, 支持真机和模拟器. 最大的特点是灵活性. 可通过配置来设定遍历的规则。
AppCrawler地址
环境准备
jdk1.8
appium-seriver
AppCrawler
快速遍历
安装好环境后,可以执行以下命令, 命令参数介绍
# 查看帮助文档
java -jar appcrawler.jar
# 运行测试
java -jar appcrawler.jar -a xueqiu.apk
通过配置文件执行
配置文件可以帮助我们自定义遍历规则,和自定义测试用例。
- 生成配置文件
# 生成配置文件
java -jar appcrawler.jar --demo
- 配置文件内容
---
# 插件列表
pluginList: []
# 是否截图
saveScreen: true
# 报告的标题
reportTitle: ""
# 结果目录
resultDir: "20190907185946"
# 在执行操作后等待多少毫秒进行刷新
waitLoading: 500
waitLaunch: 6000
# 显示取消
showCancel: true
# 最大运行时间
maxTime: 10800
# 默认的最大深度
maxDepth: 10
# appium的capability通用配置
capability:
noReset: "true"
fullReset: "false"
appium: "http://127.0.0.1:4723/wd/hub"
# 测试用例
testcase:
name: "TesterHome AppCrawler"
steps:
- given: []
when: null
then: []
xpath: "/*"
action: "Thread.sleep(5000)"
actions: []
times: 0
# 默认遍历列表
selectedList:
- given: []
when: null
then: []
xpath: "//*[contains(name(), 'Button')]"
action: null
actions: []
times: 0
- given: []
when: null
then: []
xpath: "//*[contains(name(), 'Text') and @clickable='true' and string-length(@text)<10]"
action: null
actions: []
times: 0
- given: []
when: null
then: []
xpath: "//*[@clickable='true']/*[contains(name(), 'Text') and string-length(@text)<10]"
action: null
actions: []
times: 0
- given: []
when: null
then: []
xpath: "//*[contains(name(), 'Image') and @clickable='true']"
action: null
actions: []
times: 0
- given: []
when: null
then: []
xpath: "//*[@clickable='true']/*[contains(name(), 'Image')]"
action: null
actions: []
times: 0
- given: []
when: null
then: []
xpath: "//*[contains(name(), 'Image') and @name!='']"
action: null
actions: []
times: 0
- given: []
when: null
then: []
xpath: "//*[contains(name(), 'Text') and @name!='' and string-length(@label)<10]"
action: null
actions: []
times: 0
# 优先遍历元素
firstList: []
# 最后遍历列表
lastList:
- given: []
when: null
then: []
xpath: "//*[@selected='true']/..//*"
action: null
actions: []
times: 0
- given: []
when: null
then: []
xpath: "//*[@selected='true']/../..//*"
action: null
actions: []
times: 0
# 当所有元素都被点击后默认后退控件定位
backButton:
- given: []
when: null
then: []
xpath: "Navigate up"
action: null
actions: []
times: 0
# 特定条件触发执⾏动作的设置,通常⽤于处理弹框
triggerActions:
- given: []
when: null
then: []
xpath: "share_comment_guide_btn"
action: null
actions: []
times: 0
# 自动生成的xpath表达式里可以包含的匹配属性
xpathAttributes:
- "name"
- "label"
- "value"
- "resource-id"
- "content-desc"
- "instance"
- "text"
# 先按照深度depth排序,再按照list排序,最后按照selected排序。后排序是优先级别最高的
sortByAttribute:
- "depth"
- "list"
- "selected"
# 可选 default|android|id|xpath,默认状态会自动判断是否使用android定位或者ios定位
findBy: "default"
# 用来确定url的元素定位xpath 他的text会被取出当作url因素
defineUrl: []
# 设置一个起始url和maxDepth, 用来在遍历时候指定初始状态和遍历深度
baseUrl: []
# app白名单
appWhiteList: []
# url黑名单,用于排除某些页面
urlBlackList: []
# url白名单
urlWhiteList: []
# 黑名单列表 匹配风格, 默认排除内容是2个数字以上的控件
blackList:
- given: []
when: null
then: []
xpath: ".*[0-9]{2}.*"
action: null
actions: []
times: 0
# 在重启session之前做的事情
beforeRestart: []
# 在执行action之前和之后默认执行的动作,比如等待
beforeElement:
- given: []
when: null
then: []
xpath: "/*"
action: "Thread.sleep(500)"
actions: []
times: 0
# 是否需要刷新或者滑动
afterElement: []
afterPage: []
# afterPage执行多少次后才不执行,比如连续滑动2次都没新元素即取消
afterPageMax: 2
# 同祖先(同类型)的元素最多点击多少次
tagLimitMax: 2
# 个别控件可例外
tagLimit:
- given: []
when: null
then: []
xpath: "确定"
action: null
actions: []
times: 1000
- given: []
when: null
then: []
xpath: "取消"
action: null
actions: []
times: 1000
- given: []
when: null
then: []
xpath: "share_comment_guide_btn_name"
action: null
actions: []
times: 1000
# 只需要写given与then即可
assertGlobal: []
配置文件介绍
配置文件的参数说明
如上的配置文件是完整形态,还支持简写形态,拿testcase举例
1. testcase
- testcase的完整形态
- given:所有的先决条件(先决条件通过xpath的方式进行匹配)
- when:先决条件成⽴后的⾏为
- then:断言集合
- testcase的简写形态
- xpath:对应when⾥的xpath
- action:对应when的action
实例
testcase:
name: "点击行情" # 测试用例名称
steps:
- xpath: //*[contains(@resource-id, "tab_name") and @text="行情"] #定位模式
action: click # 动作
then:
- //*[contains(@text,'雪球热股')] # 断言
2. 定位模式 xpath
定位模式支持xpath定位、正则、迷糊匹配(注意:配置文件中的xpath定位模式别和appium的xpath搞混了,配置文件中的xpath只是一个参数,它支持appium的xpath的定位方法,并且还支持正则和模糊匹配)
-
xpath
- //*[@resource-id=‘xxxx’]
- //*[contains(@text, ‘密码’)]
-
正则
- ^确定$
- ^.*输入密码
-
包含
- 密码
- 输入
- 请
实例
blackList:
- xpath: ".*[0-9]{2}.*"
- xpath: "^雪球$"
- xpath: //*[@resource-id="com.xueqiu.android:id/action_search"]
- xpath: 雪
3. 动作支持 action
- " " 只是截图记录
- back 后退
- backApp 回退到当前的app 默认等价于back⾏为 可定制
- monkey 随机事件
- xxx() 执⾏代码
- Thread.sleep(3000)
- driver.swipe(0.9, 0.5, 0.1, 0.5)
- click
- longTap
- 非以上所有行为是输入 123 abcd
4. 动作次数 times
# 点击净买入10次
triggerActions:
- xpath: 净买入
times: 10
实例自动遍历雪球app行情页面下的所有二级页面
- 安装模拟器,如mumu
- 安装雪球app
- 启动appium server
appium --session-override
- 生成配置文件
java -jar appcrawler-2.4.0-jar-with-dependencies.jar --demo
- 修改配置文件,另存为
xueqiu.yml
.(注意,保存为utf8编码)
---
pluginList: []
saveScreen: true
reportTitle: "遍历雪球行情页的所有二级页面"
resultDir: "20190907221950"
waitLoading: 500
waitLaunch: 6000
showCancel: true
maxTime: 10800
maxDepth: 1
capability:
noReset: "true"
fullReset: "false"
appium: "http://127.0.0.1:4723/wd/hub"
appPackage: com.xueqiu.android
appActivity: .view.WelcomeActivityAlias
testcase:
name: "点击行情"
steps:
- xpath: //*[contains(@resource-id, "tab_name") and @text="行情"]
action: click
then:
- //*[contains(@text,'雪球热股')]
# 遍历雪球行情页
selectedList:
- xpath: "//*[contains(name(), 'Button')]"
- xpath: "//*[contains(name(), 'Text') and @clickable='true' and string-length(@text)<10]"
- xpath: "//*[@clickable='true']/*[contains(name(), 'Text') and string-length(@text)<10]"
- xpath: "//*[contains(name(), 'Image') and @clickable='true']"
- xpath: "//*[@clickable='true']/*[contains(name(), 'Image')]"
- xpath: "//*[contains(name(), 'Image') and @name!='']"
- xpath: "//*[contains(name(), 'Text') and @name!='' and string-length(@label)<10]"
firstList: []
lastList:
- xpath: "//*[@selected='true']/..//*"
- xpath: "//*[@selected='true']/../..//*"
backButton:
- xpath: "Navigate up"
triggerActions:
- xpath: "share_comment_guide_btn"
xpathAttributes:
- "name"
- "label"
- "value"
- "resource-id"
- "content-desc"
- "instance"
- "text"
sortByAttribute:
- "depth"
- "list"
- "selected"
findBy: "default"
defineUrl: []
baseUrl: []
appWhiteList: []
urlBlackList: []
urlWhiteList: []
# 添加黑名单元素,避免点击到其他地方
blackList:
- xpath: ".*[0-9]{2}.*"
- xpath: //*[contains(@resource-id, "tab_name") and @text="雪球"]
- xpath: //*[contains(@resource-id, "tab_name") and @text="自选"]
- xpath: //*[contains(@resource-id, "tab_name") and @text="关注"]
- xpath: //*[contains(@resource-id, "tab_name") and @text="交易"]
- xpath: //*[@resource-id="com.xueqiu.android:id/action_search"]
beforeRestart: []
beforeElement:
- given: []
when: null
then: []
xpath: "/*"
action: "Thread.sleep(500)"
actions: []
times: 0
afterElement: []
afterPage: []
afterPageMax: 2
tagLimitMax: 2
tagLimit:
- given: []
when: null
then: []
xpath: "确定"
action: null
actions: []
times: 1000
- given: []
when: null
then: []
xpath: "取消"
action: null
actions: []
times: 1000
- given: []
when: null
then: []
xpath: "share_comment_guide_btn_name"
action: null
actions: []
times: 1000
assertGlobal: []
- 连接模拟器
# 连接模拟器
adb connect 127.0.0.1:7555
# 查看是否连接成功
adb devices
- 执行测试
java -jar appcrawler-2.4.0-jar-with-dependencies.jar -c xueqiu.yml
-
测试报告