python3+uiautomator2多设备执行测试用例(遍历设备,非同时执行)

获取手机列表,每个手机执行一遍测试用例


import subprocess
import time
import os
import uiautomator2  as u2
now = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time()))

class Test():
    def MultiDevice(self,d):

        d.screen_on()
        #print(d.info)
        d.screen_off()

    def getphonelist(self):
        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")
        devices = []
        for i in (out)[1:-1]:
            device = str(i).split("\\")[0].split("'")[-1]
            devices.append(device)
        return devices

    def test_xxx(self):

        for i in Test.getphonelist():
            d = u2.connect(i)  # d = u2.connect('192.168.1.117')
            Test.MultiDevice(d)



if __name__ == '__main__':
    Test=Test()
    Test.test_xxx()

unittest单元测试框架下的

import unittest
import sys
import uiautomator2  as u2
import HTMLTestRunner    #python3
from time import sleep
import os
import subprocess

class Test(unittest.TestCase):


    def MultiDevice(self,d):#功能执行

        d.screen_on()
        #print(d.info)
        d.screen_off()

    def getphonelist(self):#获取手机设备
        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")
        devices = []
        for i in (out)[1:-1]:
            device = str(i).split("\\")[0].split("'")[-1]
            devices.append(device)
        return devices  #手机设备列表

    def test_xxx(self):

        for i in Test.getphonelist(self):
            d = u2.connect(i)  # d = u2.connect('192.168.1.117')#  uiautomator2 连接手机

            Test.MultiDevice(self,d)



if __name__ == '__main__':
    sv_report = 'report'#测试报告保存文件夹
    suite = unittest.TestSuite()
    suite.addTest(Test('test_xxx'))


    now=time.strftime("%Y-%m-%d_%H-%M-%S",time.localtime())
    filename = sv_report + '/' + now + "_ReportCase.html"
    if sv_report  not in os.listdir(os.getcwd()):  # 文件夹名称不存在才创建
        os.mkdir(os.getcwd() + '/' + sv_report + '/')
        fp = open(filename, 'wb')
    else:
        fp = open(filename, 'wb')
    print('run version {}'.format(sys.version))
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title=u"自动化测试报告",description=u"TestCase Report")
    runner.run(suite)
    fp.close()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值