def on_call(*args, **kwargs): _cls_name = args[0].__class__.__name__ _met_name = func.__name__ _element_name = args[0].name try: result = func(*args, **kwargs) if result is not None: logger.debug( '[Call]: {0} >> {1} >> {2} [Return]: {3}'.format( _cls_name, _met_name, _element_name, result)) else: logger.debug('[Call]: {0} >> {1} >> {2}'.format( _cls_name, _met_name, _element_name)) return result except NoSuchElementException: logger.error( '[NoSuchElementException]: Fail to locate element {0}'. format(_element_name)) except WebDriverException as e: exc_type, _, _ = sys.exc_info() logger.error('[{0}]: {1}'.format(exc_type.__name__, e).rstrip()) raise except Exception: logger.exception('[UnwantedException]:') raise
def send_specific_keys(self, *args): keys_set = set() redundant_keys = [ 'BACK_SPACE', 'LEFT_SHIFT', 'LEFT_CONTROL', 'LEFT_ALT', 'ARROW_LEFT', 'ARROW_UP', 'ARROW_RIGHT', 'ARROW_DOWN' ] try: keys_mapping = dict( filter(lambda d: d[0] not in redundant_keys, Keys.__dict__.items())) for arg in args: pattern = re.compile(arg, re.IGNORECASE) for attr in keys_mapping: result = re.search(pattern, attr) if result: keys_set.add(attr) if keys_set: self.driver.send_keys(*keys_set) logger.debug( '[Call]: KeyActions >> send_specific_keys >> {0}'.format( ' + '.join(keys_set))) else: self.driver.send_keys('') logger.warning( '[Call]: KeyActions >> send_specific_keys >> empty content' ) except WebDriverException as e: exc_type, _, _ = sys.exc_info() logger.error('[{0}]: {1}'.format(exc_type.__name__, e).rstrip()) raise except Exception: logger.exception('[UnwantedException]:') raise
def on_call(*args, **kwargs): _cls_name = args[0].__class__.__name__ _met_name = func.__name__ try: result = func(*args, **kwargs) _result = True if result else False logger.debug('[Call]: {0} >> {1} [Return]: {2}'.format( _cls_name, _met_name, _result)) return result except TimeoutException as e: logger.warning('[TimeoutException]: {0}'.format(e).rstrip()) except WebDriverException as e: exc_type, _, _ = sys.exc_info() logger.error('[{0}]: {1}'.format(exc_type.__name__, e).rstrip()) raise except Exception: logger.exception('[UnwantedException]:') raise
def launch_local_browser(self, local_browser_name=BROWSER_NAME, window_size=BROWSER_WINDOW_SIZE, implicity_wait_timeout=IMPLICITY_WAIT_TIME): """启动本地浏览器""" try: # 初始化浏览器 if BROWSER_NAME in ["Chrome", "chrome", "CHORME"]: logger.info("Launch {0} browser".format(local_browser_name)) self.driver = webdriver.Chrome(CHROME_DRIVER_PATH) elif BROWSER_NAME in ["Firefox", "firefox", "FIREFOX", "FireFox"]: logger.info("Launch {0} browser".format(local_browser_name)) self.driver = webdriver.Firefox() elif BROWSER_NAME in ["Ie", "ie", "IE"]: logger.info("Launch {0} browser".format(local_browser_name)) self.driver = webdriver.Ie(IE_DRIVER_PATH) else: raise NameError # 设定浏览器尺寸 if window_size in ["Max", "max", "MAX"]: logger.info("Maximize browser") self.driver.maximize_window() elif window_size in ["Min", "min", "MIN"]: logger.info("Minimize browser") self.driver.minimize_window() # 设定隐式等待时间 logger.info("Set implicity wait time to {0}".format( str(implicity_wait_timeout))) self.driver.implicitly_wait(implicity_wait_timeout) return self.driver except NameError: logger.error( "Fail to launch browser due to incorrect browser name: {0}". format(BROWSER_NAME)) raise except WebDriverException as e: logger.error("Fail to launch browser: {0}".format(e)) raise e except Exception: logger.exception("Fail to launch browser", exc_info=True) raise
def launch_remote_browser(self, _command_executor, _desired_capabilities, implicity_wait_timeout=IMPLICITY_WAIT_TIME): """ 启动远程浏览器 :param _command_executor: 远程server地址,如 "http://192.168.98.106:5555/wd/hub" :param _desired_capabilities: 调用webdriver的DesiredCapabilities的模板 :param implicity_wait_timeout: 隐式等待时间 :return: self.driver """ logger.info("Launch remote browser") try: self.driver = webdriver.Remote(command_executor=_command_executor, desired_capabilities=_desired_capabilities) logger.info("Maximize browser") self.driver.maximize_window() logger.info("Set implicity wait time to {0}".format(str(implicity_wait_timeout))) self.driver.implicitly_wait(implicity_wait_timeout) return self.driver except WebDriverException as e: logger.error("Fail to launch browser: {0}".format(str(e))) raise e except Exception: logger.exception("Fail to launch browser", exc_info=True) raise