基于monkey的兼容性测试:多个不同的app在同一个设备上的兼容性,一般只有在手机厂商的公司会有这个测试场景
app多机兼容性测试:一个app在多个不同的设备上的兼容性,一般所有的app公司都会有这种兼容性测试需求
关键点:如何同时启动多台设备并且同时在多台设备上进行测试
流程:
1.需要将N台设备连接到adb(手动)
2.通过命令行启动appium服务(一个appium服务只能操纵一台手机,所以有多少台手机就必须启动多少个appium服务)
3.通过appium来启动手机上的被测应用。通过yaml文件来进行appium配置
4.并行完成测试
5.重新打开一个命令行,输入appium,如果出现下面的提示则表示配置成功
app_caps.yaml
platformName: Android
platformVersion: 5.1.1
deviceName: 127.0.0.1:62001
noRest: False
unicodeKeyboard: True
resetKeyboard: True
appPackage: com.chaoxing.mobile
appActivity: .main.ui.MainTabActivity
ip: 127.0.0.1
deviceList: '127.0.0.1:62001,127.0.0.1:62025'
muti_appium.py
#通过命令行来启动appium服务
import subprocess
from time import ctime
import threading
def appium_start(host,port):
bootstrap_port=str(port+1)
cmd='start /b appium -a ' + host +' -p '+ str(port) + ' -bp ' + bootstrap_port
print('{} at {}'.format(cmd, ctime()))
subprocess.Popen(cmd,shell=True,stdout=open('appium_log/'+str(port)+'.log','a',encoding='utf8'),stderr=subprocess.STDOUT)
if __name__=='__main__':
# appium_start('127.0.0.1', 4723)
# appium_start('127.0.0.1', 4725)
for i in range(2):
port=4723 + 2*i
threading.Thread(target=appium_start,args=('127.0.0.1',port)).start()
muti_device.py
import threading
from test_xxt import XXTTest
from appium import webdriver
from util import get_yaml_data,get_device_list
from time import ctime
def appium_desired(udid,port):
#配置appium启动app的参数信息
data=get_yaml_data('app_caps.yaml')
desired_caps={}
desired_caps['platformName']=data['platformName']
desired_caps['platformVersion']=data['platformVersion']
desired_caps['deviceName']=data['deviceName']
#在指定多机连接时,需要单独指定一个udid字段
desired_caps['udid']=udid
desired_caps['appPackage']=data['appPackage']
desired_caps['appActivity']=data['appActivity']
desired_caps['noRest']=data['noRest']
desired_caps['unicodeKeyboard']=data['unicodeKeyboard']
desired_caps['resetKeyboard']=data['resetKeyboard']
print('appium port : {} start run {} at {}'.format(port,udid,ctime()))
driver=webdriver.Remote("http://"+str(data['ip'])+':'+str(port)+'wd/hub',desired_caps)
driver.implicitly_wait(10)
xxt=XXTTest(driver)
xxt.login()
if __name__=='__main__':
device_list=get_device_list(get_yaml_data('app_caps.yaml')['deviceList'])
# print(device_list)
device_threads=[]
for i in range(len(device_list)):
port=4723+2*i
t=threading.Thread(target=appium_desired,args=(device_list[i],port))
device_threads.append(t)
for i in device_threads:
i.start()
for i in device_threads:
i.join()
util.py
import os,yaml
def check_port(port):
cmd='netstat -ano | findstr ' + str(port)
resp=os.popen(cmd).read()
if len(resp) and 'LISTENING' in resp: #端口被占用
print('端口{}被占用,即将清理端口'.format(port))
resp_list=resp.split('\n')
pid=resp_list[0].split()[-1]
print('进程号为{}'.format(pid))
cmd='taskkill -f -pid '+ pid
resp=os.popen(cmd).read()
if '成功' in resp:
print("端口{}已被清理成功".format(port))
return True
else:
print("端口{}可用".format(port))
return True
print("端口{}处于被占用状态,请检查".format(port))
return False
def get_yaml_data(path):
with open(path,'r',encoding='utf-8') as f:
return yaml.load(f,Loader=yaml.FullLoader)
def get_device_list(str_device):
if ',' in str_device:
devices=str_device.split(',')
return devices
else:
return [str_device]
if __name__=='__main__':
# check_port(4725)
data=get_yaml_data('app_caps.yaml')
print(data)
appium_device_sync.py
import threading
from time import sleep
from util import get_yaml_data,get_device_list,check_port
from muti_appium import appium_start
from muti_device import appium_desired
data=get_yaml_data('app_caps.yaml')
device_list=get_device_list(data['deviceList'])
def start_appium_sync():
print("=======并发启动appium========")
appium_threads=[]
for i in range(len(device_list)):
host='127.0.0.1'
port=4723+2*i
if check_port(port):
appium=threading.Thread(target=appium_start,args=('127.0.0.1',port))
appium_threads.append(appium)
else:
print("端口{}的appium启动失败,请检查".format(port))
return False
for i in appium_threads:
i.start()
for i in appium_threads:
i.join()
sleep(5)
return True
def start_device_sync():
print("======并发启动设备上的应用======")
device_threads=[]
for i in range(len(device_list)):
port=4723+2*i
device=threading.Thread(target=appium_desired,args=(device_list[i],port))
device_threads.append(device)
for i in device_threads:
i.start()
for i in device_threads:
i.join()
if __name__=='__main__':
start_appium_sync()
start_device_sync()
test_xxt.py
from time import sleep
class XXTTest:
def __init__(self,driver):
self.driver=driver
def login(self):
self.driver.find_element('id','com.chaoxing.mobile:id/et_account').send_keys('13277777777')
self.driver.find_element('id','com.chaoxing.mobile:id/et_password').send_keys('123456')
self.driver.find_element('id','com.chaoxing.mobile:id/btn_login').click()
sleep(5)