例:在手機(jī)中自動(dòng)刷 學(xué)習(xí)QG
- 環(huán)境配置
- python:安裝anoconda(也可以只安裝Python)示辈,安裝uiautomator2和weditor兩個(gè)包
- adb:安裝Android Studio(也可以只安裝Android SDK)取胎,配置環(huán)境變量(ANDROID_HOME和Path【..Sdk\platform-tools】)闽晦,可能無(wú)需配置
- cmd調(diào)試
- Adb devices 查看是否連接上劫窒,連接上會(huì)顯示deviceName
- 連接手機(jī)以后 莹桅,python -m uiautomator2 init 對(duì)手機(jī)進(jìn)行初始化
-
python -m weditor 打開(kāi)網(wǎng)頁(yè),逐步調(diào)試烛亦,可獲取package和當(dāng)前activity
3.1 其他方式參見(jiàn) app的自動(dòng)化測(cè)試(1)
- 編寫(xiě)python程序
- 每個(gè)動(dòng)作都設(shè)置了sleep時(shí)間诈泼,不設(shè)置很可能會(huì)由于加載問(wèn)題而出錯(cuò),其余代碼不做解釋?zhuān)斠?jiàn)注釋煤禽,該代碼由 app的自動(dòng)化測(cè)試(1) 改編铐达,可以看出,uiautomator2簡(jiǎn)單很多檬果。
import time
import datetime
import random
import uiautomator2 as u2
# 讓時(shí)間有個(gè)范圍
def rand(x):
return x*random.randint(90,110)/100
class auto_app():
# 今天瓮孙,昨天
def __init__(self):
today = datetime.datetime.today().date()
self.date_now = str(today)
self.date_yesterday = str(today-datetime.timedelta(days=1))
# 看文章
# 文章閱讀方式
def read(self,list,times): # 閱讀driver鏈接列表唐断,閱讀時(shí)間10*(time+1)秒
for i in list:
i.click()
time.sleep(rand(10))
for j in range(0,times):
x1 = rand(800)
d.swipe(x1,rand(1000),x1,rand(700)) # swipe移動(dòng),向下滑
time.sleep(rand(10))
d.click(rand(0.073), rand(0.073)) # 閱讀后返回
time.sleep(rand(2))
def article(self):
time.sleep(rand(2))
d.xpath("http://*[@text='綜合']").click()
time.sleep(rand(2))
x1 = rand(800)
[d.swipe(x1,rand(1000),x1,rand(400)) for x in range(3)] # 下滑,接近半屏
time.sleep(rand(1))
articlelist1=d.xpath("http://*[@text='%s']" % self.date_now).all() # 跟selenium有所不同
print("閱讀文章:",len(articlelist1))
self.read(articlelist1,10) # 閱讀
time.sleep(rand(2))
d.swipe(x1,rand(1000),x1,rand(400))
articlelist0=d.xpath("http://*[@text='%s']" % self.date_now).all()
articlelist=d.xpath("http://*[@text='%s']" % self.date_yesterday).all()
print("閱讀文章:",len(articlelist0))
print("閱讀文章:",len(articlelist))
self.read(set(articlelist0)-set(articlelist1),12) # 閱讀
self.read(set(articlelist)-set(articlelist1),12)
time.sleep(rand(2))
# 看視頻 (聯(lián)播頻道5)
# 視頻觀看方式
def watch(self,list,times1): # time*10秒
for i in list:
i.click()
time.sleep(rand(10))
k=0
while (not d.xpath("http://*[@text='重新播放']").exists): # 不出現(xiàn)“重新播放”杭抠,持續(xù)觀看
k=k+1
time.sleep(rand(10))
if k>times1: # 大于time*10s脸甘,停止觀看
break
d.click(rand(0.073), rand(0.073)) # 返回
time.sleep(rand(2))
def video(self):
time.sleep(rand(2))
d.xpath("http://*[@text='電視臺(tái)']").click()
time.sleep(rand(2))
d.xpath("http://*[@text='聯(lián)播頻道']").click()
time.sleep(rand(2))
videolist0=d.xpath("http://*[@text='%s']" % self.date_yesterday).all()
print("觀看視頻:",len(videolist0))
self.watch(videolist0,45)
time.sleep(rand(2))
x1=rand(800)
[d.swipe(x1,rand(1000),x1,rand(400)) for x in range(2)] # 能夠獲取更多的driver,說(shuō)明app是不滑動(dòng)不加載
time.sleep(rand(1))
videolist=d.xpath("http://*[@text='%s']" % self.date_yesterday).all() # 跟selenium有所不同
print("觀看視頻:",len(set(videolist)-set(videolist0)))
self.watch(set(videolist)-set(videolist0),36)
if __name__ == "__main__":
d=u2.connect('872QED*****')
print(datetime.datetime.today())
read=auto_app()
if False:
d.app_start('com.kuaishou.nebula')
while(True):
d.swipe(rand(800),rand(1000),rand(800),rand(200))
time.sleep(rand(10))
if True:
d.app_start('cn.xuexi.android')
read.article()
read.video()
print(datetime.datetime.today())