兩年前剛進(jìn)公司的時候用過appium劝评,最后因換項目也就沒再研究過了因惭。就在前兩個月一個通過閱讀新聞賺錢的App讓我意識到 也許appium可以這么干屈嗤,哈哈潘拨。結(jié)果很不理想,這里就分享一下過程吧恢共!
我選擇的方案是 appium1.8.1 + 夜神模擬器 + Python腳本
準(zhǔn)備工作
安裝夜神模擬器
安裝過程就不說了战秋,需要注意的是在環(huán)境變量添加 夜神模擬器adb的路徑。
然后啟動模擬器讨韭,再打開命令行輸入 adb devices
adb devices
List of devices attached
127.0.0.1:62001 device
有設(shè)備列表就對了 62001 就是模擬器的端口脂信,可以通過adb執(zhí)行命令癣蟋,appium也是通過這個來連通設(shè)備的
安裝Appium
準(zhǔn)備材料
Appium國內(nèi)下載地址
Appium中文文檔
Appium各種資料 來自testerhome
我安裝的是1.8.1 安裝過程略過
配置環(huán)境變量
H:\soft\Appium\node_modules\.bin
然后安裝nodejs ,配置jdk狰闪,Android SDK 疯搅,Windows下這些軟件的安裝過程都略過
Appium原理
開始
下載趣頭條apk文件,下載apktool工具其實就是一個jar包
將這兩個文件放在同一個文件夾(自己視情況而定)埋泵,在當(dāng)前文件夾打開命令行幔欧,使用apktool反編譯趣頭條
反編譯命令
java -jar .\apktool_2.3.3.jar d .\qukan.apk
完成后會多一個目錄,用編輯器打開里面的AndroidManifest.xml
要找的Appium啟動app需要的兩個參數(shù)appPackage丽声,appActivity
啟動Appium服務(wù)
沒什么特別的直接打開使用默認(rèn)端口就行礁蔗,如果被占用就改一下
然后打開一個inspector session
界面如下
輸入啟動參數(shù),可以鍵值對雁社,也可以直接編輯右邊的json數(shù)據(jù)浴井,我還是覺得json快一些
啟動后是這樣的
這些功能用了選取元素。
下面是我之前做好的一下分析:
1:啟動會出現(xiàn)紅包掛件 --> 掛件信息 如下
Attribute Value
index 0
class android.widget.LinearLayout
package com.jifen.qukan
content-desc
checkable false
checked false
clickable false
enabled true
focusable false
focused false
scrollable false
long-clickable false
password false
selected false
bounds [171,372][548,945]
resource-id
instance 0
判斷 -> (如果有掛件){
通過點擊關(guān)閉紅包掛件
}
關(guān)閉按鍵信息為:
Attribute Value
index 1
text 先去逛逛
class android.widget.TextView
package com.jifen.qukan
content-desc
checkable false
checked false
clickable true
enabled true
focusable false
focused false
scrollable false
long-clickable false
password false
selected false
bounds [171,913][548,945]
resource-id com.jifen.qukan:id/a0d
instance 5
2:開始讀取文章列表
文章頁面又分為兩部分:
第一部分 ---- 新聞分類: 資源:ID com.jifen.qukan:id/qk -> class:android.widget.LinearLayout
分類標(biāo)題: 對應(yīng)為:[class android.widget.TextView] 數(shù)組
第二部分 ---- 主體內(nèi)容: resource-id com.jifen.qukan:id/kt
新聞列表: [主體內(nèi)容中子元素] -> com.jifen.qukan:id/uo
3霉撵,點擊進(jìn)入新聞頁面:(判斷有無紅包掛件){ resource-id com.jifen.qukan:id/a86
有 則寫上下滑動手勢 獲取金幣
}else{
返回到列表磺浙,閱讀下一個
}
編寫python腳本
安裝Appium-Python-Client
pip install Appium-Python-Client
下面是我寫的腳本代碼
from appium import webdriver
from time import sleep
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4.2'
desired_caps['deviceName'] = '127.0.0.1:62001 device'
desired_caps['appPackage'] = 'com.jifen.qukan'
desired_caps['appActivity'] = 'com.jifen.qkbase.view.activity.JumpActivity'
desired_caps['noReset'] = 'true'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
def swipeUp(driver, t=500, n=1):
'''向上滑動屏幕'''
l = driver.get_window_size()
x1 = l['width'] * 0.5 # x坐標(biāo)
y1 = l['height'] * 0.75 # 起始y坐標(biāo)
y2 = l['height'] * 0.25 # 終點y坐標(biāo)
for i in range(n):
driver.swipe(x1, y1, x1, y2, t)
def swipeDown(driver, t=500, n=1):
'''向下滑動屏幕'''
l = driver.get_window_size()
x1 = l['width'] * 0.5 # x坐標(biāo)
y1 = l['height'] * 0.25 # 起始y坐標(biāo)
y2 = l['height'] * 0.75 # 終點y坐標(biāo)
for i in range(n):
driver.swipe(x1, y1, x1, y2,t)
def swipLeft(driver, t=500, n=1):
'''向左滑動屏幕'''
l = driver.get_window_size()
x1 = l['width'] * 0.75
y1 = l['height'] * 0.5
x2 = l['width'] * 0.25
for i in range(n):
driver.swipe(x1, y1, x2, y1, t)
def swipRight(driver, t=500, n=1):
'''向右滑動屏幕'''
l = driver.get_window_size()
x1 = l['width'] * 0.25
y1 = l['height'] * 0.5
x2 = l['width'] * 0.75
for i in range(n):
driver.swipe(x1, y1, x2, y1, t)
# 選擇新聞
def selectNews(driver):
news = []
try:
news = driver.find_element_by_id("com.jifen.qukan:id/kt").find_elements_by_xpath(
"http://*[@resource-id='com.jifen.qukan:id/uo']")
except BaseException as e:
swipeUp(driver,2000,1)
news = selectNews(driver)
return news
# 閱讀新聞
def readNew(driver):
news = selectNews(driver)
for index,item1 in enumerate(news):
print(item1.text)
item1.click()
# 等待頁面加載
sleep(3)
# 檢查是否是廣告
try:
gbBtn = driver.find_elements_by_xpath("http://android.widget.TextView[@text='關(guān)閉']")
if (gbBtn):
driver.keyevent(4)
except BaseException as e:
print(e)
swipeUp(driver,6000,7)
swipeDown(driver, 6000, 7)
sleep(3)
driver.keyevent(4)
sleep(3)
# main
header = driver.find_element_by_id("com.jifen.qukan:id/qk").find_element_by_class_name("android.widget.LinearLayout")
list = header.find_elements_by_class_name("android.widget.TextView")
# 找到標(biāo)題元素 循環(huán)點擊
for index,item in enumerate(list):
print(item.text)
item = list[index+4]
if(index==6):
# 如果到了第六個滑動一下
swipLeft(item,3)
item.click()
# 然后開始查找新聞列表
# 閱讀新聞函數(shù) # 每個分類閱讀的頁數(shù) pageNum = 0
while(pageNum<10):
readNew(driver)
swipeUp(driver,2000,1)
pageNum+=1
print(list)
driver.quit()
以上腳本運行就可以讓新聞自動閱讀啦。
記錄備忘