1.配置多设备参数:

import multiprocessing
class A(unittest.TestCase):

def android_devices(self, udid, port,systemPort):
capabilities = {
"platformName": "Android",
"udid": udid,
"deviceName": "WCGEL7U899999999",
"appPackage": 'com.xxxxxx.activities',
"systemPort": systemPort,
"appActivity": 'com.xxxxxx.activities.activity.init.InitActivity',
"noReset": True
}
driver = webdriver.Remote("http://127.0.0.1:"+str(port)+"/wd/hub", capabilities)
备注:一定要加udid和systemPort这两个参数,要不然就算启动了两个设备也只有一台设备在跑测试用例。

2.确保当前端口号没有被占用:
def kill_server(self):
server_list = os.popen('tasklist | find "node.exe"').readlines()
if len(server_list) > 0:
os.system("taskkill -F -PID node.exe")

3.同时启动多个Appium服务:
def start_appium_server(self, port,devices):
bootstrip = port + 1
os.system("appium -a 127.0.0.1" + " -p " + str(port) + " -bp " + str(bootstrip) + " -U "+str(devices)+" --no-reset")

备注:bootstrip是Appium Server的中间件,bootstrip的端口号是Appium Server的端口号+1


4.导入多进程multiprocessing模块,通过Process方法同时启动多台服务并启动设备:
if __name__ == '__main__':
desired_list = []
devices_list = ["127.0.0.1:62001", "127.0.0.1:62025"]
a= A()
a.kill_server()

for j in range(len(devices_list)):
server_port = 4723 + j * 2
th = multiprocessing.Process(target=a.start_appium_server, args=(server_port, devices_list[j]))
th.start()

for i in range(len(devices_list)):
port = 4723 + i * 2
systemPort = 8200 + i
t = multiprocessing.Process(target=a.android_devices, args=(devices_list[i], port, systemPort))
t.start()


12-22 03:38