alert.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Licensed to the Software Freedom Conservancy (SFC) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The SFC licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. """
  18. The Alert implementation.
  19. """
  20. from selenium.webdriver.common.utils import keys_to_typing
  21. from selenium.webdriver.remote.command import Command
  22. class Alert(object):
  23. """
  24. Allows to work with alerts.
  25. Use this class to interact with alert prompts. It contains methods for dismissing,
  26. accepting, inputting, and getting text from alert prompts.
  27. Accepting / Dismissing alert prompts::
  28. Alert(driver).accept()
  29. Alert(driver).dismiss()
  30. Inputting a value into an alert prompt:
  31. name_prompt = Alert(driver)
  32. name_prompt.send_keys("Willian Shakesphere")
  33. name_prompt.accept()
  34. Reading a the text of a prompt for verification:
  35. alert_text = Alert(driver).text
  36. self.assertEqual("Do you wish to quit?", alert_text)
  37. """
  38. def __init__(self, driver):
  39. """
  40. Creates a new Alert.
  41. :Args:
  42. - driver: The WebDriver instance which performs user actions.
  43. """
  44. self.driver = driver
  45. @property
  46. def text(self):
  47. """
  48. Gets the text of the Alert.
  49. """
  50. if self.driver.w3c:
  51. return self.driver.execute(Command.W3C_GET_ALERT_TEXT)["value"]
  52. else:
  53. return self.driver.execute(Command.GET_ALERT_TEXT)["value"]
  54. def dismiss(self):
  55. """
  56. Dismisses the alert available.
  57. """
  58. if self.driver.w3c:
  59. self.driver.execute(Command.W3C_DISMISS_ALERT)
  60. else:
  61. self.driver.execute(Command.DISMISS_ALERT)
  62. def accept(self):
  63. """
  64. Accepts the alert available.
  65. Usage::
  66. Alert(driver).accept() # Confirm a alert dialog.
  67. """
  68. if self.driver.w3c:
  69. self.driver.execute(Command.W3C_ACCEPT_ALERT)
  70. else:
  71. self.driver.execute(Command.ACCEPT_ALERT)
  72. def send_keys(self, keysToSend):
  73. """
  74. Send Keys to the Alert.
  75. :Args:
  76. - keysToSend: The text to be sent to Alert.
  77. """
  78. if self.driver.w3c:
  79. self.driver.execute(Command.W3C_SET_ALERT_VALUE, {'value': keys_to_typing(keysToSend),
  80. 'text': keysToSend})
  81. else:
  82. self.driver.execute(Command.SET_ALERT_VALUE, {'text': keysToSend})
  83. def authenticate(self, username, password):
  84. """
  85. Send the username / password to an Authenticated dialog (like with Basic HTTP Auth).
  86. Implicitly 'clicks ok'
  87. Usage::
  88. driver.switch_to.alert.authenticate('cheese', 'secretGouda')
  89. :Args:
  90. -username: string to be set in the username section of the dialog
  91. -password: string to be set in the password section of the dialog
  92. """
  93. self.driver.execute(
  94. Command.SET_ALERT_CREDENTIALS,
  95. {'username': username, 'password': password})
  96. self.accept()