class Duplicate(WebDriver): def __init__(self, sess_id, svr_addr): super(WebDriver, self).__init__() self.command_executor = RemoteConnection(svr_addr, True) self.session_id = sess_id self.error_handler = ErrorHandler() self.file_detector = LocalFileDetector() #override def execute(self, driver_command, params=None): if self.session_id is not None: if not params: params = {'sessionId': self.session_id} elif 'sessionId' not in params: params['sessionId'] = self.session_id params = self._wrap_value(params) response = self.command_executor.execute(driver_command, params) if response: self.error_handler.check_response(response) response['value'] = self._unwrap_value(response.get('value', None)) return response # If the server doesn't send a response, assume the command was # a success return {'success': 0, 'value': None, 'sessionId': self.session_id}
class ResumableRemote(webdriver.Remote): def __init__(self, command_executor='http://127.0.0.1:4444/wd/hub', session_id=None, **kwargs): if session_id is not None: self.command_executor = command_executor try: string_type = basestring except: string_type = str if isinstance(self.command_executor, (string_type, bytes)): self.command_executor = RemoteConnection( command_executor, keep_alive=kwargs.get('keep_alive', False)) self.command_executor._commands['get_session'] = ('GET', '/session/$sessionId') self._is_remote = True self.start_client() self.resume_session(session_id) self._switch_to = SwitchTo(self) self._mobile = Mobile(self) self.error_handler = ErrorHandler() else: super(ResumableRemote, self).__init__( command_executor=command_executor, **kwargs) def resume_session(self, session_id): self.session_id = session_id response = self.command_executor.execute('get_session', {'sessionId': session_id}) self.capabilities = response['value']
# 判断是否只运行上一次失败的用例 if args.lf: if int(args.lf): pytest_execute_params.append('--lf') # 判断是否清空已有测试结果 if args.clr: if int(args.clr): pytest_execute_params.append('--clean-alluredir') pytest_execute_params.append(dir) tmp_exit_code = pytest.main(pytest_execute_params) if not tmp_exit_code == 0: exit_code = tmp_exit_code print('%s结束%s浏览器测试......' % (DateTimeTool.getNowTime(), current_browser)) print('%s清除未被关闭的浏览器......' % DateTimeTool.getNowTime()) try: conn = RemoteConnection( Read_WEB_UI_Config().web_ui_config.selenium_hub, True) sessions = conn.execute(Command.GET_ALL_SESSIONS, None) sessions = sessions['value'] for session in sessions: session_id = session['id'] conn.execute(Command.QUIT, {'sessionId': session_id}) except Exception as e: print('%s清除未关闭浏览器异常:\r\n%s' % (DateTimeTool.getNowTime(), e.args.__str__())) print('%s清除未被关闭的浏览器完成......' % DateTimeTool.getNowTime()) print('%s结束测试......' % DateTimeTool.getNowTime())