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