392929a582864a61a7912f35d7d4b8b0d6352aed
[WebKit-https.git] / WebDriverTests / imported / w3c / webdriver / tests / state / is_element_selected.py
1 from tests.support.asserts import assert_error, assert_dialog_handled, assert_success
2 from tests.support.inline import inline
3 from tests.support.fixtures import create_dialog
4
5
6 alert_doc = inline("<script>window.alert()</script>")
7 check_doc = inline("<input id=checked type=checkbox checked/><input id=notChecked type=checkbox/>")
8 option_doc = inline("""<select>
9                         <option id=notSelected>r-</option>
10                         <option id=selected selected>r+</option>
11                        </select>
12                     """)
13
14
15 # 13.1 Is Element Selected
16
17 def test_no_browsing_context(session, create_window):
18     # 13.1 step 1
19     session.window_handle = create_window()
20     session.close()
21
22     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
23                                     .format(session_id=session.session_id,
24                                             element_id="foo"))
25
26     assert_error(result, "no such window")
27
28
29 def test_handle_prompt_dismiss(new_session, add_browser_capabilites):
30     # 13.1 step 2
31     _, session = new_session({"capabilities": {"alwaysMatch": add_browser_capabilites({"unhandledPromptBehavior": "dismiss"})}})
32     session.url = inline("<input id=foo>")
33     element = session.find.css("#foo", all=False)
34
35     create_dialog(session)("alert", text="dismiss #1", result_var="dismiss1")
36
37     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
38                                     .format(session_id=session.session_id,
39                                             element_id=element.id))
40
41     assert_success(result, False)
42     assert_dialog_handled(session, "dismiss #1")
43
44     create_dialog(session)("confirm", text="dismiss #2", result_var="dismiss2")
45
46     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
47                                     .format(session_id=session.session_id,
48                                             element_id=element.id))
49
50     assert_success(result, False)
51     assert_dialog_handled(session, "dismiss #2")
52
53     create_dialog(session)("prompt", text="dismiss #3", result_var="dismiss3")
54
55     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
56                                     .format(session_id=session.session_id,
57                                             element_id=element.id))
58
59     assert_success(result, False)
60     assert_dialog_handled(session, "dismiss #3")
61
62
63 def test_handle_prompt_accept(new_session, add_browser_capabilites):
64     # 13.1 step 2
65     _, session = new_session({"capabilities": {"alwaysMatch": add_browser_capabilites({"unhandledPromptBehavior": "accept"})}})
66     session.url = inline("<input id=foo>")
67     element = session.find.css("#foo", all=False)
68
69     create_dialog(session)("alert", text="dismiss #1", result_var="dismiss1")
70
71     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
72                                     .format(session_id=session.session_id,
73                                             element_id=element.id))
74
75     assert_success(result, False)
76     assert_dialog_handled(session, "dismiss #1")
77
78     create_dialog(session)("confirm", text="dismiss #2", result_var="dismiss2")
79
80     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
81                                     .format(session_id=session.session_id,
82                                             element_id=element.id))
83
84     assert_success(result, False)
85     assert_dialog_handled(session, "dismiss #2")
86
87     create_dialog(session)("prompt", text="dismiss #3", result_var="dismiss3")
88
89     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
90                                     .format(session_id=session.session_id,
91                                             element_id=element.id))
92
93     assert_success(result, False)
94     assert_dialog_handled(session, "dismiss #3")
95
96
97 def test_handle_prompt_missing_value(session):
98     # 13.1 step 2
99     session.url = inline("<input id=foo>")
100     element = session.find.css("#foo", all=False)
101
102     create_dialog(session)("alert", text="dismiss #1", result_var="dismiss1")
103
104     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
105                                     .format(session_id=session.session_id,
106                                             element_id=element.id))
107
108     assert_error(result, "unexpected alert open")
109     assert_dialog_handled(session, "dismiss #1")
110
111     create_dialog(session)("confirm", text="dismiss #2", result_var="dismiss2")
112
113     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
114                                     .format(session_id=session.session_id,
115                                             element_id=element.id))
116
117     assert_error(result, "unexpected alert open")
118     assert_dialog_handled(session, "dismiss #2")
119
120     create_dialog(session)("prompt", text="dismiss #3", result_var="dismiss3")
121
122     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
123                                     .format(session_id=session.session_id,
124                                             element_id=element.id))
125
126     assert_error(result, "unexpected alert open")
127     assert_dialog_handled(session, "dismiss #3")
128
129
130 def test_element_stale(session):
131     # 13.1 step 4
132     session.url = check_doc
133     element = session.find.css("#checked", all=False)
134     session.refresh()
135     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
136                                     .format(session_id=session.session_id,
137                                             element_id=element.id))
138
139     assert_error(result, "stale element reference")
140
141
142 def test_element_checked(session):
143     # 13.1 step 5
144     session.url = check_doc
145     element = session.find.css("#checked", all=False)
146     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
147                                     .format(session_id=session.session_id,
148                                             element_id=element.id))
149
150     assert_success(result, True)
151
152
153 def test_checkbox_not_selected(session):
154     # 13.1 step 5
155     session.url = check_doc
156     element = session.find.css("#notChecked", all=False)
157     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
158                                     .format(session_id=session.session_id,
159                                             element_id=element.id))
160
161     assert_success(result, False)
162
163
164 def test_element_selected(session):
165     # 13.1 step 5
166     session.url = option_doc
167     element = session.find.css("#selected", all=False)
168     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
169                                     .format(session_id=session.session_id,
170                                             element_id=element.id))
171
172     assert_success(result, True)
173
174
175 def test_element_not_selected(session):
176     # 13.1 step 5
177     session.url = option_doc
178     element = session.find.css("#notSelected", all=False)
179     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
180                                     .format(session_id=session.session_id,
181                                             element_id=element.id))
182
183     assert_success(result, False)