def test_html_all_collection(session): session.url = inline(""" <p>foo <p>bar """) html = session.find.css("html", all=False) head = session.find.css("head", all=False) body = session.find.css("body", all=False) ps = session.find.css("p") response = execute_async_script( session, """ let resolve = arguments[0]; resolve(document.all); """) value = assert_success(response) assert isinstance(value, list) # <html>, <head>, <body>, <p>, <p> assert len(value) == 5 assert_same_element(session, html, value[0]) assert_same_element(session, head, value[1]) assert_same_element(session, body, value[2]) assert_same_element(session, ps[0], value[3]) assert_same_element(session, ps[1], value[4])
def test_xhtml_namespace(session, using, value): session.url = inline("""<a href="#" id="linkText">full link text</a>""", doctype="xhtml") expected = session.execute_script("return document.links[0]") response = find_element(session, using, value) value = assert_success(response) assert_same_element(session, value, expected)
def test_parent_htmldocument(session): session.url = inline("") from_element = session.execute_script("""return document.querySelector("body")""") expected = session.execute_script("return document.documentElement") response = find_element(session, from_element.id, "xpath", "..") value = assert_success(response) assert_same_element(session, value, expected)
def test_get_shadow_root(session, checkbox_dom): session.url = checkbox_dom expected = session.execute_script( "return document.querySelector('custom-checkbox-element').shadowRoot.host" ) custom_element = session.find.css("custom-checkbox-element", all=False) response = get_shadow_root(session, custom_element.id) assert_success(response) assert_same_element(session, custom_element, expected)
def test_found_element_equivalence(session, get_shadow_page): session.url = get_shadow_page("<div><input type='checkbox'/></div>") custom_element = session.find.css("custom-shadow-element", all=False) expected = session.execute_script( "return arguments[0].shadowRoot.querySelector('input')", args=(custom_element, )) shadow_root = custom_element.shadow_root response = find_element(session, shadow_root.id, "css", "input") value = assert_success(response) assert_same_element(session, value, expected)
def test_element_in_object(session, inline): session.url = inline("<div></div>") div = session.find.css("div", all=False) response = execute_script(session, """ let div = document.querySelector("div"); div.reference = div; return {foo: div}; """) value = assert_success(response) assert_same_element(session, div, value["foo"])
def test_get_shadow_root(session, inline, checkbox_dom): session.url = inline(checkbox_dom) expected = session.execute_script( "return document.querySelector('custom-checkbox-element').shadowRoot.host" ) custom_element = session.find.css("custom-checkbox-element", all=False) response = get_shadow_root(session, custom_element.id) value = assert_success(response) assert isinstance(value, dict) assert "shadow-6066-11e4-a52e-4f735466cecf" in value assert_same_element(session, custom_element, expected)
def test_element_in_object(session): session.url = inline("<div></div>") div = session.find.css("div", all=False) response = execute_script(session, """ let div = document.querySelector("div"); div.reference = div; return {foo: div}; """) value = assert_success(response) assert_same_element(session, div, value["foo"])
def test_xhtml_namespace(session, using, value): session.url = inline("""<p><a href="#" id="linkText">full link text</a></p>""", doctype="xhtml") expected = session.execute_script("return document.links[0]") response = find_elements(session, using, value) value = assert_success(response) assert isinstance(value, list) assert len(value) == 1 found_element = value[0] assert_same_element(session, found_element, expected)
def test_element_in_collection(session, inline): session.url = inline("<div></div>") divs = session.find.css("div") response = execute_script(session, """ let div = document.querySelector("div"); div.reference = div; return [div]; """) value = assert_success(response) for expected, actual in zip(divs, value): assert_same_element(session, expected, actual)
def test_find_element(session, inline, get_shadow_page, using, value): # Step 8 - 9 session.url = inline( get_shadow_page("<div><a href=# id=linkText>full link text</a></div>")) custom_element = session.find.css("custom-shadow-element", all=False) expected = session.execute_script( "return arguments[0].shadowRoot.querySelector('#linkText')", args=(custom_element, )) shadow_root = custom_element.shadow_root response = find_element(session, shadow_root.id, using, value) value = assert_success(response) assert_same_element(session, value, expected)
def test_parent_htmldocument(session, inline): session.url = inline("") from_element = session.execute_script("""return document.querySelector("body")""") expected = session.execute_script("return document.documentElement") response = find_elements(session, from_element.id, "xpath", "..") value = assert_success(response) assert isinstance(value, list) assert len(value) == 1 found_element = value[0] assert_same_element(session, found_element, expected)
def test_find_elements_partial_link_text(session, document, value): # Step 8 - 9 session.url = inline("<a href=#>not wanted</a><br/>{0}".format(document)) expected = session.execute_script("return document.links[1];") response = find_elements(session, "partial link text", value) value = assert_success(response) assert isinstance(value, list) assert len(value) == 1 found_element = value[0] assert_same_element(session, found_element, expected)
def test_xhtml_namespace(session, using, value): session.url = inline("""<p><a href="#" id="linkText">full link text</a></p>""", doctype="xhtml") from_element = session.execute_script("""return document.querySelector("p")""") expected = session.execute_script("return document.links[0]") response = find_elements(session, from_element.id, using, value) value = assert_success(response) assert isinstance(value, list) assert len(value) == 1 found_element = value[0] assert_same_element(session, found_element, expected)
def test_find_element_link_text(session, get_shadow_page, document, value): # Step 8 - 9 session.url = get_shadow_page("<div>{0}</div>".format(document)) custom_element = session.find.css("custom-shadow-element", all=False) expected = session.execute_script( "return arguments[0].shadowRoot.querySelectorAll('a')[0]", args=(custom_element, )) shadow_root = custom_element.shadow_root response = find_element(session, shadow_root.id, "link text", value) assert_success(response) assert_same_element(session, value, expected)
def test_element_in_collection(session): session.url = inline("<div></div>") divs = session.find.css("div") response = execute_script(session, """ let div = document.querySelector("div"); div.reference = div; return [div]; """) value = assert_success(response) for expected, actual in zip(divs, value): assert_same_element(session, expected, actual)
def check_user_prompt_closed_without_exception(dialog_type, retval): session.url = inline("<p>bar</p>") element = session.find.css("p", all=False) create_dialog(dialog_type, text=dialog_type) response = find_element(session, "css selector", "p") value = assert_success(response) assert_dialog_handled(session, expected_text=dialog_type, expected_retval=retval) assert_same_element(session, value, element)
def test_find_elements_link_text(session, inline, document, value): # Step 8 - 9 session.url = inline("<div><a href=#>not wanted</a><br/>{0}</div>".format(document)) element = session.find.css("div", all=False) expected = session.execute_script("return document.links[1];") response = find_elements(session, element.id, "link text", value) value = assert_success(response) assert isinstance(value, list) assert len(value) == 1 found_element = value[0] assert_same_element(session, found_element, expected)
def check_user_prompt_closed_without_exception(dialog_type, retval): session.url = inline("<div><p>bar</p><div>") outer_element = session.find.css("div", all=False) inner_element = session.find.css("p", all=False) create_dialog(dialog_type, text=dialog_type) response = find_element(session, outer_element.id, "css selector", "p") value = assert_success(response) assert_dialog_handled(session, expected_text=dialog_type, expected_retval=retval) assert_same_element(session, value, inner_element)
def test_html_collection(session): session.url = inline(""" <p>foo <p>bar """) ps = session.find.css("p") response = execute_script(session, "return document.getElementsByTagName('p')") value = assert_success(response) assert isinstance(value, list) assert len(value) == 2 for expected, actual in zip(ps, value): assert_same_element(session, expected, actual)
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;") if result.body["value"] is None: assert from_js == None else: assert_same_element(session, result.body["value"], from_js)
def test_html_collection(session, inline): session.url = inline(""" <p>foo <p>bar """) ps = session.find.css("p") response = execute_script(session, "return document.getElementsByTagName('p')") value = assert_success(response) assert isinstance(value, list) assert len(value) == 2 for expected, actual in zip(ps, value): assert_same_element(session, expected, actual)
def test_node_list(session): session.url = inline(""" <p>foo <p>bar """) ps = session.find.css("p") response = execute_script(session, "return document.querySelectorAll('p')") value = assert_success(response) assert isinstance(value, list) assert len(value) == 2 for expected, actual in zip(ps, value): assert_same_element(session, expected, actual)
def test_html_options_collection(session): session.url = inline(""" <select> <option> <option> </select> """) options = session.find.css("option") response = execute_script(session, "return document.querySelector('select').options") value = assert_success(response) assert isinstance(value, list) assert len(value) == 2 for expected, actual in zip(options, value): assert_same_element(session, expected, actual)
def check_user_prompt_closed_without_exception(dialog_type, retval): session.url = inline(get_shadow_page("<div><p>bar</p><div>")) outer_element = session.find.css("custom-shadow-element", all=False) shadow_root = outer_element.shadow_root inner_element = session.execute_script("return arguments[0].shadowRoot.querySelector('p')", args=(outer_element,)) create_dialog(dialog_type, text=dialog_type) response = find_element(session, shadow_root.id, "css selector", "p") value = assert_success(response) assert_dialog_handled(session, expected_text=dialog_type, expected_retval=retval) assert_same_element(session, value, inner_element)
def test_html_form_controls_collection(session): session.url = inline(""" <form> <input> <input> </form> """) inputs = session.find.css("input") response = execute_script(session, "return document.forms[0].elements") value = assert_success(response) assert isinstance(value, list) assert len(value) == 2 for expected, actual in zip(inputs, value): assert_same_element(session, expected, actual)
def check_user_prompt_closed_without_exception(dialog_type, retval): session.url = inline("<div><p>bar</p><div>") outer_element = session.find.css("div", all=False) inner_element = session.find.css("p", all=False) create_dialog(dialog_type, text=dialog_type) response = find_elements(session, outer_element.id, "css selector", "p") value = assert_success(response) assert isinstance(value, list) assert len(value) == 1 assert_dialog_handled(session, expected_text=dialog_type, expected_retval=retval) assert_same_element(session, value[0], inner_element)
def test_html_options_collection(session, inline): session.url = inline(""" <select> <option> <option> </select> """) options = session.find.css("option") response = execute_script(session, "return document.querySelector('select').options") value = assert_success(response) assert isinstance(value, list) assert len(value) == 2 for expected, actual in zip(options, value): assert_same_element(session, expected, actual)
def test_find_elements_link_text(session, get_shadow_page, document, value): # Step 8 - 9 session.url = get_shadow_page("<div><a href=#>not wanted</a><br/>{0}</div>".format(document)) element = session.find.css("div", all=False) custom_element = session.find.css("custom-shadow-element", all=False) shadow_root = custom_element.shadow_root expected = session.execute_script("return arguments[0].shadowRoot.querySelectorAll('a')[1]", args=(custom_element,)) response = find_elements(session, shadow_root.id, "link text", value) value = assert_success(response) assert isinstance(value, list) assert len(value) == 1 found_element = value[0] assert_same_element(session, found_element, expected)
def check_user_prompt_closed_without_exception(dialog_type, retval): session.url = inline("<p>bar</p>") element = session.find.css("p", all=False) create_dialog(dialog_type, text=dialog_type) response = find_elements(session, "css selector", "p") value = assert_success(response) assert isinstance(value, list) assert len(value) == 1 assert_dialog_handled(session, expected_text=dialog_type, expected_retval=retval) assert_same_element(session, value[0], element)
def test_body_is_interactable(session): session.url = inline(""" <body onkeypress="document.getElementById('result').value += event.key"> <input type="text" id="result"/> </body> """) element = session.find.css("body", all=False) result = session.find.css("input", all=False) # By default body is the active element assert_same_element(session, element, session.active_element) response = send_keys_to_element(session, element, "foo") assert_success(response) assert_same_element(session, element, session.active_element) assert result.property("value") == "foo"
def test_idl_attribute_element(session): session.url = inline(""" <p>foo <p>bar <script> const [foo, bar] = document.querySelectorAll("p"); foo.bar = bar; </script> """) _foo, bar = session.find.css("p") response = execute_script(session, """ const foo = document.querySelector("p"); return foo.bar; """) value = assert_success(response) assert_same_element(session, bar, value)
def test_idl_attribute_element(session): session.url = inline(""" <p>foo <p>bar <script> const [foo, bar] = document.querySelectorAll("p"); foo.bar = bar; </script> """) _foo, bar = session.find.css("p") response = execute_script( session, """ const foo = document.querySelector("p"); return foo.bar; """) value = assert_success(response) assert_same_element(session, bar, value)
def test_idl_attribute_element(session): session.url = inline(""" <p>foo <p>bar <script> const elements = document.querySelectorAll("p"); let foo = elements[0]; let bar = elements[1]; foo.bar = bar; </script> """) _foo, bar = session.find.css("p") response = execute_async_script( session, """ const resolve = arguments[0]; const foo = document.querySelector("p"); resolve(foo.bar); """) value = assert_success(response) assert_same_element(session, bar, value)
def test_html_all_collection(session): session.url = inline(""" <p>foo <p>bar """) html = session.find.css("html", all=False) head = session.find.css("head", all=False) body = session.find.css("body", all=False) ps = session.find.css("p") response = execute_script(session, "return document.all") value = assert_success(response) assert isinstance(value, list) # <html>, <head>, <body>, <p>, <p> assert len(value) == 5 assert_same_element(session, html, value[0]) assert_same_element(session, head, value[1]) assert_same_element(session, body, value[2]) assert_same_element(session, ps[0], value[3]) assert_same_element(session, ps[1], value[4])
def test_iframe_is_interactable(session): session.url = inline(iframe(""" <body onkeypress="document.getElementById('result').value += event.key"> <input type="text" id="result"/> </body> """)) body = session.find.css("body", all=False) frame = session.find.css("iframe", all=False) # By default the body has the focus assert_same_element(session, body, session.active_element) response = send_keys_to_element(session, frame, "foo") assert_success(response) assert_same_element(session, frame, session.active_element) # Any key events are immediately routed to the nested # browsing context's active document. session.switch_frame(frame) result = session.find.css("input", all=False) assert result.property("value") == "foo"
def test_frame_id_null(session, inline, iframe): session.url = inline(iframe("{}<div>foo".format(iframe("<p>bar")))) frame1 = session.find.css("iframe", all=False) session.switch_frame(frame1) element1 = session.find.css("div", all=False) frame2 = session.find.css("iframe", all=False) session.switch_frame(frame2) element2 = session.find.css("p", all=False) # Switch to top-level browsing context response = switch_to_frame(session, None) assert_success(response) with pytest.raises(NoSuchElementException): element2.text with pytest.raises(NoSuchElementException): element1.text frame = session.find.css("iframe", all=False) assert_same_element(session, frame, frame1)
def test_html_all_collection(session, inline): session.url = inline(""" <p>foo <p>bar """) html = session.find.css("html", all=False) head = session.find.css("head", all=False) meta = session.find.css("meta", all=False) body = session.find.css("body", all=False) ps = session.find.css("p") response = execute_script(session, "return document.all") value = assert_success(response) assert isinstance(value, list) # <html>, <head>, <meta>, <body>, <p>, <p> assert len(value) == 6 assert_same_element(session, html, value[0]) assert_same_element(session, head, value[1]) assert_same_element(session, meta, value[2]) assert_same_element(session, body, value[3]) assert_same_element(session, ps[0], value[4]) assert_same_element(session, ps[1], value[5])