def alert_message(driver=None): """ Return the message displayed in the alert. :param driver: a selenium webdriver :raise AssertionError: driver isn't of the expected type :raise NoAlertPresentException: when trying to intercept an alert which not present :return: 0 if success """ try: assert driver is not None and isinstance( driver, web_drivers_tuple()), "Driver is expected." alert_object = driver.switch_to.alert return alert_object.text except AssertionError as assertion: logging.error( "alerts.alert_message raised an assertion with following input" " driver:'{}'. Assertion is '{}'".format(driver, assertion.args)) raise except NoAlertPresentException as no_alert: logging.error( "alerts.intercept_alert can't interact with an alert as there" " is no displayed alert.\nGet {}".format(no_alert.args)) raise NoAlertPresentException( "Can't interact with an alert as there is no" " displayed alert") from None
def accept_alert(self): alert = self.is_alert_switch_to_it if alert: return alert.accept() else: self.logger.error("获取alert文本失败") raise NoAlertPresentException("没有alert抛出")
def get_alert_text(self) -> str: """ get test of alert :return: text of alert """ alert = self.is_alert_switch_to_it if alert: return alert.text else: self.logger.error("获取alert文本失败") raise NoAlertPresentException("没有alert抛出")
def alert(self): timeout = time.time() + self._timeout while time.time() < timeout: try: alert = self._wrapped.switch_to_alert() alert.text return alert except NoAlertPresentException: time.sleep(0.2) msg = "Wait for alert to be displayed for 2 seconds, but it was not displayed." raise NoAlertPresentException(msg)
def wait_for_alert(self): for i in range(self.timeout_seconds): try: alert = self.driver.switch_to_alert() if alert.text: break except NoAlertPresentException as nape: pass self.sleep() else: raise NoAlertPresentException(msg='Wait for alert timed out') return True
def wait_for_alert(self, timeout_tries=80, sleep_interval=.25): for i in range(timeout_tries): try: alert = self.driver.switch_to_alert() if alert.text: break except NoAlertPresentException as nape: pass self.sleep(sleep_interval) else: raise NoAlertPresentException(msg='Wait for alert timed out') return True
def Accept_alert(self): for i in range (self.timeout_seconds): log.debug("before_try_") try: log.debug("Inside try wait alert") jalert = self.driver.switch_to.alert log.info("switched to alert") jalert.accept() log.info("alert accepted") return except NoAlertPresentException as nape: log.warning("alert not seen , sleeping till alert is shown") time.sleep(.5) else: log.debug("inside else") raise NoAlertPresentException () log.exception("no alert observed , returing") return True
def alert_action(self, alert_action_state, text=None): try: alert = Alert(self.context.driver) if alert_action_state == AlertAction.ACCEPT: alert.accept() self.context.logger.info("Alert accept performed") return True elif alert_action_state == AlertAction.DISMISS: alert.dismiss() return True elif alert_action_state == AlertAction.SETTEXT: print(text) alert.send_keys(text) return True elif alert_action_state == AlertAction.GETTEXT: return alert.text return False except NoAlertPresentException as e: raise NoAlertPresentException(e)
def alert_message(driver=None): """ Return the message displayed in the alert. :param driver: a selenium webdriver :raise AssertionError: driver isn't of the expected type :raise NoAlertPresentException: when trying to intercept an alert which not present :return: 0 if success """ try: driver_field_validation(driver, {"type": "id", "value": "fake"}, log) alert_object = driver.switch_to.alert return alert_object.text except NoAlertPresentException as no_alert: log.error( f"alerts.intercept_alert can't interact with an alert as there" f" is no displayed alert.\nGet {no_alert.args}") raise NoAlertPresentException( "Can't interact with an alert as there is no" " displayed alert") from None
def get_alert(self, timeout=DEFAULT_TIMEOUT, message='Getting alert with a timeout of {} second(s).'): """ Switch focus to the alert and return it. get_alert() will place the timeout in seconds into the ``message`` if an extra set of curly brackets {} are placed. :param timeout: int - time in seconds for explicit wait :param message: str - error message to return in case of failure :return: alert selenium.webdriver.common.alert import Alert """ self.log.info(message.format(timeout)) try: alert = WebDriverWait(self.driver, timeout).until(alert_is_present()) return alert except TimeoutException as error: error.msg = (f'Failed to find the alert before the timeout ' f' [{timeout} second(s)].') raise NoAlertPresentException('Failed to find an alert') from error
def intercept_alert(driver=None, messages=None, accept=True, value=None): """ intercept_alert goal is to provide an easy interface to handle alerts. Only the web driver is mandatory. Giving only the web driver accept the alert whatever the message is. Giving a list of messages, ensure that the alert message is one of the list. :param value: The string to be entered in the prompt alert. :param driver: a selenium webdriver :param messages: A list of string which should include the alert message. :param accept: boolean defaulted to True. Accept or dismiss the alert. :raise AssertionError: driver, messages, accept and value aren't of the expected type :raise ElementNotSelectableException: when trying to send keys on a non-prompt alert :raise NoAlertPresentException: when trying to intercept an alert which not present :raise Exception: when the alert message is not one of the expected :return: 0 if success """ try: assert driver is not None and isinstance( driver, web_drivers_tuple()), "Driver is expected." assert messages is None or isinstance( messages, list), "Messages should be a list or None" assert isinstance(accept, bool), "Accept is boolean True or False" assert value is None or isinstance(value, str), "Value is None or a string" alert_object = driver.switch_to.alert if messages is not None and isinstance(messages, list): if alert_object.text not in messages: raise Exception("Message not found") if value is not None: alert_object.send_keys(value) if accept: alert_object.accept() else: alert_object.dismiss() return 0 except AssertionError as assertion: logging.error( "alerts.intercept_alert raised an assertion with following" " input driver:'{}', message:'{}' and accept:'{}'." " Assertion is '{}'".format(driver, messages, accept, assertion.args)) raise except ElementNotSelectableException as not_selectable: logging.error( "alerts.intercept_alert can't fill the alert popup with '{}'" " as there is no input field.\nGet {}".format( value, not_selectable.args)) raise ElementNotSelectableException( "Can't fill the alert popup with '{}' as there is no " "input field.".format(value)) from None except NoAlertPresentException as no_alert: logging.error( "alerts.intercept_alert can't interact with an alert as there is no " "displayed alert.\nGet {}".format(no_alert.args)) raise NoAlertPresentException( "Can't interact with an alert as there is no " "displayed alert") from None
def intercept_alert(driver=None, messages: List[str] = None, accept: bool = True, value: str = None): """ intercept_alert goal is to provide an easy interface to handle alerts. Only the web driver is mandatory. Giving only the web driver accept the alert whatever the message is. Giving a list of messages, ensure that the alert message is one of the list. :param driver: a selenium webdriver :param messages: A list of string which should include the alert message. :param accept: boolean defaulted to True. Accept or dismiss the alert. :param value: The string to be entered in the prompt alert. :raise AssertionError: driver, messages, accept and value aren't of the expected type :raise ElementNotSelectableException: when trying to send keys on a non-prompt alert :raise NoAlertPresentException: when trying to intercept an alert which not present :raise Exception: when the alert message is not one of the expected :return: 0 if success """ try: driver_field_validation(driver, {"type": "id", "value": "fake"}, log) if not isinstance(messages, list) and messages is not None: log.error("Messages should be a list or None") raise TypeError("Messages should be a list or None") if not isinstance(accept, bool): log.error("Accept is boolean True or False") raise TypeError("Accept is boolean True or False") if value is not None and not isinstance(value, str): log.error("Value is None or a string") raise TypeError("Value is None or a string") alert_object = driver.switch_to.alert if (messages is not None and isinstance(messages, list) and alert_object.text not in messages): raise Exception("Message not found") if value is not None: alert_object.send_keys(value) if accept: alert_object.accept() else: alert_object.dismiss() return 0 except ElementNotSelectableException as not_selectable: log.error( f"alerts.intercept_alert can't fill the alert popup with '{value}'" f" as there is no input field.\nGet {not_selectable.args}") raise ElementNotSelectableException( f"Can't fill the alert popup with '{value}' as there is no " "input field.") from None except ElementNotInteractableException as not_interactable: log.error(not_interactable) raise ElementNotInteractableException( "Cannot found an input field") from None except NoAlertPresentException as no_alert: log.error( f"alerts.intercept_alert can't interact with an alert as there is no " f"displayed alert.\nGet {no_alert.args}") raise NoAlertPresentException( "Can't interact with an alert as there is no " "displayed alert") from None