问题:同一套代码,如何同时在不同的手机上执行,测下app在不同手机上兼容性?
解决方法:在一个脚本中同时启动多个Appium服务,让多个设备同时执行。
准备多个设备的capabilities参数
desired_caps = {
‘Nokia‘: {
‘platformName‘: ‘Android‘,
‘appPackage‘: ‘com.XXXX.XXXX‘,
‘appActivity‘: ‘.MainActivity‘,
‘noReset‘: True,
‘deviceName‘: ‘Nokia 7‘,
‘resetKeyboard‘: True,
},
‘Honor‘: {
‘platformName‘: ‘Android‘,
‘appPackage‘: ‘com.XXXX.XXXX‘,
‘appActivity‘: ‘.MainActivity‘,
‘noReset‘: True,
‘deviceName‘: ‘Honor 10‘,
‘resetKeyboard‘: True
},
}
设定Appium服务端口和Bootstrap端口及设备的对应关系
device_info = {
‘Nokia‘: {
‘port‘: ‘4723‘,
‘bpport‘: ‘4750‘,
‘uuid‘: ‘xxxxxxxx‘
},
‘Honor‘: {
‘port‘: ‘4725‘,
‘bpport‘: ‘4752‘,
‘uuid‘: ‘xxxxxxxx2‘
}
}
Mac环境下根据端口杀死进程
需确保appium服务的端口没被占用
def check_port(port):
a = os.popen("lsof -i tcp:%s"%port)
t = a.read()
print(t)
if port and ‘LISTEN‘ in t:
b = os.popen("lsof -i tcp:%s |grep -v COMMAND | awk ‘{print $2}‘"%port)
time.sleep(2)
pid = b.read()
os.kill(int(pid),signal.SIGKILL)
print("Kill pid:%s"%pid)
else:
print(‘Port %s is available!‘ % (port))
启动Appium服务
import subprocess
def appium_start(port,bpport,device):
‘‘‘appium驱动方法‘‘‘
host = ‘127.0.0.1‘
check_port(port)
time.sleep(3)
cmd = ‘nohup appium -a ‘ + host + ‘ -p ‘ + str(port) + ‘ -bp ‘ + str(bpport) + ‘ -U‘ + str(device) + ‘ &‘
print("%s at %s" % (cmd, ctime()))
‘‘‘保存Appium日志‘‘‘
subprocess.Popen(cmd, shell=True, stdout=open(‘./appium_log/‘ + str(port) + ‘.log‘, ‘a‘), stderr=subprocess.STDOUT)
测试用例脚本
def run_app(device, desired):
port = str(device[‘port‘])
driver: webdriver = webdriver.Remote(‘http://localhost:%s/wd/hub‘ % port, desired)
time.sleep(10)
for i in range(10):
driver.find_element_by_xpath(‘//android.widget.TextView[@text="XXXX"]‘).click()
time.sleep(3)
driver.find_element_by_xpath(‘//android.widget.TextView[@text="XXXX"]‘).click()
time.sleep(3)
多设备执行
import multiprocessing
device_list = [‘Nokia‘, ‘Honor‘]
appium_starts = []
for i in device_list:
appium = multiprocessing.Process(target=appium_start, args=(device_info[i][‘port‘],device_info[i][‘bpport‘],device_info[i][‘uuid‘]))
appium_starts.append(appium)
desired_process = []
for i in device_list:
desired = multiprocessing.Process(target=run_app, args=(device_info[i], desired_caps[i]))
desired_process.append(desired)
if __name__ == ‘__main__‘:
for appium in appium_starts:
appium.start()
for appium in appium_starts:
# 执行进程
appium.join()
time.sleep(5)
for i in desired_process:
i.start()