uiautomator2 扫描头测试代码

本文介绍了一个使用Python和UIAutomator2实现的多设备自动化测试框架。该框架能够同时在多个Android设备上启动指定的应用程序并执行一系列交互操作,如点击按钮等。通过多线程方式实现了并发测试,提高了测试效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值