| 1 |
import pytest |
| 2 |
from tests.support.asserts import assert_success |
| 3 |
from tests.support.sync import Poll |
| 4 |
from webdriver.error import NoSuchAlertException |
| 5 |
|
| 6 |
|
| 7 |
def execute_async_script(session, script, args=None): |
| 8 |
if args is None: |
| 9 |
args = [] |
| 10 |
body = {"script": script, "args": args} |
| 11 |
|
| 12 |
return session.transport.send( |
| 13 |
"POST", "/session/{session_id}/execute/async".format(**vars(session)), body |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
@pytest.mark.parametrize("dialog_type", ["alert", "confirm", "prompt"]) |
| 18 |
def test_no_abort_by_user_prompt_in_other_tab(session, inline, dialog_type): |
| 19 |
original_handle = session.window_handle |
| 20 |
original_handles = session.handles |
| 21 |
|
| 22 |
session.url = inline( |
| 23 |
f""" |
| 24 |
<a onclick="window.open();">open window</a> |
| 25 |
<script> |
| 26 |
window.addEventListener("message", function (event) {{ |
| 27 |
{dialog_type}("foo"); |
| 28 |
}}); |
| 29 |
</script> |
| 30 |
""" |
| 31 |
) |
| 32 |
|
| 33 |
session.find.css("a", all=False).click() |
| 34 |
wait = Poll(session, timeout=5, message="No new window has been opened") |
| 35 |
new_handles = wait.until(lambda s: set(s.handles) - set(original_handles)) |
| 36 |
assert len(new_handles) == 1 |
| 37 |
|
| 38 |
session.window_handle = new_handles.pop() |
| 39 |
|
| 40 |
response = execute_async_script( |
| 41 |
session, |
| 42 |
""" |
| 43 |
const resolve = arguments[0]; |
| 44 |
|
| 45 |
// Trigger opening a user prompt in the other window. |
| 46 |
window.opener.postMessage("foo", "*"); |
| 47 |
|
| 48 |
// Delay resolving the Promise to ensure a user prompt has been opened. |
| 49 |
setTimeout(() => resolve(42), 500); |
| 50 |
""", |
| 51 |
) |
| 52 |
|
| 53 |
assert_success(response, 42) |
| 54 |
|
| 55 |
session.window.close() |
| 56 |
|
| 57 |
session.window_handle = original_handle |
| 58 |
|
| 59 |
# Opening the alert in a different window is async here and can cause |
| 60 |
# delays in slow builds like CCOV or TSAN. |
| 61 |
wait = Poll( |
| 62 |
session, |
| 63 |
timeout=15, |
| 64 |
ignored_exceptions=NoSuchAlertException, |
| 65 |
message="No user prompt with text 'foo' detected", |
| 66 |
) |
| 67 |
wait.until(lambda s: s.alert.text == "foo") |
| 68 |
|
| 69 |
session.alert.accept() |