Unreviewed. Update W3C WebDriver imported tests.
[WebKit-https.git] / WebDriverTests / imported / w3c / webdriver / tests / interaction / element_clear.py
1 import pytest
2 from tests.support.asserts import assert_error, assert_success
3 from tests.support.inline import inline
4
5
6 def clear(session, element):
7     return session.transport.send("POST", "session/{session_id}/element/{element_id}/clear"
8                                   .format(session_id=session.session_id,
9                                           element_id=element.id))
10
11
12 # 14.2 Element Clear
13
14 def test_no_browsing_context(session, create_window):
15     # 14.2 step 1
16     session.url = inline("<p>This is not an editable paragraph.")
17     element = session.find.css("p", all=False)
18
19     session.window_handle = create_window()
20     session.close()
21
22     response = clear(session, element)
23     assert_error(response, "no such window")
24
25
26 def test_element_not_found(session):
27     # 14.2 Step 2
28     response = session.transport.send("POST", "session/{session_id}/element/{element_id}/clear"
29                                       .format(session_id=session.session_id,
30                                               element_id="box1"))
31
32     assert_error(response, "no such element")
33
34
35 def test_element_not_editable(session):
36     # 14.2 Step 3
37     session.url = inline("<p>This is not an editable paragraph.")
38
39     element = session.find.css("p", all=False)
40     response = clear(session, element)
41     assert_error(response, "invalid element state")
42
43
44 def test_button_element_not_resettable(session):
45     # 14.2 Step 3
46     session.url = inline("<input type=button value=Federer>")
47
48     element = session.find.css("input", all=False)
49     response = clear(session, element)
50     assert_error(response, "invalid element state")
51
52
53 def test_disabled_element_not_resettable(session):
54     # 14.2 Step 3
55     session.url = inline("<input type=text value=Federer disabled>")
56
57     element = session.find.css("input", all=False)
58     response = clear(session, element)
59     assert_error(response, "invalid element state")
60
61
62 def test_scroll_into_element_view(session):
63     # 14.2 Step 4
64     session.url = inline("<input type=text value=Federer><div style= \"height: 200vh; width: 5000vh\">")
65
66     # Scroll to the bottom right of the page
67     session.execute_script("window.scrollTo(document.body.scrollWidth, document.body.scrollHeight);")
68     element = session.find.css("input", all=False)
69     # Clear and scroll back to the top of the page
70     response = clear(session, element)
71     assert_success(response)
72
73     # Check if element cleared is scrolled into view
74     rect = session.execute_script("return document.getElementsByTagName(\"input\")[0].getBoundingClientRect()")
75
76     pageDict = {}
77
78     pageDict["innerHeight"] = session.execute_script("return window.innerHeight")
79     pageDict["innerWidth"] = session.execute_script("return window.innerWidth")
80     pageDict["pageXOffset"] = session.execute_script("return window.pageXOffset")
81     pageDict["pageYOffset"] = session.execute_script("return window.pageYOffset")
82
83     assert rect["top"] < (pageDict["innerHeight"] + pageDict["pageYOffset"]) and \
84            rect["left"] < (pageDict["innerWidth"] + pageDict["pageXOffset"]) and \
85            (rect["top"] + element.rect["height"]) > pageDict["pageYOffset"] and \
86            (rect["left"] + element.rect["width"]) > pageDict["pageXOffset"]
87
88
89 # TODO
90 # Any suggestions on implementation?
91 # def test_session_implicit_wait_timeout(session):
92     # 14.2 Step 5
93
94 # TODO
95 # Any suggestions on implementation?
96 # def test_element_not_interactable(session):
97 #     # 14.2 Step 6
98 #     assert_error(response, "element not interactable")
99
100
101 def test_element_readonly(session):
102     # 14.2 Step 7
103     session.url = inline("<input type=text readonly value=Federer>")
104
105     element = session.find.css("input", all=False)
106     response = clear(session, element)
107     assert_error(response, "invalid element state")
108
109
110 def test_element_disabled(session):
111     # 14.2 Step 7
112     session.url = inline("<input type=text disabled value=Federer>")
113
114     element = session.find.css("input", all=False)
115     response = clear(session, element)
116     assert_error(response, "invalid element state")
117
118
119 def test_element_pointer_events_disabled(session):
120     # 14.2 Step 7
121     session.url = inline("<input type=text value=Federer style=\"pointer-events: none\">")
122
123     element = session.find.css("input", all=False)
124     response = clear(session, element)
125     assert_error(response, "invalid element state")
126
127
128 @pytest.mark.parametrize("element", [["text", "<input id=text type=text value=\"Federer\"><input id=empty type=text value=\"\">"],
129                                     ["search", "<input id=search type=search value=\"Federer\"><input id=empty type=search value=\"\">"],
130                                     ["url", "<input id=url type=url value=\"www.hello.com\"><input id=empty type=url value=\"\">"],
131                                     ["tele", "<input id=tele type=telephone value=\"2061234567\"><input id=empty type=telephone value=\"\">"],
132                                     ["email", "<input id=email type=email value=\"hello@world.com\"><input id=empty type=email value=\"\">"],
133                                     ["password", "<input id=password type=password value=\"pass123\"><input id=empty type=password value=\"\">"],
134                                     ["date", "<input id=date type=date value=\"2017-12-25\"><input id=empty type=date value=\"\">"],
135                                     ["time", "<input id=time type=time value=\"11:11\"><input id=empty type=time value=\"\">"],
136                                     ["number", "<input id=number type=number value=\"19\"><input id=empty type=number value=\"\">"],
137                                     ["range", "<input id=range type=range min=\"0\" max=\"10\"><input id=empty type=range value=\"\">"],
138                                     ["color", "<input id=color type=color value=\"#ff0000\"><input id=empty type=color value=\"\">"],
139                                     ["file", "<input id=file type=file value=\"C:\\helloworld.txt\"><input id=empty type=file value=\"\">"],
140                                     ["textarea", "<textarea id=textarea>Hello World</textarea><textarea id=empty></textarea>"],
141                                     ["sel", "<select id=sel><option></option><option>a</option><option>b</option></select><select id=empty><option></option></select>"],
142                                     ["out", "<output id=out value=100></output><output id=empty></output>"],
143                                     ["para", "<p id=para contenteditable=true>This is an editable paragraph.</p><p id=empty contenteditable=true></p>"]])
144
145 def test_clear_content_editable_resettable_element(session, element):
146     # 14.2 Step 8
147     url = element[1] + """<input id=focusCheck type=checkbox>
148                     <input id=blurCheck type=checkbox>
149                     <script>
150                     var id = %s
151                     document.getElementById("id").addEventListener("focus", checkFocus);
152                     document.getElementById("id").addEventListener("blur", checkBlur);
153                     document.getElementById("empty").addEventListener("focus", checkFocus);
154                     document.getElementById("empty").addEventListener("blur", checkBlur);
155
156                     function checkFocus() {
157                         document.getElementById("focusCheck").checked = true;
158                     }
159                     function checkBlur() {
160                         document.getElementById("blurCheck").checked = true;
161                     }
162                     </script>""" % element[0]
163     session.url = inline(url)
164     # Step 1
165     empty_element = session.find.css("#empty", all=False)
166     test_clear_element_helper(session, empty_element, False)
167     session.execute_script("document.getElementById(\"focusCheck\").checked = false;")
168     session.execute_script("document.getElementById(\"blurCheck\").checked = false;")
169     # Step 2 - 4
170     test_element = session.find.css("#" + element[0], all=False)
171     test_clear_element_helper(session, test_element, True)
172
173
174 def test_clear_element_helper(session, element, value):
175     response = clear(session, element)
176     assert_success(response)
177     response = session.execute_script("return document.getElementById(\"focusCheck\").checked;")
178     assert response is value
179     response = session.execute_script("return document.getElementById(\"blurCheck\").checked;")
180     assert response is value
181     if element.name == "p":
182         response = session.execute_script("return document.getElementById(\"para\").innerHTML;")
183         assert response == ""
184     else:
185         assert element.property("value") == ""