前言
AppCrawler是一個基于自動遍歷的app爬蟲工具. 支持android和iOS, 支持真機和模擬器. 最大的特點是靈活性. 可通過配置來設定遍歷的規(guī)則似踱。
AppCrawler地址
環(huán)境準備
快速遍歷
安裝好環(huán)境后趋艘,可以執(zhí)行以下命令, 命令參數(shù)介紹
# 查看幫助文檔
java -jar appcrawler.jar
# 運行測試
java -jar appcrawler.jar -a xueqiu.apk
通過配置文件執(zhí)行
配置文件可以幫助我們自定義遍歷規(guī)則砾脑,和自定義測試用例。
- 生成配置文件
# 生成配置文件
java -jar appcrawler.jar --demo
- 配置文件內容
---
# 插件列表
pluginList: []
# 是否截圖
saveScreen: true
# 報告的標題
reportTitle: ""
# 結果目錄
resultDir: "20190907185946"
# 在執(zhí)行操作后等待多少毫秒進行刷新
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: "http://*[contains(name(), 'Button')]"
action: null
actions: []
times: 0
- given: []
when: null
then: []
xpath: "http://*[contains(name(), 'Text') and @clickable='true' and string-length(@text)<10]"
action: null
actions: []
times: 0
- given: []
when: null
then: []
xpath: "http://*[@clickable='true']/*[contains(name(), 'Text') and string-length(@text)<10]"
action: null
actions: []
times: 0
- given: []
when: null
then: []
xpath: "http://*[contains(name(), 'Image') and @clickable='true']"
action: null
actions: []
times: 0
- given: []
when: null
then: []
xpath: "http://*[@clickable='true']/*[contains(name(), 'Image')]"
action: null
actions: []
times: 0
- given: []
when: null
then: []
xpath: "http://*[contains(name(), 'Image') and @name!='']"
action: null
actions: []
times: 0
- given: []
when: null
then: []
xpath: "http://*[contains(name(), 'Text') and @name!='' and string-length(@label)<10]"
action: null
actions: []
times: 0
# 優(yōu)先遍歷元素
firstList: []
# 最后遍歷列表
lastList:
- given: []
when: null
then: []
xpath: "http://*[@selected='true']/..//*"
action: null
actions: []
times: 0
- given: []
when: null
then: []
xpath: "http://*[@selected='true']/../..//*"
action: null
actions: []
times: 0
# 當所有元素都被點擊后默認后退控件定位
backButton:
- given: []
when: null
then: []
xpath: "Navigate up"
action: null
actions: []
times: 0
# 特定條件觸發(fā)執(zhí)?動作的設置,通常?于處理彈框
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排序尔艇。后排序是優(yōu)先級別最高的
sortByAttribute:
- "depth"
- "list"
- "selected"
# 可選 default|android|id|xpath,默認狀態(tài)會自動判斷是否使用android定位或者ios定位
findBy: "default"
# 用來確定url的元素定位xpath 他的text會被取出當作url因素
defineUrl: []
# 設置一個起始url和maxDepth, 用來在遍歷時候指定初始狀態(tài)和遍歷深度
baseUrl: []
# app白名單
appWhiteList: []
# url黑名單咆疗,用于排除某些頁面
urlBlackList: []
# url白名單
urlWhiteList: []
# 黑名單列表 匹配風格, 默認排除內容是2個數(shù)字以上的控件
blackList:
- given: []
when: null
then: []
xpath: ".*[0-9]{2}.*"
action: null
actions: []
times: 0
# 在重啟session之前做的事情
beforeRestart: []
# 在執(zhí)行action之前和之后默認執(zhí)行的動作漓帚,比如等待
beforeElement:
- given: []
when: null
then: []
xpath: "/*"
action: "Thread.sleep(500)"
actions: []
times: 0
# 是否需要刷新或者滑動
afterElement: []
afterPage: []
# afterPage執(zhí)行多少次后才不執(zhí)行母债,比如連續(xù)滑動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: []
配置文件的參數(shù)說明
如上的配置文件是完整形態(tài)午磁,還支持簡寫形態(tài),拿testcase舉例
1. testcase
- testcase的完整形態(tài)
- given:所有的先決條件(先決條件通過xpath的方式進行匹配)
- when:先決條件成?后的?為
- then:斷言集合
- testcase的簡寫形態(tài)
- 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只是一個參數(shù)衙熔,它支持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() 執(zhí)?代碼
- Thread.sleep(3000)
- driver.swipe(0.9, 0.5, 0.1, 0.5)
- click
- longTap
- 非以上所有行為是輸入 123 abcd
4. 動作次數(shù) 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: "http://*[contains(name(), 'Button')]"
- xpath: "http://*[contains(name(), 'Text') and @clickable='true' and string-length(@text)<10]"
- xpath: "http://*[@clickable='true']/*[contains(name(), 'Text') and string-length(@text)<10]"
- xpath: "http://*[contains(name(), 'Image') and @clickable='true']"
- xpath: "http://*[@clickable='true']/*[contains(name(), 'Image')]"
- xpath: "http://*[contains(name(), 'Image') and @name!='']"
- xpath: "http://*[contains(name(), 'Text') and @name!='' and string-length(@label)<10]"
firstList: []
lastList:
- xpath: "http://*[@selected='true']/..//*"
- xpath: "http://*[@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
- 執(zhí)行測試
java -jar appcrawler-2.4.0-jar-with-dependencies.jar -c xueqiu.yml
-
測試報告
測試報告