App下載

python怎么自己設(shè)置群控效果?實現(xiàn)自動化群控的步驟!

加里敦大學學生 2021-08-20 11:36:46 瀏覽數(shù) (2690)
反饋

在python大數(shù)據(jù)和人工智能之后我們又有一個叫做群控系統(tǒng)的名詞興起,那么下面我們來說說“python怎么自己設(shè)置群控效果?”這個問題的相關(guān)內(nèi)容分享和解決思路解析! 

1. 前言

群控,相信大部分人都不會陌生!印象里是一臺電腦控制多臺設(shè)備完成一系列的操作,更多的人喜歡把它和 Hui 產(chǎn)綁定在一起!

事實上,群控在自動化測試中也被廣泛使用!接下來的幾篇文章,我將帶大家聊聊企業(yè)級自動化中,群控正確的使用姿勢!

本篇先從基礎(chǔ)篇開始,聊聊使用「 Python + adb 」命令如何編寫一套群控腳本

2. 準備

在本機安裝 Android 開發(fā)環(huán)境,保證 adb 被添加到環(huán)境變量

將準備好的多臺設(shè)備,使用數(shù)據(jù)線( 或者通過 Hub )連接到電腦上

通過 adb devices 命令查看已經(jīng)連接的所有設(shè)備

# 下面顯示連接了3臺設(shè)備
xag:Test xingag$ adb devices
List of devices attached
822QEDTL225T7    device
ca2b3455        device
DE45d9323SE96   device

3. 實戰(zhàn)

自動化群控以閑魚 App 的一次關(guān)鍵字搜索為例,步驟包含:打開應(yīng)用、點擊到搜索界面、輸入內(nèi)容、點擊搜索按鈕
下面通過7步來完成這一操作

1、獲取目標應(yīng)用的包名及初始化 Activity

獲取方式有很多種,主流方式包含:adb 命令、解析 APK、第三方 APK、無障礙服務(wù)
這里推薦使用 adb 命令這種方式

# 獲取當前運行應(yīng)用的包名及初始Activity
adb shell dumpsys activity | grep -i run

打開閑魚 App,在命令終端輸入上面的命令,終端會將包名及 Activity 名稱顯示出來


2、獲取所有在線的設(shè)備

通過 adb devices 命令,通過輸出內(nèi)容,進行一次過濾,得到所有連接到 PC 端的設(shè)備

# 所有設(shè)備ID
devices = []

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("	")[0])
           devices = devices[1:]
    except Exception as e:
            print(e)

    # 連上的所有設(shè)備及數(shù)量
    return devices

3、群控打開目標應(yīng)用

遍歷設(shè)備列表,使用 adb -s 設(shè)備ID shell am start -W 命令分別打開目標應(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í)行點擊操作、在輸入框中輸入內(nèi)容、調(diào)用本地方法及輸入?yún)?shù)
這里分別對應(yīng):保存 UI 樹控件、查找輸入框元素并執(zhí)行點擊操作、保存 UI 樹控件(界面變化了)、輸入文本內(nèi)容、查看搜索按鈕元素并執(zhí)行點擊操作

# 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

需要指出的是,為了提高群控的適配性,控件的實際坐標需要通過下面的步驟去獲?。?/p>

  • 導出界面的控件樹
  • 解析控件樹 XML 文件,利用正則表達式得到目標控件的坐標值
  • 計算出控件的中心點坐標


利用控件 ID 獲取元素中心點坐標的實現(xiàn)代碼如下:

def get_element_position(element_id, uidump_name):
    """
    通過元素的id,使用ElementTree,解析元素控件樹,查找元素的坐標中心點
    :param element_id: 元素id,比如:
    :return: 元素坐標
    """

    # 解析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'])

    # 中心點坐標
    position_center = int((int(coord[0]) + int(coord[2])) / 2), int((int(coord[1]) + int(coord[3])) / 2)

    return position_center

5、區(qū)分設(shè)備

為了保證群控腳本執(zhí)行不會產(chǎn)生干擾,在每個步驟執(zhí)行之前,都應(yīng)該將設(shè)備 ID 作為參數(shù)進行區(qū)分
比如:將控件的界面控件樹按照設(shè)備保存為不同的名稱、點擊界面和輸入的命令傳相應(yīng)設(shè)備 ID 作為入?yún)?/p>

def save_ui_tree_to_local(dName):
    """
    獲取當前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è)備列表,以保證每一個步驟,分別執(zhí)行到每臺設(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')
    # 獲取元素的坐標
    bound_search_input = get_element_position(element_id, device)
    # 點擊元素
    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)用

當所有的操作完成之后,同樣是遍歷設(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 自動化群控最簡單的實現(xiàn)方式,后面將和大家討論更加復(fù)雜的實現(xiàn)方式。

項目地址:https://github.com/xingag/test_auto/tree/master/group_control

那么以上就是有關(guān)于:“python怎么自己設(shè)置群控效果?”這個問題的相關(guān)內(nèi)容分享和解決方法,更多有關(guān)于python的相關(guān)內(nèi)容我們都可以在W3Cschool中進學習和了解!

0 人點贊