Allows to work with alerts. Use this class to interact with alert prompts. It contains methods for dismissing, accepting, inputting, and getting text from alert prompts. Accepting / Dismissing alert prompts:: Alert(driver).accept() Alert(driver).dismiss() Inputti
| 21 | |
| 22 | |
| 23 | class Alert: |
| 24 | """Allows to work with alerts. |
| 25 | |
| 26 | Use this class to interact with alert prompts. It contains methods for dismissing, |
| 27 | accepting, inputting, and getting text from alert prompts. |
| 28 | |
| 29 | Accepting / Dismissing alert prompts:: |
| 30 | |
| 31 | Alert(driver).accept() |
| 32 | Alert(driver).dismiss() |
| 33 | |
| 34 | Inputting a value into an alert prompt:: |
| 35 | |
| 36 | name_prompt = Alert(driver) |
| 37 | name_prompt.send_keys("Willian Shakesphere") |
| 38 | name_prompt.accept() |
| 39 | |
| 40 | |
| 41 | Reading a the text of a prompt for verification:: |
| 42 | |
| 43 | alert_text = Alert(driver).text |
| 44 | self.assertEqual("Do you wish to quit?", alert_text) |
| 45 | """ |
| 46 | |
| 47 | def __init__(self, driver) -> None: |
| 48 | """Creates a new Alert. |
| 49 | |
| 50 | Args: |
| 51 | driver: The WebDriver instance which performs user actions. |
| 52 | """ |
| 53 | self.driver = driver |
| 54 | |
| 55 | @property |
| 56 | def text(self) -> str: |
| 57 | """Gets the text of the Alert.""" |
| 58 | return self.driver.execute(Command.W3C_GET_ALERT_TEXT)["value"] |
| 59 | |
| 60 | def dismiss(self) -> None: |
| 61 | """Dismisses the alert available.""" |
| 62 | self.driver.execute(Command.W3C_DISMISS_ALERT) |
| 63 | |
| 64 | def accept(self) -> None: |
| 65 | """Accepts the alert available. |
| 66 | |
| 67 | Example: |
| 68 | Alert(driver).accept() # Confirm a alert dialog. |
| 69 | """ |
| 70 | self.driver.execute(Command.W3C_ACCEPT_ALERT) |
| 71 | |
| 72 | def send_keys(self, keysToSend: str) -> None: |
| 73 | """Send Keys to the Alert. |
| 74 | |
| 75 | Args: |
| 76 | keysToSend: The text to be sent to Alert. |
| 77 | """ |
| 78 | self.driver.execute(Command.W3C_SET_ALERT_VALUE, {"value": keys_to_typing(keysToSend), "text": keysToSend}) |