8027d5e03cbca3f6473189028dfb379abc215ed3
[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
34     create_dialog(session)("alert", text="dismiss #1", result_var="dismiss1")
35
36     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
37                                     .format(session_id=session.session_id,
38                                             element_id="foo"))
39
40     assert_success(result, "foo")
41     assert_dialog_handled(session, "dismiss #1")
42
43     create_dialog(session)("confirm", text="dismiss #2", result_var="dismiss2")
44
45     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
46                                     .format(session_id=session.session_id,
47                                             element_id="foo"))
48
49     assert_success(result, "foo")
50     assert_dialog_handled(session, "dismiss #2")
51
52     create_dialog(session)("prompt", text="dismiss #3", result_var="dismiss3")
53
54     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
55                                     .format(session_id=session.session_id,
56                                             element_id="foo"))
57
58     assert_success(result, "foo")
59     assert_dialog_handled(session, "dismiss #3")
60
61
62 def test_handle_prompt_accept(new_session, add_browser_capabilites):
63     # 13.1 step 2
64     _, session = new_session({"capabilities": {"alwaysMatch": add_browser_capabilites({"unhandledPromptBehavior": "accept"})}})
65     session.url = inline("<input id=foo>")
66
67     create_dialog(session)("alert", text="dismiss #1", result_var="dismiss1")
68
69     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
70                                     .format(session_id=session.session_id,
71                                             element_id="foo"))
72
73     assert_success(result, "foo")
74     assert_dialog_handled(session, "dismiss #1")
75
76     create_dialog(session)("confirm", text="dismiss #2", result_var="dismiss2")
77
78     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
79                                     .format(session_id=session.session_id,
80                                             element_id="foo"))
81
82     assert_success(result, "foo")
83     assert_dialog_handled(session, "dismiss #2")
84
85     create_dialog(session)("prompt", text="dismiss #3", result_var="dismiss3")
86
87     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
88                                     .format(session_id=session.session_id,
89                                             element_id="foo"))
90
91     assert_success(result, "foo")
92     assert_dialog_handled(session, "dismiss #3")
93
94
95 def test_handle_prompt_missing_value(session, create_dialog):
96     # 13.1 step 2
97     session.url = inline("<input id=foo>")
98
99     create_dialog(session)("alert", text="dismiss #1", result_var="dismiss1")
100
101     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
102                                     .format(session_id=session.session_id,
103                                             element_id="foo"))
104
105     assert_error(result, "unexpected alert open")
106     assert_dialog_handled(session, "dismiss #1")
107
108     create_dialog(session)("confirm", text="dismiss #2", result_var="dismiss2")
109
110     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
111                                     .format(session_id=session.session_id,
112                                             element_id="foo"))
113
114     assert_error(result, "unexpected alert open")
115     assert_dialog_handled(session, "dismiss #2")
116
117     create_dialog(session)("prompt", text="dismiss #3", result_var="dismiss3")
118
119     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
120                                     .format(session_id=session.session_id,
121                                             element_id="foo"))
122
123     assert_error(result, "unexpected alert open")
124     assert_dialog_handled(session, "dismiss #3")
125
126
127 def test_element_stale(session):
128     # 13.1 step 4
129     session.url = check_doc
130     element = session.find.css("#checked", all=False)
131     session.refresh()
132     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
133                                     .format(session_id=session.session_id,
134                                             element_id=element.id))
135
136     assert_error(result, "stale element reference")
137
138
139 def test_element_checked(session):
140     # 13.1 step 5
141     session.url = check_doc
142     element = session.find.css("#checked", all=False)
143     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
144                                     .format(session_id=session.session_id,
145                                             element_id=element.id))
146
147     assert_success(result, True)
148
149
150 def test_checkbox_not_selected(session):
151     # 13.1 step 5
152     session.url = check_doc
153     element = session.find.css("#notChecked", all=False)
154     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
155                                     .format(session_id=session.session_id,
156                                             element_id=element.id))
157
158     assert_success(result, False)
159
160
161 def test_element_selected(session):
162     # 13.1 step 5
163     session.url = option_doc
164     element = session.find.css("#selected", all=False)
165     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
166                                     .format(session_id=session.session_id,
167                                             element_id=element.id))
168
169     assert_success(result, True)
170
171
172 def test_element_not_selected(session):
173     # 13.1 step 5
174     session.url = option_doc
175     element = session.find.css("#notSelected", all=False)
176     result = session.transport.send("GET", "session/{session_id}/element/{element_id}/selected"
177                                     .format(session_id=session.session_id,
178                                             element_id=element.id))
179
180     assert_success(result, False)