WebDriver: Add support to import and run W3C tests
[WebKit-https.git] / WebDriverTests / imported / w3c / webdriver / tests / element_retrieval / get_active_element.py
1 import pytest
2
3 from tests.support.asserts import assert_error, assert_success, assert_dialog_handled, assert_same_element
4 from tests.support.fixtures import create_dialog
5 from tests.support.inline import inline
6
7 def assert_result_is_active_element(session, result):
8     """Ensure that the provided object is a successful WebDriver response
9     describing an element reference and that the referenced element matches the
10     element returned by the `activeElement` attribute of the current browsing
11     context's active document."""
12     assert result.status == 200
13
14     from_js = session.execute_script("return document.activeElement;")
15
16     if result.body["value"] is None:
17         assert from_js == None
18     else:
19         assert_same_element(session, result.body["value"], from_js)
20
21 # > 1. If the current browsing context is no longer open, return error with
22 # >    error code no such window.
23 def test_closed_context(session, create_window):
24     new_window = create_window()
25     session.window_handle = new_window
26     session.close()
27
28     result = session.transport.send("GET",
29                                     "session/%s/element/active" % session.session_id)
30
31     assert_error(result, "no such window")
32
33 # [...]
34 # 2. Handle any user prompts and return its value if it is an error.
35 # [...]
36 # In order to handle any user prompts a remote end must take the following
37 # steps:
38 # 2. Run the substeps of the first matching user prompt handler:
39 #
40 #    [...]
41 #    - dismiss state
42 #      1. Dismiss the current user prompt.
43 #    [...]
44 #
45 # 3. Return success.
46 def test_handle_prompt_dismiss(new_session, add_browser_capabilites):
47     _, session = new_session({"capabilities": {"alwaysMatch": add_browser_capabilites({"unhandledPromptBehavior": "dismiss"})}})
48     session.url = inline("<body><p>Hello, World!</p></body>")
49
50     create_dialog(session)("alert", text="dismiss #1", result_var="dismiss1")
51
52     result = session.transport.send("GET",
53                                     "session/%s/element/active" % session.session_id)
54
55     assert_result_is_active_element(session, result)
56     assert_dialog_handled(session, "dismiss #1")
57     assert session.execute_script("return dismiss1;") == None
58
59     create_dialog(session)("confirm", text="dismiss #2", result_var="dismiss2")
60
61     result = session.transport.send("GET",
62                                     "session/%s/element/active" % session.session_id)
63
64     assert_result_is_active_element(session, result)
65     assert_dialog_handled(session, "dismiss #2")
66     assert read_global(session, "dismiss2") == None
67
68     create_dialog(session)("prompt", text="dismiss #3", result_var="dismiss3")
69
70     result = session.transport.send("GET",
71                                     "session/%s/element/active" % session.session_id)
72
73     assert_result_is_active_element(session, result)
74     assert_dialog_handled(session, "dismiss #3")
75     assert read_global(session, "dismiss3") == None
76
77 # [...]
78 # 2. Handle any user prompts and return its value if it is an error.
79 # [...]
80 # In order to handle any user prompts a remote end must take the following
81 # steps:
82 # 2. Run the substeps of the first matching user prompt handler:
83 #
84 #    [...]
85 #    - accept state
86 #      1. Accept the current user prompt.
87 #    [...]
88 #
89 # 3. Return success.
90 def test_handle_prompt_accept(new_session, add_browser_capabilites):
91     _, session = new_session({"capabilities": {"alwaysMatch": add_browser_capabilites({"unhandledPromptBehavior": "accept"})}})
92     session.url = inline("<body><p>Hello, World!</p></body>")
93     create_dialog(session)("alert", text="accept #1", result_var="accept1")
94
95     result = session.transport.send("GET",
96                                     "session/%s/element/active" % session.session_id)
97
98     assert_result_is_active_element(session, result)
99     assert_dialog_handled(session, "accept #1")
100     assert read_global(session, "accept1") == None
101
102     create_dialog(session)("confirm", text="accept #2", result_var="accept2")
103
104     result = session.transport.send("GET",
105                                     "session/%s/element/active" % session.session_id)
106
107     assert_result_is_active_element(session, result)
108     assert_dialog_handled(session, "accept #2")
109     assert read_global(session, "accept2"), True
110
111     create_dialog(session)("prompt", text="accept #3", result_var="accept3")
112
113     result = session.transport.send("GET",
114                                     "session/%s/element/active" % session.session_id)
115
116     assert_result_is_active_element(session, result)
117     assert_dialog_handled(session, "accept #3")
118     assert read_global(session, "accept3") == ""
119
120 # [...]
121 # 2. Handle any user prompts and return its value if it is an error.
122 # [...]
123 # In order to handle any user prompts a remote end must take the following
124 # steps:
125 # 2. Run the substeps of the first matching user prompt handler:
126 #
127 #    [...]
128 #    - missing value default state
129 #    - not in the table of simple dialogs
130 #      1. Dismiss the current user prompt.
131 #      2. Return error with error code unexpected alert open.
132 def test_handle_prompt_missing_value(session, create_dialog):
133     session.url = inline("<body><p>Hello, World!</p></body>")
134
135     create_dialog("alert", text="dismiss #1", result_var="dismiss1")
136
137     result = session.transport.send("GET",
138                                     "session/%s/element/active" % session.session_id)
139
140     assert_error(result, "unexpected alert open")
141     assert_dialog_handled(session, "dismiss #1")
142     assert session.execute_script("return accept1;") == None
143
144     create_dialog("confirm", text="dismiss #2", result_var="dismiss2")
145
146     result = session.transport.send("GET",
147                                     "session/%s/element/active" % session.session_id)
148
149     assert_error(result, "unexpected alert open")
150     assert_dialog_handled(session, "dismiss #2")
151     assert session.execute_script("return dismiss2;") == False
152
153     create_dialog("prompt", text="dismiss #3", result_var="dismiss3")
154
155     result = session.transport.send("GET",
156                                     "session/%s/element/active" % session.session_id)
157
158     assert_error(result, "unexpected alert open")
159     assert_dialog_handled(session, "dismiss #3")
160     assert session.execute_script("return dismiss3;") == None
161
162 # > [...]
163 # > 3. Let active element be the active element of the current browsing
164 # >    context's document element.
165 # > 4. Let active web element be the JSON Serialization of active element.
166 # > 5. Return success with data active web element.
167 def test_success_document(session):
168     session.url = inline("""
169         <body>
170             <h1>Heading</h1>
171             <input />
172             <input />
173             <input style="opacity: 0;" />
174             <p>Another element</p>
175         </body>""")
176     result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
177
178     assert_result_is_active_element(session, result)
179
180 def test_sucess_input(session):
181     session.url = inline("""
182         <body>
183             <h1>Heading</h1>
184             <input autofocus />
185             <input style="opacity: 0;" />
186             <p>Another element</p>
187         </body>""")
188     result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
189
190     assert_result_is_active_element(session, result)
191
192 def test_sucess_input_non_interactable(session):
193     session.url = inline("""
194         <body>
195             <h1>Heading</h1>
196             <input />
197             <input style="opacity: 0;" autofocus />
198             <p>Another element</p>
199         </body>""")
200     result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
201
202     assert_result_is_active_element(session, result)
203
204 def test_success_explicit_focus(session):
205     session.url = inline("""
206         <body>
207             <h1>Heading</h1>
208             <input />
209             <iframe></iframe>
210         </body>""")
211
212     session.execute_script("document.body.getElementsByTagName('h1')[0].focus();")
213     result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
214     assert_result_is_active_element(session, result)
215
216     session.execute_script("document.body.getElementsByTagName('input')[0].focus();")
217     result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
218     assert_result_is_active_element(session, result)
219
220     session.execute_script("document.body.getElementsByTagName('iframe')[0].focus();")
221     result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
222     assert_result_is_active_element(session, result)
223
224     session.execute_script("document.body.getElementsByTagName('iframe')[0].focus();")
225     session.execute_script("document.body.getElementsByTagName('iframe')[0].remove();")
226     result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
227     assert_result_is_active_element(session, result)
228
229     session.execute_script("document.body.appendChild(document.createElement('textarea'));")
230     result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
231     assert_result_is_active_element(session, result)
232
233 def test_success_iframe_content(session):
234     session.url = inline("<body></body>")
235     session.execute_script("""
236         var iframe = document.createElement('iframe');
237         document.body.appendChild(iframe);
238         var input = iframe.contentDocument.createElement('input');
239         iframe.contentDocument.body.appendChild(input);
240         input.focus();""")
241
242     result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
243
244     assert_result_is_active_element(session, result)
245
246 def test_sucess_without_body(session):
247     session.url = inline("<body></body>")
248     session.execute_script("document.body.remove();")
249
250     result = session.transport.send("GET", "session/%s/element/active"% session.session_id)
251
252     assert_result_is_active_element(session, result)