python實(shí)現(xiàn)自動(dòng)化群控的步驟
群控,相信大部分人都不會(huì)陌生!印象里是一臺(tái)電腦控制多臺(tái)設(shè)備完成一系列的操作,更多的人喜歡把它和 Hui 產(chǎn)綁定在一起!
事實(shí)上,群控在自動(dòng)化測(cè)試中也被廣泛使用!接下來的幾篇文章,我將帶大家聊聊企業(yè)級(jí)自動(dòng)化中,群控正確的使用姿勢(shì)!
本篇先從基礎(chǔ)篇開始,聊聊使用「 Python + adb 」命令如何編寫一套群控腳本
2. 準(zhǔn)備在本機(jī)安裝 Android 開發(fā)環(huán)境,保證 adb 被添加到環(huán)境變量
將準(zhǔn)備好的多臺(tái)設(shè)備,使用數(shù)據(jù)線( 或者通過 Hub )連接到電腦上
通過 adb devices 命令查看已經(jīng)連接的所有設(shè)備
# 下面顯示連接了3臺(tái)設(shè)備xag:Test xingag$ adb devicesList of devices attached822QEDTL225T7 deviceca2b3455deviceDE45d9323SE96 device3. 實(shí)戰(zhàn)
自動(dòng)化群控以閑魚 App 的一次關(guān)鍵字搜索為例,步驟包含:打開應(yīng)用、點(diǎn)擊到搜索界面、輸入內(nèi)容、點(diǎn)擊搜索按鈕下面通過7步來完成這一操作
1、獲取目標(biāo)應(yīng)用的包名及初始化 Activity獲取方式有很多種,主流方式包含:adb 命令、解析 APK、第三方 APK、無障礙服務(wù)這里推薦使用 adb 命令這種方式
# 獲取當(dāng)前運(yùn)行應(yīng)用的包名及初始Activityadb shell dumpsys activity | grep -i run
打開閑魚 App,在命令終端輸入上面的命令,終端會(huì)將包名及 Activity 名稱顯示出來
通過 adb devices 命令,通過輸出內(nèi)容,進(jìn)行一次過濾,得到所有連接到 PC 端的設(shè)備
# 所有設(shè)備IDdevices = []def get_online_devices(self): ''' 獲取所有在線的設(shè)備 :return: ''' global devices try:for device_serias_name in exec_cmd('adb devices'): # 過濾掉第一條數(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 devices3、群控打開目標(biāo)應(yīng)用
遍歷設(shè)備列表,使用 adb -s 設(shè)備ID shell am start -W 命令分別打開目標(biāo)應(yīng)用
def start_app(self): ''' 打開App :return: ''' for device in devices:os.popen('adb -s ' + device + ' shell am start -W {}/{}'.format(self.packageName, self.home_activity)) print(’等待加載完成...’) sleep(10)4、封裝執(zhí)行步驟
為了方便管理設(shè)備,將每一步的操作寫入到Y(jié)AML文件中,可以通過 ID 查找元素并執(zhí)行點(diǎn)擊操作、在輸入框中輸入內(nèi)容、調(diào)用本地方法及輸入?yún)?shù)這里分別對(duì)應(yīng):保存 UI 樹控件、查找輸入框元素并執(zhí)行點(diǎn)擊操作、保存 UI 樹控件(界面變化了)、輸入文本內(nèi)容、查看搜索按鈕元素并執(zhí)行點(diǎn)擊操作
# steps_adb.yaml# 包名和Activitypackage_name: com.taobao.idlefishhome_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
需要指出的是,為了提高群控的適配性,控件的實(shí)際坐標(biāo)需要通過下面的步驟去獲取:
導(dǎo)出界面的控件樹 解析控件樹 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): ''' 通過元素的id,使用ElementTree,解析元素控件樹,查找元素的坐標(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元素 # 通過元素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_center5、區(qū)分設(shè)備
為了保證群控腳本執(zhí)行不會(huì)產(chǎn)生干擾,在每個(gè)步驟執(zhí)行之前,都應(yīng)該將設(shè)備 ID 作為參數(shù)進(jìn)行區(qū)分比如:將控件的界面控件樹按照設(shè)備保存為不同的名稱、點(diǎn)擊界面和輸入的命令傳相應(yīng)設(shè)備 ID 作為入?yún)?/p>
def save_ui_tree_to_local(dName): ''' 獲取當(dāng)前Activity控件樹,保存到本地 文件名固定為: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))6、執(zhí)行步驟
從 YAML 文件中讀取執(zhí)行步驟,遍歷步驟集合,內(nèi)部遍歷設(shè)備列表,以保證每一個(gè)步驟,分別執(zhí)行到每臺(tái)設(shè)備上
# 執(zhí)行步驟for step in self.steps: # 設(shè)備 for device in devices: pass
接著,通過步驟名稱匹配不同的操作,即可操作設(shè)備了
# 操作名稱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(’其他操作步驟’)7、關(guān)閉應(yīng)用
當(dāng)所有的操作完成之后,同樣是遍歷設(shè)備,利用 adb 命令去關(guān)閉 App 即可
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)方式。
項(xiàng)目地址:https://github.com/xingag/test_auto/tree/master/group_control
以上就是python實(shí)現(xiàn)自動(dòng)化群控的步驟的詳細(xì)內(nèi)容,更多關(guān)于python 自動(dòng)化群控的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. JSP的Cookie在登錄中的使用2. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實(shí)現(xiàn)代碼3. HTML5 Canvas繪制圖形從入門到精通4. 使用Spry輕松將XML數(shù)據(jù)顯示到HTML頁(yè)的方法5. 利用CSS3新特性創(chuàng)建透明邊框三角6. ASP基礎(chǔ)知識(shí)VBScript基本元素講解7. 詳解CSS偽元素的妙用單標(biāo)簽之美8. XHTML 1.0:標(biāo)記新的開端9. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究10. XML入門的常見問題(四)
