def __init__(self, is_need_reset_app=False): if self.__inited is None: self.config = Read_APP_UI_Config().app_ui_config self.device_info = FileTool.readJsonFromFile('config/app_ui_tmp/' + str(os.getppid())) self.demoProject_config = APP_UI_Android_DemoProject_Read_Config( 'config/demoProject/%s' % self.device_info['app_ui_config']).config self.current_desired_capabilities = FileTool.readJsonFromFile( 'config/app_ui_tmp/' + str(os.getppid()) + '_current_desired_capabilities') self._appium_hub = 'http://' + self.device_info[ 'server_ip'] + ':%s/wd/hub' % self.device_info['server_port'] self._init(self.demoProject_config.init) self._delete_last_device_session(self.device_info['device_desc']) self.driver = webdriver.Remote( self._appium_hub, desired_capabilities=self.current_desired_capabilities) self._save_last_device_session(self.driver.session_id, self.device_info['device_desc']) self.appOperator = AppOperator(self.driver, self._appium_hub) self.__inited = True if is_need_reset_app: self.appOperator.reset_app()
def java_maven_init(): print('%s开始java maven更新......'%DateTimeTool.getNowTime()) print('%s删除旧的maven依赖包......'%DateTimeTool.getNowTime()) FileTool.truncateDir('common/java/lib/java/libs') print('%s删除旧的maven依赖包完成......'%DateTimeTool.getNowTime()) maven_update_command = 'mvn -U -f "' + os.path.join(os.getcwd(),'config/java/pom.xml"') + ' dependency:copy-dependencies -DoutputDirectory="' + os.path.join(os.getcwd(), 'common/java/lib/java/libs"') output = subprocess.check_output(maven_update_command, shell=True, timeout=3600) if 'Windows' == platform.system(): print(output.decode('cp936')) else: print(output.decode('utf-8')) print('%s完成java maven更新......'%DateTimeTool.getNowTime())
def java_maven_init(): print('开始java maven更新......') print('删除旧的maven依赖包......') FileTool.truncateDir('common/java/lib/java/libs') print('删除旧的maven依赖包完成......') maven_update_command = 'mvn -U -f ' + os.path.join( os.getcwd(), 'config/java/pom.xml' ) + ' dependency:copy-dependencies -DoutputDirectory=' + os.path.join( os.getcwd(), 'common/java/lib/java/libs') try: output = subprocess.check_output(maven_update_command, shell=True, timeout=3600) print(output.decode('utf-8')) except: sys.exit('java maven更新失败......') print('完成java maven更新......')
def start_app_device_test(index,device_info,keyword,dir,markexpr,capture,reruns,lf): for path, dirs, files in os.walk('config/app_ui_tmp'): for file in files: if(int(file)==index): os.rename(os.path.join(path,file),os.path.join(path,str(os.getpid()))) print('开始检测appium server是否可用......') try: doRquest = DoRequest('http://'+device_info['server_ip']+':%s/wd/hub'%device_info['server_port'].strip()) httpResponseResult = doRquest.get('/status') result = ujson.loads(httpResponseResult.body) if result['status'] == 0: print('appium server状态为可用......') else: sys.exit('appium server状态为不可用') except: sys.exit('appium server状态为不可用') a_devices_desired_capabilities = device_info['capabilities'] print('开始设备' + device_info['device_desc'] + '测试......') for desired_capabilities in a_devices_desired_capabilities: FileTool.writeObjectIntoFile(desired_capabilities, 'config/app_ui_tmp/' + str(os.getpid()) + '_current_desired_capabilities') if desired_capabilities['appPackage']: desired_capabilities_desc = desired_capabilities['appPackage'] else: desired_capabilities_desc = os.path.basename(desired_capabilities['app']) print('当前设备开始测试的desired_capabilities为:%s' % desired_capabilities) # 执行pytest前的参数准备 pytest_execute_params = ['-c', 'config/pytest.ini', '-v', '--alluredir', 'output/app_ui/' + device_info['device_desc'] + '/' + desired_capabilities_desc] # 判断目录参数 if not dir: dir = 'cases/app_ui/' # 判断关键字参数 if keyword: pytest_execute_params.append('-k') pytest_execute_params.append(keyword) # 判断markexpr参数 if markexpr: pytest_execute_params.append('-m') pytest_execute_params.append(markexpr) # 判断是否输出日志 if capture: if int(capture): pytest_execute_params.append('-s') # 判断是否失败重跑 if reruns: if int(reruns): pytest_execute_params.append('--reruns') pytest_execute_params.append(reruns) # 判断是否只运行上一次失败的用例 if lf: if int(lf): pytest_execute_params.append('--lf') pytest_execute_params.append(dir) # 构建孙进程 process = multiprocessing.Process(target=pytest_main, args=(pytest_execute_params,)) process.start() process.join() print('当前设备结束测试的desired_capabilities为:%s' % desired_capabilities) print('结束设备' + device_info['device_desc'] + '测试......')
keyword=args.keyword dir=args.dir markexpr=args.markexpr capture=args.capture reruns=args.reruns lf=args.lf test_type=args.test_type.lower() devices_info_file=args.devices_info_file if test_type=='phone': if not devices_info_file: sys.exit('请指定多设备并行信息文件,查看帮助:python run_app_ui_test.py --help') # 初始化进程池 p_pool = Custom_Pool(int(Read_APP_UI_Config().app_ui_config.max_device_pool)) devices_info=Read_APP_UI_Devices_Info(devices_info_file).devices_info if os.path.exists('config/app_ui_tmp'): FileTool.truncateDir('config/app_ui_tmp/') else: os.mkdir('config/app_ui_tmp') for i in range(len(devices_info)): device_info=devices_info[i] FileTool.writeObjectIntoFile(device_info,'config/app_ui_tmp/'+str(i)) p=p_pool.apply_async(start_app_device_test,(i,device_info,keyword,dir,markexpr,capture,reruns,lf)) p_pool.close() p_pool.join() else: # 执行pytest前的参数准备 pytest_execute_params = ['-c', 'config/pytest.ini', '-v', '--alluredir', 'output/app_ui/windows'] # 判断目录参数 if not dir: dir = 'cases/app_ui/' # 判断关键字参数
# 初始化java依赖的libs java_maven_init() print('%s初始化基础数据......' % DateTimeTool.getNowTime()) web_ui_init() print('%s初始化基础数据完成......' % DateTimeTool.getNowTime()) print('%s初始化完成......' % DateTimeTool.getNowTime()) print('%s开始测试......' % DateTimeTool.getNowTime()) exit_code = 0 for current_browser in Read_WEB_UI_Config().web_ui_config.test_browsers: print('%s开始%s浏览器测试......' % (DateTimeTool.getNowTime(), current_browser)) # 由于pytest的并发插件xdist采用子进程形式,当前主进程的单例在子进程中会重新创建,所以将每次要测试的浏览器信息写入到文件中, # 保证子进程能够正确读取当前要测试的浏览器 FileTool.replaceFileContent('config/web_ui_config.conf', '\r\n', '\n') FileTool.replaceFileContentWithLBRB('config/web_ui_config.conf', '=' + current_browser, 'current_browser', '\n') # 执行pytest前的参数准备 pytest_execute_params = [ '-c', 'config/pytest.ini', '-v', '--alluredir', 'output/web_ui/' + current_browser + '/', '-n', Read_WEB_UI_Config().web_ui_config.test_workers, '--dist', 'loadfile' ] # 判断目录参数 dir = 'cases/web_ui/' if args.dir: dir = args.dir # 判断关键字参数
def start_app_device_test(index,device_info,keyword,dir,markexpr,capture,reruns,lf,clr): for path, dirs, files in os.walk('config/app_ui_tmp'): for file in files: if(int(file)==index): os.rename(os.path.join(path,file),os.path.join(path,str(os.getpid()))) print('%s开始检测appium server是否可用......'%DateTimeTool.getNowTime()) try: doRquest = DoRequest('http://'+device_info['server_ip']+':%s/wd/hub'%device_info['server_port'].strip()) httpResponseResult = doRquest.get('/status') result = ujson.loads(httpResponseResult.body) if result['status'] == 0: print('%sappium server状态为可用......'%DateTimeTool.getNowTime()) else: sys.exit('%sappium server状态为不可用'%DateTimeTool.getNowTime()) except: print('%sappium server状态为不可用'%DateTimeTool.getNowTime()) raise Exception('%sappium server状态为不可用'%DateTimeTool.getNowTime()) a_devices_desired_capabilities = device_info['capabilities'] print('%s开始设备%s测试......'%(DateTimeTool.getNowTime(),device_info['device_desc'])) print('%s当前设备所需测试的desired_capabilities为:%s'%(DateTimeTool.getNowTime(),a_devices_desired_capabilities)) for desired_capabilities in a_devices_desired_capabilities: FileTool.writeObjectIntoFile(desired_capabilities,'config/app_ui_tmp/'+str(os.getpid())+'_current_desired_capabilities') desired_capabilities_desc=None if 'appPackage' in desired_capabilities.keys(): desired_capabilities_desc = desired_capabilities['appPackage'] elif 'app' in desired_capabilities.keys(): desired_capabilities_desc = desired_capabilities['app'].split('/')[-1] elif 'bundleId' in desired_capabilities.keys(): desired_capabilities_desc = desired_capabilities['bundleId'] print('%s当前设备开始测试的desired_capabilities为:%s'%(DateTimeTool.getNowTime(),desired_capabilities)) # 执行pytest前的参数准备 pytest_execute_params = ['-c', 'config/pytest.ini', '-v', '--alluredir', 'output/app_ui/%s/%s/report_data/'%(device_info['device_desc'],desired_capabilities_desc)] # 判断目录参数 if not dir: dir = 'cases/app_ui/' # 判断关键字参数 if keyword: pytest_execute_params.append('-k') pytest_execute_params.append(keyword) # 判断markexpr参数 if markexpr: pytest_execute_params.append('-m') pytest_execute_params.append(markexpr) # 判断是否输出日志 if capture: if int(capture): pytest_execute_params.append('-s') # 判断是否失败重跑 if reruns: if int(reruns): pytest_execute_params.append('--reruns') pytest_execute_params.append(reruns) # 判断是否只运行上一次失败的用例 if lf: if int(lf): pytest_execute_params.append('--lf') # 判断是否清空已有测试结果 if clr: if int(clr): pytest_execute_params.append('--clean-alluredir') pytest_execute_params.append(dir) # 构建孙进程 process = multiprocessing.Process(target=pytest_main,args=(pytest_execute_params,)) process.start() process.join() print('%s当前设备结束测试的desired_capabilities为:%s' % (DateTimeTool.getNowTime(),desired_capabilities)) print('%s结束设备%s测试......'%(DateTimeTool.getNowTime(),device_info['device_desc']))