Unreviewed. Update W3C WebDriver imported tests.
[WebKit-https.git] / WebDriverTests / imported / w3c / webdriver / tests / retrieval / find_elements.py
1 import pytest
2
3 from tests.support.asserts import assert_error, assert_same_element, assert_success
4 from tests.support.inline import inline
5
6
7 def find_elements(session, using, value):
8     return session.transport.send("POST",
9                                   "session/%s/elements" % session.session_id,
10                                   {"using": using, "value": value})
11
12
13 @pytest.mark.parametrize("using", ["a", True, None, 1, [], {}])
14 def test_invalid_using_argument(session, using):
15     # Step 1 - 2
16     response = find_elements(session, using, "value")
17     assert_error(response, "invalid argument")
18
19
20 @pytest.mark.parametrize("value", [None, [], {}])
21 def test_invalid_selector_argument(session, value):
22     # Step 3 - 4
23     response = find_elements(session, "css selector", value)
24     assert_error(response, "invalid argument")
25
26
27 def test_closed_context(session, create_window):
28     # Step 5
29     new_window = create_window()
30     session.window_handle = new_window
31     session.close()
32
33     response = session.transport.send("POST",
34                                       "session/%s/elements" % session.session_id,
35                                       {"using": "css selector", "value": "foo"})
36
37     assert_error(response, "no such window")
38
39
40 @pytest.mark.parametrize("using,value",
41                          [("css selector", "#linkText"),
42                           ("link text", "full link text"),
43                           ("partial link text", "link text"),
44                           ("tag name", "a"),
45                           ("xpath", "//a")])
46 def test_find_elements(session, using, value):
47     # Step 8 - 9
48     session.url = inline("<a href=# id=linkText>full link text</a>")
49
50     response = find_elements(session, using, value)
51     assert_success(response)
52     assert len(response.body["value"]) == 1
53
54
55 @pytest.mark.parametrize("using,value", [("css selector", "#wontExist")])
56 def test_no_element(session, using, value):
57     # Step 8 - 9
58     response = find_elements(session, using, value)
59     assert_success(response)
60     assert response.body["value"] == []
61
62
63 @pytest.mark.parametrize("using,value",
64                          [("css selector", "#linkText"),
65                           ("link text", "full link text"),
66                           ("partial link text", "link text"),
67                           ("tag name", "a"),
68                           ("xpath", "//*[name()='a']")])
69 def test_xhtml_namespace(session, using, value):
70     session.url = inline("""<p><a href="#" id="linkText">full link text</a></p>""", doctype="xhtml")
71     expected = session.execute_script("return document.links[0]")
72
73     response = find_elements(session, using, value)
74     value = assert_success(response)
75     assert isinstance(value, list)
76     assert len(value) == 1
77
78     found_element = value[0]
79     assert_same_element(session, found_element, expected)