帶你用 Python 實(shí)現(xiàn)自動(dòng)化群控(入門篇)
回復(fù)“書籍”即可獲贈(zèng)Python從入門到進(jìn)階共10本電子書

1. 前言
群控,相信大部分人都不會(huì)陌生!印象里是一臺(tái)電腦控制多臺(tái)設(shè)備完成一系列的操作,更多的人喜歡把它和 Hui 產(chǎn)綁定在一起!
事實(shí)上,群控在自動(dòng)化測(cè)試中也被廣泛使用!接下來(lái)的幾篇文章,我將帶大家聊聊企業(yè)級(jí)自動(dòng)化中,群控正確的使用姿勢(shì)!
本篇先從基礎(chǔ)篇開(kāi)始,聊聊使用「?Python + adb?」命令如何編寫一套群控腳本
2. 準(zhǔn)備
在本機(jī)安裝?Android 開(kāi)發(fā)環(huán)境,保證?adb 被添加到環(huán)境變量
將準(zhǔn)備好的多臺(tái)設(shè)備,使用數(shù)據(jù)線( 或者通過(guò) Hub )連接到電腦上
通過(guò)?adb devices?命令查看已經(jīng)連接的所有設(shè)備
#?下面顯示連接了3臺(tái)設(shè)備
xag:Test?xingag$?adb?devices
List?of?devices?attached
822QEDTL225T7????device
ca2b3455????????device
DE45d9323SE96???device
3. 實(shí)戰(zhàn)
#?獲取當(dāng)前運(yùn)行應(yīng)用的包名及初始Activity
adb?shell?dumpsys?activity?|?grep?-i?run
#?所有設(shè)備ID
devices?=?[]
def?get_online_devices(self):
????"""
????獲取所有在線的設(shè)備
????:return:
????"""
????global?devices
????try:
????????for?device_serias_name?in?exec_cmd("adb?devices"):
???????????#?過(guò)濾掉第一條數(shù)據(jù)及不在線的設(shè)備
???????????if?"device"?in?device_serias_name:
??????????????devices.append(device_serias_name.split("\t")[0])
???????????devices?=?devices[1:]
????except?Exception?as?e:
????????????print(e)
????#?連上的所有設(shè)備及數(shù)量
????return?devices
def?start_app(self):
????"""
????打開(kāi)App
????:return:?
????"""
????for?device?in?devices:
????????os.popen("adb?-s?"?+?device?+?"?shell?am?start?-W?{}/{}".format(self.packageName,?self.home_activity))
????print('等待加載完成...')
????sleep(10)
#?steps_adb.yaml
#?包名和Activity
package_name:??com.taobao.idlefish
home_activity:??com.taobao.fleamarket.home.activity.InitActivity
#?執(zhí)行步驟
steps:
??-?save_ui_tree_to_local:
??????method:??save_ui_tree_to_local
??????args:
??-?find_element_and_click:
??????id:??com.taobao.idlefish:id/tx_id
??-?save_ui_tree_to_local:
??????method:??save_ui_tree_to_local
??-?input_content:
??????content:??Python
??-?find_element_and_click:
??????id:??com.taobao.idlefish:id/search_button
導(dǎo)出界面的控件樹(shù)
解析控件樹(shù) XML 文件,利用正則表達(dá)式得到目標(biāo)控件的坐標(biāo)值
計(jì)算出控件的中心點(diǎn)坐標(biāo)

利用控件 ID 獲取元素中心點(diǎn)坐標(biāo)的實(shí)現(xiàn)代碼如下:
def?get_element_position(element_id,?uidump_name):
????"""
????通過(guò)元素的id,使用ElementTree,解析元素控件樹(shù),查找元素的坐標(biāo)中心點(diǎn)
????:param element_id:?元素id,比如:
????:return:?元素坐標(biāo)
????"""
????#?解析XML
????tree?=?ET.parse('./../%s.xml'?%?uidump_name)
????root?=?tree.getroot()
????#?待查找的元素
????result_element?=?None
????#?print('查找數(shù)目',?len(root.findall('.//node')))
????#?遍歷查找node元素
????#?通過(guò)元素id
????for?node_element?in?root.findall('.//node'):
????????if?node_element.attrib['resource-id']?==?element_id:
????????????result_element?=?node_element
????????????break
????#?如果找不到元素,直接返回空
????if?result_element?is?None:
????????print('抱歉!找不到元素!')
????????return?None
????#?解析數(shù)據(jù)
????coord?=?re.compile(r"\d+").findall(result_element.attrib['bounds'])
????#?中心點(diǎn)坐標(biāo)
????position_center?=?int((int(coord[0])?+?int(coord[2]))?/?2),?int((int(coord[1])?+?int(coord[3]))?/?2)
????return?position_center
def?save_ui_tree_to_local(dName):
????"""
????獲取當(dāng)前Activity控件樹(shù),保存到本地
????文件名固定為:uidump.xml
????:param?dName:?設(shè)備id
????:return:
????"""
????exec_cmd("adb??-s?%s?shell?uiautomator?dump?/data/local/tmp/%s.xml"?%?(dName,?dName))
????sleep(2)
????exec_cmd("adb?-s?%s?pull?/data/local/tmp/%s.xml?./../"?%?(dName,?dName))
#?執(zhí)行步驟
for?step?in?self.steps:
????#?設(shè)備
????for?device?in?devices:?
????????pass
#?操作名稱
step_name?=?list(step)[0]
if?step_name?==?'save_ui_tree_to_local':
????#?保存UI數(shù)到本地
????method?=?step.get(step_name).get('method')
????save_ui_tree_to_local(device)
elif?step_name?==?'find_element_and_click':
????element_id?=?step.get(step_name).get('id')
????#?獲取元素的坐標(biāo)
????bound_search_input?=?get_element_position(element_id,?device)
????#?點(diǎn)擊元素
????exec_cmd('adb?-s?%s?shell?input?tap?%s?%s'?%?(device,?bound_search_input[0],?bound_search_input[1]))
elif?step_name?==?'input_content':
????input_content?=?step.get(step_name).get('content')
????#?模擬輸入
????exec_cmd('adb?-s?%s?shell?input?text?%s'?%?(device,?input_content))
else:
????print('其他操作步驟')def?stop_all(self):
???"""
???關(guān)閉應(yīng)用
???:return:
???"""
???for?device?in?devices:
???????os.popen("adb?-s?"?+?device?+?"?shell?am?force-stop??%s"?%?self.packageName)4. 最后
本篇僅僅是 Python 自動(dòng)化群控最簡(jiǎn)單的實(shí)現(xiàn)方式,后面將和大家討論更加復(fù)雜的實(shí)現(xiàn)方式。
我已經(jīng)將文中全部源碼上傳到后臺(tái),關(guān)注公眾號(hào)后回復(fù)「?qk?」即可獲得全部源碼
如果你覺(jué)得文章還不錯(cuò),請(qǐng)大家?點(diǎn)贊、分享、留言下,因?yàn)檫@將是我持續(xù)輸出更多優(yōu)質(zhì)文章的最強(qiáng)動(dòng)力!
-------------------?End?-------------------
往期精彩文章推薦:
手把手教你使用Flask搭建ES搜索引擎(實(shí)戰(zhàn)篇)
簡(jiǎn)述Python、Anaconda、virtualenv和Miniconda之間的區(qū)別
【進(jìn)階篇】Python+Go——帶大家一起另尋途徑提高計(jì)算性能

歡迎大家點(diǎn)贊,留言,轉(zhuǎn)發(fā),轉(zhuǎn)載,感謝大家的相伴與支持
想加入Python學(xué)習(xí)群請(qǐng)?jiān)诤笈_(tái)回復(fù)【入群】
萬(wàn)水千山總是情,點(diǎn)個(gè)【在看】行不行
/今日留言主題/
隨便說(shuō)一兩句吧~~
