1 from tests.support.asserts import assert_error, assert_dialog_handled, assert_same_element
2 from tests.support.fixtures import create_dialog
3 from tests.support.inline import inline
6 def read_global(session, name):
7 return session.execute_script("return %s;" % name)
10 def get_active_element(session):
11 return session.transport.send("GET", "session/%s/element/active" % session.session_id)
14 def assert_is_active_element(session, response):
15 """Ensure that the provided object is a successful WebDriver
16 response describing an element reference and that the referenced
17 element matches the element returned by the `activeElement`
18 attribute of the current browsing context's active document.
21 assert response.status == 200
22 assert "value" in response.body
24 from_js = session.execute_script("return document.activeElement")
26 if response.body["value"] is None:
27 assert from_js is None
29 assert_same_element(session, response.body["value"], from_js)
32 # > 1. If the current browsing context is no longer open, return error with
33 # > error code no such window.
34 def test_closed_context(session, create_window):
35 new_window = create_window()
36 session.window_handle = new_window
39 response = get_active_element(session)
40 assert_error(response, "no such window")
44 # 2. Handle any user prompts and return its value if it is an error.
46 # In order to handle any user prompts a remote end must take the following
48 # 2. Run the substeps of the first matching user prompt handler:
52 # 1. Dismiss the current user prompt.
56 def test_handle_prompt_dismiss(new_session, add_browser_capabilites):
57 _, session = new_session({"capabilities": {"alwaysMatch": add_browser_capabilites({"unhandledPromptBehavior": "dismiss"})}})
58 session.url = inline("<body><p>Hello, World!</p></body>")
60 create_dialog(session)("alert", text="dismiss #1", result_var="dismiss1")
62 response = get_active_element(session)
63 assert_is_active_element(session, response)
64 assert_dialog_handled(session, "dismiss #1")
65 assert session.execute_script("return dismiss1") is None
67 create_dialog(session)("confirm", text="dismiss #2", result_var="dismiss2")
69 response = get_active_element(session)
70 assert_is_active_element(session, response)
71 assert_dialog_handled(session, "dismiss #2")
72 assert read_global(session, "dismiss2") is None
74 create_dialog(session)("prompt", text="dismiss #3", result_var="dismiss3")
76 response = get_active_element(session)
77 assert_is_active_element(session, response)
78 assert_dialog_handled(session, "dismiss #3")
79 assert read_global(session, "dismiss3") is None
83 # 2. Handle any user prompts and return its value if it is an error.
85 # In order to handle any user prompts a remote end must take the following
87 # 2. Run the substeps of the first matching user prompt handler:
91 # 1. Accept the current user prompt.
95 def test_handle_prompt_accept(new_session, add_browser_capabilites):
96 _, session = new_session({"capabilities": {"alwaysMatch": add_browser_capabilites({"unhandledPromptBehavior": "accept"})}})
97 session.url = inline("<body><p>Hello, World!</p></body>")
98 create_dialog(session)("alert", text="accept #1", result_var="accept1")
100 response = get_active_element(session)
101 assert_is_active_element(session, response)
102 assert_dialog_handled(session, "accept #1")
103 assert read_global(session, "accept1") is None
105 create_dialog(session)("confirm", text="accept #2", result_var="accept2")
107 response = get_active_element(session)
108 assert_is_active_element(session, response)
109 assert_dialog_handled(session, "accept #2")
110 assert read_global(session, "accept2") is True
112 create_dialog(session)("prompt", text="accept #3", result_var="accept3")
114 response = get_active_element(session)
115 assert_is_active_element(session, response)
116 assert_dialog_handled(session, "accept #3")
117 assert read_global(session, "accept3") == "" or read_global(session, "accept3") == "undefined"
121 # 2. Handle any user prompts and return its value if it is an error.
123 # In order to handle any user prompts a remote end must take the following
125 # 2. Run the substeps of the first matching user prompt handler:
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>")
135 create_dialog("alert", text="dismiss #1", result_var="dismiss1")
137 response = get_active_element(session)
138 assert_error(response, "unexpected alert open")
139 assert_dialog_handled(session, "dismiss #1")
140 assert session.execute_script("return dismiss1") is None
142 create_dialog("confirm", text="dismiss #2", result_var="dismiss2")
144 response = get_active_element(session)
145 assert_error(response, "unexpected alert open")
146 assert_dialog_handled(session, "dismiss #2")
147 assert session.execute_script("return dismiss2") is False
149 create_dialog("prompt", text="dismiss #3", result_var="dismiss3")
151 response = get_active_element(session)
152 assert_error(response, "unexpected alert open")
153 assert_dialog_handled(session, "dismiss #3")
154 assert session.execute_script("return dismiss3") is None
158 # > 3. Let active element be the active element of the current browsing
159 # > context's document element.
160 # > 4. Let active web element be the JSON Serialization of active element.
161 # > 5. Return success with data active web element.
162 def test_success_document(session):
163 session.url = inline("""
168 <input style="opacity: 0" />
169 <p>Another element</p>
171 response = get_active_element(session)
172 assert_is_active_element(session, response)
175 def test_sucess_input(session):
176 session.url = inline("""
180 <input style="opacity: 0" />
181 <p>Another element</p>
183 response = get_active_element(session)
184 assert_is_active_element(session, response)
187 def test_sucess_input_non_interactable(session):
188 session.url = inline("""
192 <input style="opacity: 0" autofocus />
193 <p>Another element</p>
195 response = get_active_element(session)
196 assert_is_active_element(session, response)
199 def test_success_explicit_focus(session):
200 session.url = inline("""
207 session.execute_script("document.body.getElementsByTagName('h1')[0].focus()")
208 response = get_active_element(session)
209 assert_is_active_element(session, response)
211 session.execute_script("document.body.getElementsByTagName('input')[0].focus()")
212 response = get_active_element(session)
213 assert_is_active_element(session, response)
215 session.execute_script("document.body.getElementsByTagName('iframe')[0].focus()")
216 response = get_active_element(session)
217 assert_is_active_element(session, response)
219 session.execute_script("document.body.getElementsByTagName('iframe')[0].focus();")
220 session.execute_script("""
221 var iframe = document.body.getElementsByTagName('iframe')[0];
225 iframe.removeNode(true);
227 response = get_active_element(session)
228 assert_is_active_element(session, response)
230 session.execute_script("document.body.appendChild(document.createElement('textarea'))")
231 response = get_active_element(session)
232 assert_is_active_element(session, response)
235 def test_success_iframe_content(session):
236 session.url = inline("<body></body>")
237 session.execute_script("""
238 let iframe = document.createElement('iframe');
239 document.body.appendChild(iframe);
240 let input = iframe.contentDocument.createElement('input');
241 iframe.contentDocument.body.appendChild(input);
245 response = get_active_element(session)
246 assert_is_active_element(session, response)
249 def test_missing_document_element(session):
250 session.url = inline("<body></body>")
251 session.execute_script("""
252 if (document.body.remove) {
253 document.body.remove();
255 document.body.removeNode(true);
258 response = get_active_element(session)
259 assert_error(response, "no such element")