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