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