Unreviewed. Update W3C WebDriver imported tests.
[WebKit-https.git] / WebDriverTests / imported / w3c / webdriver / tests / element_retrieval / get_active_element.py
index 900e3fd..5c2b52a 100644 (file)
@@ -1,22 +1,33 @@
-import pytest
-
-from tests.support.asserts import assert_error, assert_success, assert_dialog_handled, assert_same_element
+from tests.support.asserts import assert_error, assert_dialog_handled, assert_same_element
 from tests.support.fixtures import create_dialog
 from tests.support.inline import inline
 
-def assert_result_is_active_element(session, result):
-    """Ensure that the provided object is a successful WebDriver response
-    describing an element reference and that the referenced element matches the
-    element returned by the `activeElement` attribute of the current browsing
-    context's active document."""
-    assert result.status == 200
 
-    from_js = session.execute_script("return document.activeElement;")
+def read_global(session, name):
+    return session.execute_script("return %s;" % name)
+
+
+def get_active_element(session):
+    return session.transport.send("GET", "session/%s/element/active" % session.session_id)
+
+
+def assert_is_active_element(session, response):
+    """Ensure that the provided object is a successful WebDriver
+    response describing an element reference and that the referenced
+    element matches the element returned by the `activeElement`
+    attribute of the current browsing context's active document.
+
+    """
+    assert response.status == 200
+    assert "value" in response.body
+
+    from_js = session.execute_script("return document.activeElement")
 
-    if result.body["value"] is None:
-        assert from_js == None
+    if response.body["value"] is None:
+        assert from_js is None
     else:
-        assert_same_element(session, result.body["value"], from_js)
+        assert_same_element(session, response.body["value"], from_js)
+
 
 # > 1. If the current browsing context is no longer open, return error with
 # >    error code no such window.
@@ -25,10 +36,9 @@ def test_closed_context(session, create_window):
     session.window_handle = new_window
     session.close()
 
-    result = session.transport.send("GET",
-                                    "session/%s/element/active" % session.session_id)
+    response = get_active_element(session)
+    assert_error(response, "no such window")
 
-    assert_error(result, "no such window")
 
 # [...]
 # 2. Handle any user prompts and return its value if it is an error.
@@ -49,30 +59,25 @@ def test_handle_prompt_dismiss(new_session, add_browser_capabilites):
 
     create_dialog(session)("alert", text="dismiss #1", result_var="dismiss1")
 
-    result = session.transport.send("GET",
-                                    "session/%s/element/active" % session.session_id)
-
-    assert_result_is_active_element(session, result)
+    response = get_active_element(session)
+    assert_is_active_element(session, response)
     assert_dialog_handled(session, "dismiss #1")
-    assert session.execute_script("return dismiss1;") == None
+    assert session.execute_script("return dismiss1") is None
 
     create_dialog(session)("confirm", text="dismiss #2", result_var="dismiss2")
 
-    result = session.transport.send("GET",
-                                    "session/%s/element/active" % session.session_id)
-
-    assert_result_is_active_element(session, result)
+    response = get_active_element(session)
+    assert_is_active_element(session, response)
     assert_dialog_handled(session, "dismiss #2")
-    assert read_global(session, "dismiss2") == None
+    assert read_global(session, "dismiss2") is None
 
     create_dialog(session)("prompt", text="dismiss #3", result_var="dismiss3")
 
-    result = session.transport.send("GET",
-                                    "session/%s/element/active" % session.session_id)
-
-    assert_result_is_active_element(session, result)
+    response = get_active_element(session)
+    assert_is_active_element(session, response)
     assert_dialog_handled(session, "dismiss #3")
-    assert read_global(session, "dismiss3") == None
+    assert read_global(session, "dismiss3") is None
+
 
 # [...]
 # 2. Handle any user prompts and return its value if it is an error.
@@ -92,30 +97,25 @@ def test_handle_prompt_accept(new_session, add_browser_capabilites):
     session.url = inline("<body><p>Hello, World!</p></body>")
     create_dialog(session)("alert", text="accept #1", result_var="accept1")
 
-    result = session.transport.send("GET",
-                                    "session/%s/element/active" % session.session_id)
-
-    assert_result_is_active_element(session, result)
+    response = get_active_element(session)
+    assert_is_active_element(session, response)
     assert_dialog_handled(session, "accept #1")
-    assert read_global(session, "accept1") == None
+    assert read_global(session, "accept1") is None
 
     create_dialog(session)("confirm", text="accept #2", result_var="accept2")
 
-    result = session.transport.send("GET",
-                                    "session/%s/element/active" % session.session_id)
-
-    assert_result_is_active_element(session, result)
+    response = get_active_element(session)
+    assert_is_active_element(session, response)
     assert_dialog_handled(session, "accept #2")
-    assert read_global(session, "accept2"), True
+    assert read_global(session, "accept2") is True
 
     create_dialog(session)("prompt", text="accept #3", result_var="accept3")
 
-    result = session.transport.send("GET",
-                                    "session/%s/element/active" % session.session_id)
-
-    assert_result_is_active_element(session, result)
+    response = get_active_element(session)
+    assert_is_active_element(session, response)
     assert_dialog_handled(session, "accept #3")
-    assert read_global(session, "accept3") == ""
+    assert read_global(session, "accept3") == "" or read_global(session, "accept3") == "undefined"
+
 
 # [...]
 # 2. Handle any user prompts and return its value if it is an error.
@@ -134,30 +134,25 @@ def test_handle_prompt_missing_value(session, create_dialog):
 
     create_dialog("alert", text="dismiss #1", result_var="dismiss1")
 
-    result = session.transport.send("GET",
-                                    "session/%s/element/active" % session.session_id)
-
-    assert_error(result, "unexpected alert open")
+    response = get_active_element(session)
+    assert_error(response, "unexpected alert open")
     assert_dialog_handled(session, "dismiss #1")
-    assert session.execute_script("return accept1;") == None
+    assert session.execute_script("return dismiss1") is None
 
     create_dialog("confirm", text="dismiss #2", result_var="dismiss2")
 
-    result = session.transport.send("GET",
-                                    "session/%s/element/active" % session.session_id)
-
-    assert_error(result, "unexpected alert open")
+    response = get_active_element(session)
+    assert_error(response, "unexpected alert open")
     assert_dialog_handled(session, "dismiss #2")
-    assert session.execute_script("return dismiss2;") == False
+    assert session.execute_script("return dismiss2") is False
 
     create_dialog("prompt", text="dismiss #3", result_var="dismiss3")
 
-    result = session.transport.send("GET",
-                                    "session/%s/element/active" % session.session_id)
-
-    assert_error(result, "unexpected alert open")
+    response = get_active_element(session)
+    assert_error(response, "unexpected alert open")
     assert_dialog_handled(session, "dismiss #3")
-    assert session.execute_script("return dismiss3;") == None
+    assert session.execute_script("return dismiss3") is None
+
 
 # > [...]
 # > 3. Let active element be the active element of the current browsing
@@ -170,36 +165,36 @@ def test_success_document(session):
             <h1>Heading</h1>
             <input />
             <input />
-            <input style="opacity: 0;" />
+            <input style="opacity: 0" />
             <p>Another element</p>
         </body>""")
-    result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
+    response = get_active_element(session)
+    assert_is_active_element(session, response)
 
-    assert_result_is_active_element(session, result)
 
 def test_sucess_input(session):
     session.url = inline("""
         <body>
             <h1>Heading</h1>
             <input autofocus />
-            <input style="opacity: 0;" />
+            <input style="opacity: 0" />
             <p>Another element</p>
         </body>""")
-    result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
+    response = get_active_element(session)
+    assert_is_active_element(session, response)
 
-    assert_result_is_active_element(session, result)
 
 def test_sucess_input_non_interactable(session):
     session.url = inline("""
         <body>
             <h1>Heading</h1>
             <input />
-            <input style="opacity: 0;" autofocus />
+            <input style="opacity: 0" autofocus />
             <p>Another element</p>
         </body>""")
-    result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
+    response = get_active_element(session)
+    assert_is_active_element(session, response)
 
-    assert_result_is_active_element(session, result)
 
 def test_success_explicit_focus(session):
     session.url = inline("""
@@ -209,44 +204,56 @@ def test_success_explicit_focus(session):
             <iframe></iframe>
         </body>""")
 
-    session.execute_script("document.body.getElementsByTagName('h1')[0].focus();")
-    result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
-    assert_result_is_active_element(session, result)
+    session.execute_script("document.body.getElementsByTagName('h1')[0].focus()")
+    response = get_active_element(session)
+    assert_is_active_element(session, response)
 
-    session.execute_script("document.body.getElementsByTagName('input')[0].focus();")
-    result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
-    assert_result_is_active_element(session, result)
+    session.execute_script("document.body.getElementsByTagName('input')[0].focus()")
+    response = get_active_element(session)
+    assert_is_active_element(session, response)
 
-    session.execute_script("document.body.getElementsByTagName('iframe')[0].focus();")
-    result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
-    assert_result_is_active_element(session, result)
+    session.execute_script("document.body.getElementsByTagName('iframe')[0].focus()")
+    response = get_active_element(session)
+    assert_is_active_element(session, response)
 
     session.execute_script("document.body.getElementsByTagName('iframe')[0].focus();")
-    session.execute_script("document.body.getElementsByTagName('iframe')[0].remove();")
-    result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
-    assert_result_is_active_element(session, result)
+    session.execute_script("""
+        var iframe = document.body.getElementsByTagName('iframe')[0];
+        if (iframe.remove) {
+          iframe.remove();
+        } else {
+          iframe.removeNode(true);
+        }""")
+    response = get_active_element(session)
+    assert_is_active_element(session, response)
+
+    session.execute_script("document.body.appendChild(document.createElement('textarea'))")
+    response = get_active_element(session)
+    assert_is_active_element(session, response)
 
-    session.execute_script("document.body.appendChild(document.createElement('textarea'));")
-    result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
-    assert_result_is_active_element(session, result)
 
 def test_success_iframe_content(session):
     session.url = inline("<body></body>")
     session.execute_script("""
-        var iframe = document.createElement('iframe');
+        let iframe = document.createElement('iframe');
         document.body.appendChild(iframe);
-        var input = iframe.contentDocument.createElement('input');
+        let input = iframe.contentDocument.createElement('input');
         iframe.contentDocument.body.appendChild(input);
-        input.focus();""")
+        input.focus();
+        """)
 
-    result = session.transport.send("GET", "session/%s/element/active" % session.session_id)
+    response = get_active_element(session)
+    assert_is_active_element(session, response)
 
-    assert_result_is_active_element(session, result)
 
-def test_sucess_without_body(session):
+def test_missing_document_element(session):
     session.url = inline("<body></body>")
-    session.execute_script("document.body.remove();")
-
-    result = session.transport.send("GET", "session/%s/element/active"% session.session_id)
-
-    assert_result_is_active_element(session, result)
+    session.execute_script("""
+        if (document.body.remove) {
+          document.body.remove();
+        } else {
+          document.body.removeNode(true);
+        }""")
+
+    response = get_active_element(session)
+    assert_error(response, "no such element")