1# META: timeout=long
2
3import pytest
4
5from tests.support.asserts import (
6    assert_error,
7    assert_same_element,
8    assert_success,
9    assert_dialog_handled,
10)
11from tests.support.inline import inline
12
13
14def find_elements(session, using, value):
15    return session.transport.send(
16        "POST", "session/{session_id}/elements".format(**vars(session)),
17        {"using": using, "value": value})
18
19
20@pytest.fixture
21def check_user_prompt_closed_without_exception(session, create_dialog):
22    def check_user_prompt_closed_without_exception(dialog_type, retval):
23        session.url = inline("<p>bar</p>")
24        element = session.find.css("p", all=False)
25
26        create_dialog(dialog_type, text=dialog_type)
27
28        response = find_elements(session, "css selector", "p")
29        value = assert_success(response)
30        assert isinstance(value, list)
31        assert len(value) == 1
32
33        assert_dialog_handled(session, expected_text=dialog_type, expected_retval=retval)
34
35        assert_same_element(session, value[0], element)
36
37    return check_user_prompt_closed_without_exception
38
39
40@pytest.fixture
41def check_user_prompt_closed_with_exception(session, create_dialog):
42    def check_user_prompt_closed_with_exception(dialog_type, retval):
43        session.url = inline("<p>bar</p>")
44
45        create_dialog(dialog_type, text=dialog_type)
46
47        response = find_elements(session, "css selector", "p")
48        assert_error(response, "unexpected alert open")
49
50        assert_dialog_handled(session, expected_text=dialog_type, expected_retval=retval)
51
52    return check_user_prompt_closed_with_exception
53
54
55@pytest.fixture
56def check_user_prompt_not_closed_but_exception(session, create_dialog):
57    def check_user_prompt_not_closed_but_exception(dialog_type):
58        session.url = inline("<p>bar</p>")
59
60        create_dialog(dialog_type, text=dialog_type)
61
62        response = find_elements(session, "css selector", "p")
63        assert_error(response, "unexpected alert open")
64
65        assert session.alert.text == dialog_type
66        session.alert.dismiss()
67
68    return check_user_prompt_not_closed_but_exception
69
70
71@pytest.mark.capabilities({"unhandledPromptBehavior": "accept"})
72@pytest.mark.parametrize("dialog_type, retval", [
73    ("alert", None),
74    ("confirm", True),
75    ("prompt", ""),
76])
77def test_accept(check_user_prompt_closed_without_exception, dialog_type, retval):
78    check_user_prompt_closed_without_exception(dialog_type, retval)
79
80
81@pytest.mark.capabilities({"unhandledPromptBehavior": "accept and notify"})
82@pytest.mark.parametrize("dialog_type, retval", [
83    ("alert", None),
84    ("confirm", True),
85    ("prompt", ""),
86])
87def test_accept_and_notify(check_user_prompt_closed_with_exception, dialog_type, retval):
88    check_user_prompt_closed_with_exception(dialog_type, retval)
89
90
91@pytest.mark.capabilities({"unhandledPromptBehavior": "dismiss"})
92@pytest.mark.parametrize("dialog_type, retval", [
93    ("alert", None),
94    ("confirm", False),
95    ("prompt", None),
96])
97def test_dismiss(check_user_prompt_closed_without_exception, dialog_type, retval):
98    check_user_prompt_closed_without_exception(dialog_type, retval)
99
100
101@pytest.mark.capabilities({"unhandledPromptBehavior": "dismiss and notify"})
102@pytest.mark.parametrize("dialog_type, retval", [
103    ("alert", None),
104    ("confirm", False),
105    ("prompt", None),
106])
107def test_dismiss_and_notify(check_user_prompt_closed_with_exception, dialog_type, retval):
108    check_user_prompt_closed_with_exception(dialog_type, retval)
109
110
111@pytest.mark.capabilities({"unhandledPromptBehavior": "ignore"})
112@pytest.mark.parametrize("dialog_type", ["alert", "confirm", "prompt"])
113def test_ignore(check_user_prompt_not_closed_but_exception, dialog_type):
114    check_user_prompt_not_closed_but_exception(dialog_type)
115
116
117@pytest.mark.parametrize("dialog_type, retval", [
118    ("alert", None),
119    ("confirm", False),
120    ("prompt", None),
121])
122def test_default(check_user_prompt_closed_with_exception, dialog_type, retval):
123    check_user_prompt_closed_with_exception(dialog_type, retval)
124