Window10系統php
Appium1.21.0html
AndroidSDK r24.1.1python
Python3.7.5android
支付寶apk文件git
使用android sdk aapt命令查看支付寶apk包信息,後面會用到,以下。github
Android Asset Packaging Tool
Usage:
aapt l[ist] [-v] [-a] file.{zip,jar,apk}
List contents of Zip-compatible archive.
aapt d[ump] [--values] [--include-meta-data] WHAT file.{apk} [asset [asset ...]]
strings Print the contents of the resource table string pool in the APK.
badging Print the label and icon for the app declared in APK.
permissions Print the permissions from the APK.
resources Print the resource table from the APK.
configurations Print the configurations in the APK.
xmltree Print the compiled xmls in the given assets.
xmlstrings Print the strings of the given compiled xml assets.
D:\android-sdk-windows\build-tools\29.0.0> .\aapt.exe dump badging C:\Users\XXX\Downloads\alipay_wap_main.apk
輸出結果以下:
package: name='com.eg.android.AlipayGphone'
versionCode='410'
versionName='10.2.26.9000'
compileSdkVersion='29'
compileSdkVersionCodename='10'
install-location:'auto'
sdkVersion:'18'
targetSdkVersion:'29'
launchable-activity: name='com.eg.android.AlipayGphone.AlipayLogin'
一開始想用模擬器(如夜神模擬器)進行自動化,後來發現支付寶在模擬器裏運行特別卡,最終決定使用真機了。這裏使用android sdk的adb工具檢測手機設備是否鏈接正常,以下。若是看不到鏈接信息或者顯示unauthorized的,請開啓手機的USB調試權限,也有可能開啓開發者模式呦。web
pip install 默認安裝路徑修改windows
Appium工做原理android-studio
pip install Appium-Python-Client --user
An extension library for adding Selenium 3.0 draft and Mobile JSON Wire Protocol Specification draft functionality to the Python language bindings, for use with the mobile testing framework Appium.
pip install pytest --user
The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.
安裝包的時候加上參數--user 包就會自動安裝到自定義路徑下面
{
"deviceName": "Alipay-Test",
"platformName": "Android",
"platformVersion": "10",
"appActivity": "com.eg.android.AlipayGphone.AlipayLogin",
"appPackage": "com.eg.android.AlipayGphone",
"noReset": true,
"fullReset": false
}
知道如何定位支付寶界面的元素後,開始編寫python自動化運行腳本。大致分爲以下幾個步驟。
def setUp(self): desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = '10' desired_caps['deviceName'] = 'Alipay' desired_caps['appActivity'] = 'com.eg.android.AlipayGphone.AlipayLogin' desired_caps['appPackage'] = 'com.eg.android.AlipayGphone' desired_caps['noReset'] = True desired_caps['fullReset'] = False self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
客戶端初始化後會自動啓動支付寶APP,注意noReset和fullReset參數的設置。
If noReset is set to TRUE, the app data will NOT be cleared before this session starts.
If fullReset is set to true, the app will get uninstalled and all data will be cleared.
def unlocked(self): sz = self.getSize(); x1 = int(sz[0] * 0.10) #x座標 y1 = int(sz[1] * 0.95) #起始y座標 y2 = int(sz[1] * 0.15) #終點y座標 self.driver.swipe(x1, y1, x1, y2, 1000) sleep(1) try: self.driver.find_element_by_id('com.android.systemui:id/vivo_pin_keyboard') for k in [5,1,2,9,9,9]: self.driver.find_element_by_id('com.android.systemui:id/VivoPinkey%d' % k).click() print('手機解鎖成功...') except NoSuchElementException: print('手機已解鎖或解鎖失敗')
def entry_ant_forest(self): try: sleep(2) # 點擊螞蟻森林icon self.driver.find_element_by_android_uiautomator('new UiSelector().text("螞蟻森林")').click() except NoSuchElementException: # 異常回到首頁重試 self.driver.back() sleep(2) # 點擊支付寶icon self.driver.find_element_by_android_uiautomator('new UiSelector().text("支付寶")').click() sleep(2) # 點擊螞蟻森林icon self.driver.find_element_by_android_uiautomator('new UiSelector().text("螞蟻森林")').click()
按理說進入螞蟻森林直接模擬點擊「螞蟻森林」icon就能夠了,可是偶爾會拋出NoSuchElementException異常。也就是Appium在切換activity後致使元素沒法定位,若是手機不鎖屏不會發生這種狀況(能夠在開發者模式中指定),鎖屏解鎖後切換到支付寶的activity後偶爾會出現這種狀況。沒有找到太好的解決方法,發生異常時使手機界面返回到首頁,而後點擊支付寶從新進入,最後點擊螞蟻森林進入。
def search_energy(self): # 點擊找能量 self.driver.tap([(1000, 1520), (1080, 1580)], 1000) sleep(3) try: self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("好友能量都收完了")') except NoSuchElementException: try: self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("返回個人森林")') except NoSuchElementException: pass else: print('所有好友能量收取完畢...') return # 收取好友能量 self.collect_energy() # 繼續找能量 self.search_energy() else: print('所有好友能量收取完畢...')
點擊「找能量」功能自動定位到有能量偷取的好友界面。若是界面中有「好友能量都收完了」或者「返回個人森林」相關字樣,結束查找,不然開始收取好友能量。
def collect_energy(self): name = '' try: name = self.driver.find_element_by_id('com.alipay.mobile.nebula:id/h5_tv_title').text except NoSuchElementException: pass print('開始收取%s的能量...' % name) # 獲取手機屏幕寬高 sz = self.getSize(); width = sz[0] height = sz[1] # 能量球可能出現的區域座標 start_x = 110 end_x = 940 start_y = 460 end_y = 880 for i in range(start_y, end_y, 80): for j in range(start_x,end_x, 80): try: self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("關閉")').click() sleep(1) except NoSuchElementException: pass tap_x1 = int((int(j) / width) * width) tap_y1 = int((int(i) / height) * height) # 點擊指定座標 self.driver.tap([(tap_x1, tap_y1), (tap_x1, tap_y1)], 1000) print('結束收取%s的能量...' % name)
首先獲取當前頁面的「com.alipay.mobile.nebula:id/h5_tv_title」元素,表明好友的名字;由於螞蟻森林如今不能定位能量球元素了,因此要在能量球可能出現的方塊區域按照必定的座標步長模擬點擊進行能量偷取。上面的方案中,規定的能量球可能出現的區域爲[(110,460),(940,880)],這個座標能夠根據實際的機型進行修改,能夠經過Appium座標定位判斷出矩形區域,以下。
還要一個點須要注意的,點擊的時候可能會出現裝飾樹和掛件的展現,以下圖所示。這時候須要在界面中查找「關閉」元素,而後click事件關閉就好了。
若是效果感受還不錯的話,那就關注一下微信公衆號(請掃下方二維碼),回覆「螞蟻森林」獲取完整代碼吧。