uiautomator2 扫描头测试代码

import sys
import uiautomator2  as u2
from time import sleep
import os
import subprocess
import threading
import time

def func():
    print('代码第{}行, 函数名称 {}'.format(sys._getframe().f_lineno, sys._getframe().f_code.co_name))
    print('调用该函数的上级为{}'.format(sys._getframe(1).f_code.co_name))


def MultiDevice( d):  # 功能执行
        i = 0
        while True:
            #d.screen_on()
            try:

                # 获取正在运行的app,并传给currentApp变量
                currentApp = d.app_current()
                print(currentApp['package'])
                # 待测试的app包名
                pkg_name = 'test.apidemo.activity'

                if pkg_name  not in currentApp['package']:
                    print("start app ",pkg_name)
                    d.app_start(pkg_name)

                if d(resourceId="test.apidemo.activity:id/btnStartScan").exists:
                    d(resourceId="test.apidemo.activity:id/btnStartScan").click(offset=(0.01,0.01))
                    i = i + 1
                    print("机器:",threading.currentThread().ident,"测试第 ",i,"次 !")
                #time.sleep(1)
                if d(resourceId="test.apidemo.activity:id/btnDisableScan").exists:
                    d(resourceId="test.apidemo.activity:id/btnDisableScan").click(2)
                time.sleep(1)
                if d(resourceId="test.apidemo.activity:id/btnEnableScan").exists:
                    d(resourceId="test.apidemo.activity:id/btnEnableScan").click(2)
                time.sleep(1)
                #time.sleep(1)
            except Exception as e:
                print(e)




    #d.screen_off()

def getphonelist():  # 获取手机设备
    cmd = r'adb devices'  # % apk_file
    pr = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    pr.wait()  # 不会马上返回输出的命令,需要等待
    out = pr.stdout.readlines()  # out = pr.stdout.read().decode("UTF-8")
    #print(out)
    devices = []
    for i in (out)[1:-1]:
        #print(i)
        device = str(i).split("\\")[0].split("'")[-1]
        devices.append(device)
    #print(devices)
    return devices  # 手机设备列表



def test_xxx(i):
    while True:
        try:
            print(i)
            print(threading.currentThread().ident)
            d = u2.connect(i)  # d = u2.connect('192.168.1.117')#  uiautomator2 连接手机
            d.info
            time.sleep(3)
            MultiDevice(d)
        except Exception as e:
            print(e)


def start():
        threads = []
        for i in range(len(getphonelist())):
            #print(i)
            t = threading.Thread(target=test_xxx,args=((getphonelist()[i]),))
            #print(i, " end")
            threads.append(t)

        print("start to threads!")
        for t in threads:
            time.sleep(0.3)
            t.start()
        for t in threads:
            t.join()
if __name__ == '__main__':
    start()

你可能感兴趣的:(python小程序,python,android,开发语言)