该代码还存在优化的地方,后续优化方向:结合unittest、ddt、内反射搭建自动化测试框架
实现代码如下:
#通过appium实现云测 import os import threading import time from appium import webdriver from xctest_app.xc_tools.get_aapt import * from xctest_app.xc_tools.get_DevicesInfo import * class appiumtest: def get_start(self,udid,port,bport,version,app_path,package,activity): logfile='./xc_logs/'+str(udid).replace(':','_')+'.log' #启动appium服务 cmd=f'start /b appium -a 127.0.0.1 -p {port} -bp {bport} --log-timestamp --log {logfile}' os.system(cmd) time.sleep(10) di={} di['platformName']='Android' di['platformVersion']=version di['deviceName']='Appium' di['app']=app_path di['unicodeKeyboard'] = True di['resetKeyboard'] = True di['appPackage']=package di['appActivity']=activity di['udid']=udid #android6上需要指明 # di['automationName']='uiautomator2' driver = webdriver.Remote(f'http://127.0.0.1:{port}/wd/hub',di) time.sleep(5) driver.implicitly_wait(20) try: driver.find_element_by_id('com.ss.android.article.news:id/a5a').click() time.sleep(5) driver.find_element_by_xpath("//android.widget.TextView[@text='未登录']").click() driver.find_element_by_xpath("//android.widget.TextView[@resource-id='com.ss.android.article.news:id/ca1']").click() r=driver.find_element_by_xpath("//android.widget.TextView[@resource-id='com.ss.android.article.news:id/s1']").text if r=='账号注册': print('测试成功') else: print('测试失败') except: print('脚本异常') #截图 screen_name=time.strftime('%Y%m%d_%H%M%S.png') driver.get_screenshot_as_file(f'./xc_report/{screen_name}') #卸载 time.sleep(5) os.system(f'adb -s {udid} uninstall {package}') if __name__ == '__main__': os.system('taskkill /F /IM node.exe') app_path='./xc_apk/wangyiyunyinyue.apk' a=appiumtest() # 调用封装的方法获取app的主包名和activityname aapt=packagename_activity().get_packagename(app_path) # 调用封装的方法获取udid、版本号、端口信息 list=DevicesInfo().get_devices() for i in list: device_info=i.split('#') udid=device_info[0] version=device_info[1] port=device_info[2] bport=device_info[3] t=threading.Thread(target=appiumtest().get_start,args=(udid,port,bport,version,app_path,aapt[0],aapt[1])) t.start()