summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/mozilla/tests/webdriver/classic/execute_async_script/execute_async.py
blob: fe42e44ce71d150d97258fc3490c7327f180d5c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import pytest
from tests.support.asserts import assert_success
from tests.support.sync import Poll
from webdriver.error import NoSuchAlertException


def execute_async_script(session, script, args=None):
    if args is None:
        args = []
    body = {"script": script, "args": args}

    return session.transport.send(
        "POST", "/session/{session_id}/execute/async".format(**vars(session)), body
    )


@pytest.mark.parametrize("dialog_type", ["alert", "confirm", "prompt"])
def test_no_abort_by_user_prompt_in_other_tab(session, inline, dialog_type):
    original_handle = session.window_handle
    original_handles = session.handles

    session.url = inline(
        """
      <a onclick="window.open();">open window</a>
      <script>
        window.addEventListener("message", function (event) {{
          {}("foo");
        }});
      </script>
    """.format(
            dialog_type
        )
    )

    session.find.css("a", all=False).click()
    wait = Poll(session, timeout=5, message="No new window has been opened")
    new_handles = wait.until(lambda s: set(s.handles) - set(original_handles))
    assert len(new_handles) == 1

    session.window_handle = new_handles.pop()

    response = execute_async_script(
        session,
        """
        const resolve = arguments[0];

        // Trigger opening a user prompt in the other window.
        window.opener.postMessage("foo", "*");

        // Delay resolving the Promise to ensure a user prompt has been opened.
        setTimeout(() => resolve(42), 500);
        """,
    )

    assert_success(response, 42)

    session.window.close()

    session.window_handle = original_handle

    # Opening the alert in a different window is async here and can cause
    # delays in slow builds like CCOV or TSAN.
    wait = Poll(
        session,
        timeout=15,
        ignored_exceptions=NoSuchAlertException,
        message="No user prompt with text 'foo' detected",
    )
    wait.until(lambda s: s.alert.text == "foo")

    session.alert.accept()