通过启用多线程传入不同设备参数进行操控,代码如下:
# -*- coding: utf-8 -*-
# @file: thread_multiple_devices.py
# @author: xiaoxiao
# @date : 2019/8/6
import threading
import subprocess
import time
class ThreadMultipleDevices():
def __init__(self, package_name):
self.package_name = package_name
# 启动app应用
def app_start(self, launch_activity, device_id=''):
if device_id != '':
cmd_start = "adb -s %s shell am start -n %s" % (device_id, self.package_name + "/" + launch_activity)
else:
cmd_start = "adb shell am start -n %s" % (self.package_name + "/" + launch_activity)
subprocess.Popen(cmd_start, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
print('Running device:' + threading.current_thread().name + ', app start success:'+ str(cmd_start))
time.sleep(5)
def test_meizu16_app_start(self):
self.app_start('com.yyy', devices[0])
def test_vivo_X9_app_start(self):
self.app_start('com.yyy', devices[1])
devices = ['882QADT9UWTBW', 'e9d3a867']
thread_mulitple_devices = ThreadMultipleDevices('com.xxx')
t1 = threading.Thread(target=thread_mulitple_devices.test_meizu16_app_start, name='meizu16')
t2 = threading.Thread(target=thread_mulitple_devices.test_vivo_X9_app_start, name='vivo X9')
t1.start()
t2.start()
print('======= end =========')
执行结果:
======= end =========
Running device:meizu16, app start success:adb -s e9d3a867 shell am start -n com.xxx/com.yyy
Running device:vivo X9, app start success:adb -s 882QADT9UWTBW shell am start -n com.xxx/com.yyy