Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/common/alerts_tests.py
1865 views
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
import sys
19
20
import pytest
21
22
from selenium.common.exceptions import (
23
InvalidElementStateException,
24
NoAlertPresentException,
25
UnexpectedAlertPresentException,
26
)
27
from selenium.webdriver.common.by import By
28
from selenium.webdriver.support import expected_conditions as EC
29
from selenium.webdriver.support.wait import WebDriverWait
30
31
32
@pytest.fixture(autouse=True)
33
def close_alert(driver):
34
yield
35
try:
36
driver.switch_to.alert.dismiss()
37
except Exception:
38
pass
39
40
41
def test_should_be_able_to_override_the_window_alert_method(driver, pages):
42
pages.load("alerts.html")
43
driver.execute_script("window.alert = function(msg) { document.getElementById('text').innerHTML = msg; }")
44
driver.find_element(by=By.ID, value="alert").click()
45
try:
46
assert driver.find_element(By.ID, "text").text == "cheese"
47
except Exception as e:
48
# if we're here, likely the alert is displayed
49
# not dismissing it will affect other tests
50
try:
51
_wait_for_alert(driver).dismiss()
52
except Exception:
53
pass
54
raise e
55
56
57
def test_should_allow_users_to_accept_an_alert_manually(driver, pages):
58
pages.load("alerts.html")
59
driver.find_element(by=By.ID, value="alert").click()
60
alert = _wait_for_alert(driver)
61
alert.accept()
62
# If we can perform any action, we're good to go
63
assert "Testing Alerts" == driver.title
64
65
66
def test_should_allow_users_to_accept_an_alert_with_no_text_manually(driver, pages):
67
pages.load("alerts.html")
68
driver.find_element(By.ID, "empty-alert").click()
69
alert = _wait_for_alert(driver)
70
alert.accept()
71
72
# If we can perform any action, we're good to go
73
assert "Testing Alerts" == driver.title
74
75
76
def test_should_get_text_of_alert_opened_in_set_timeout(driver, pages):
77
pages.load("alerts.html")
78
driver.find_element(By.ID, "slow-alert").click()
79
80
# DO NOT WAIT OR SLEEP HERE
81
# This is a regression test for a bug where only the first switchTo call would throw,
82
# and only if it happens before the alert actually loads.
83
84
alert = _wait_for_alert(driver)
85
try:
86
assert "Slow" == alert.text
87
finally:
88
alert.accept()
89
90
91
def test_should_allow_users_to_dismiss_an_alert_manually(driver, pages):
92
pages.load("alerts.html")
93
driver.find_element(by=By.ID, value="alert").click()
94
alert = _wait_for_alert(driver)
95
alert.dismiss()
96
# If we can perform any action, we're good to go
97
assert "Testing Alerts" == driver.title
98
99
100
def test_should_allow_auser_to_accept_aprompt(driver, pages):
101
pages.load("alerts.html")
102
driver.find_element(by=By.ID, value="prompt").click()
103
alert = _wait_for_alert(driver)
104
alert.accept()
105
106
# If we can perform any action, we're good to go
107
assert "Testing Alerts" == driver.title
108
109
110
def test_should_allow_auser_to_dismiss_aprompt(driver, pages):
111
pages.load("alerts.html")
112
driver.find_element(by=By.ID, value="prompt").click()
113
alert = _wait_for_alert(driver)
114
alert.dismiss()
115
116
# If we can perform any action, we're good to go
117
assert "Testing Alerts" == driver.title
118
119
120
def test_should_allow_auser_to_set_the_value_of_aprompt(driver, pages):
121
pages.load("alerts.html")
122
driver.find_element(by=By.ID, value="prompt").click()
123
alert = _wait_for_alert(driver)
124
alert.send_keys("cheese")
125
alert.accept()
126
127
result = driver.find_element(by=By.ID, value="text").text
128
assert "cheese" == result
129
130
131
@pytest.mark.xfail_firefox
132
@pytest.mark.xfail_remote
133
def test_setting_the_value_of_an_alert_throws(driver, pages):
134
pages.load("alerts.html")
135
driver.find_element(By.ID, "alert").click()
136
137
alert = _wait_for_alert(driver)
138
with pytest.raises(InvalidElementStateException):
139
alert.send_keys("cheese")
140
alert.accept()
141
142
143
@pytest.mark.xfail_chrome(
144
condition=sys.platform == "darwin", reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=26", run=False
145
)
146
@pytest.mark.xfail_edge(
147
condition=sys.platform == "darwin", reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=26", run=False
148
)
149
def test_alert_should_not_allow_additional_commands_if_dimissed(driver, pages):
150
pages.load("alerts.html")
151
driver.find_element(By.ID, "alert").click()
152
153
alert = _wait_for_alert(driver)
154
alert.dismiss()
155
156
with pytest.raises(NoAlertPresentException):
157
alert.text
158
159
160
@pytest.mark.xfail_firefox(reason="Fails on travis")
161
@pytest.mark.xfail_remote(reason="Fails on travis")
162
@pytest.mark.xfail_safari
163
def test_should_allow_users_to_accept_an_alert_in_aframe(driver, pages):
164
pages.load("alerts.html")
165
driver.switch_to.frame(driver.find_element(By.NAME, "iframeWithAlert"))
166
driver.find_element(By.ID, "alertInFrame").click()
167
168
alert = _wait_for_alert(driver)
169
alert.accept()
170
171
assert "Testing Alerts" == driver.title
172
173
174
@pytest.mark.xfail_firefox(reason="Fails on travis")
175
@pytest.mark.xfail_remote(reason="Fails on travis")
176
@pytest.mark.xfail_safari
177
def test_should_allow_users_to_accept_an_alert_in_anested_frame(driver, pages):
178
pages.load("alerts.html")
179
driver.switch_to.frame(driver.find_element(By.NAME, "iframeWithIframe"))
180
driver.switch_to.frame(driver.find_element(By.NAME, "iframeWithAlert"))
181
182
driver.find_element(By.ID, "alertInFrame").click()
183
184
alert = _wait_for_alert(driver)
185
alert.accept()
186
187
assert "Testing Alerts" == driver.title
188
189
190
@pytest.mark.xfail_safari
191
def test_should_throw_an_exception_if_an_alert_has_not_been_dealt_with_and_dismiss_the_alert(driver, pages):
192
pages.load("alerts.html")
193
driver.find_element(By.ID, "alert").click()
194
WebDriverWait(driver, 5).until(EC.alert_is_present())
195
196
with pytest.raises(UnexpectedAlertPresentException):
197
driver.find_element(By.ID, "select").click()
198
# Alert would be dismissed automatically
199
200
201
def test_prompt_should_use_default_value_if_no_keys_sent(driver, pages):
202
pages.load("alerts.html")
203
driver.find_element(By.ID, "prompt-with-default").click()
204
205
alert = _wait_for_alert(driver)
206
alert.accept()
207
208
txt = driver.find_element(By.ID, "text").text
209
assert "This is a default value" == txt
210
211
212
def test_prompt_should_have_null_value_if_dismissed(driver, pages):
213
pages.load("alerts.html")
214
driver.find_element(By.ID, "prompt-with-default").click()
215
alert = _wait_for_alert(driver)
216
alert.dismiss()
217
218
assert "null" == driver.find_element(By.ID, "text").text
219
220
221
def test_handles_two_alerts_from_one_interaction(driver, pages):
222
pages.load("alerts.html")
223
224
driver.find_element(By.ID, "double-prompt").click()
225
226
alert1 = _wait_for_alert(driver)
227
alert1.send_keys("brie")
228
alert1.accept()
229
230
alert2 = _wait_for_alert(driver)
231
alert2.send_keys("cheddar")
232
alert2.accept()
233
234
assert driver.find_element(By.ID, "text1").text == "brie"
235
assert driver.find_element(By.ID, "text2").text == "cheddar"
236
237
238
@pytest.mark.xfail_safari
239
def test_should_handle_alert_on_page_load(driver, pages):
240
pages.load("alerts.html")
241
driver.find_element(By.ID, "open-page-with-onload-alert").click()
242
alert = _wait_for_alert(driver)
243
value = alert.text
244
alert.accept()
245
assert "onload" == value
246
247
248
def test_should_handle_alert_on_page_load_using_get(driver, pages):
249
pages.load("pageWithOnLoad.html")
250
alert = _wait_for_alert(driver)
251
value = alert.text
252
alert.accept()
253
254
assert "onload" == value
255
WebDriverWait(driver, 3).until(
256
EC.text_to_be_present_in_element((By.TAG_NAME, "p"), "Page with onload event handler")
257
)
258
259
260
@pytest.mark.xfail_chrome(reason="Non W3C conformant")
261
@pytest.mark.xfail_edge(reason="Non W3C conformant")
262
def test_should_handle_alert_on_page_before_unload(driver, pages):
263
pages.load("pageWithOnBeforeUnloadMessage.html")
264
265
element = driver.find_element(By.ID, "navigate")
266
element.click()
267
WebDriverWait(driver, 3).until(EC.title_is("Testing Alerts"))
268
269
270
def test_should_allow_the_user_to_get_the_text_of_an_alert(driver, pages):
271
pages.load("alerts.html")
272
driver.find_element(by=By.ID, value="alert").click()
273
alert = _wait_for_alert(driver)
274
value = alert.text
275
alert.accept()
276
assert "cheese" == value
277
278
279
def test_should_allow_the_user_to_get_the_text_of_aprompt(driver, pages):
280
pages.load("alerts.html")
281
driver.find_element(By.ID, "prompt").click()
282
283
alert = _wait_for_alert(driver)
284
value = alert.text
285
alert.accept()
286
287
assert "Enter something" == value
288
289
290
def test_alert_should_not_allow_additional_commands_if_dismissed(driver, pages):
291
pages.load("alerts.html")
292
driver.find_element(By.ID, "alert").click()
293
294
alert = _wait_for_alert(driver)
295
alert.accept()
296
297
with pytest.raises(NoAlertPresentException):
298
alert.text
299
300
301
@pytest.mark.xfail_firefox(reason="https://bugzilla.mozilla.org/show_bug.cgi?id=1279211")
302
@pytest.mark.xfail_remote(reason="https://bugzilla.mozilla.org/show_bug.cgi?id=1279211")
303
@pytest.mark.xfail_chrome
304
@pytest.mark.xfail_edge
305
@pytest.mark.xfail_safari
306
def test_unexpected_alert_present_exception_contains_alert_text(driver, pages):
307
pages.load("alerts.html")
308
driver.find_element(by=By.ID, value="alert").click()
309
alert = _wait_for_alert(driver)
310
value = alert.text
311
with pytest.raises(UnexpectedAlertPresentException) as e:
312
pages.load("simpleTest.html")
313
assert value == e.value.alert_text
314
assert f"Alert Text: {value}" in str(e)
315
316
317
def _wait_for_alert(driver):
318
return WebDriverWait(driver, 3).until(EC.alert_is_present())
319
320