获取手机列表,每个手机执行一遍测试用例
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()