Unreviewed. Update W3C WebDriver imported tests.
[WebKit-https.git] / WebDriverTests / imported / w3c / webdriver / tests / element_send_keys / form_controls.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 element_send_keys(session, element, text):
8     return session.transport.send(
9         "POST",
10         "/session/{session_id}/element/{element_id}/value".format(
11             session_id=session.session_id,
12             element_id=element.id),
13         {"text": text})
14
15
16 def add_event_listeners(element):
17     element.session.execute_script("""
18         let [target] = arguments;
19         window.events = [];
20         for (let expected of ["focus", "blur", "change", "keypress", "keydown", "keyup", "input"]) {
21           target.addEventListener(expected, ({type}) => window.events.push(type));
22         }
23         """, args=(element,))
24
25
26 def get_events(session):
27     return session.execute_script("return window.events")
28
29
30 def test_input(session):
31     session.url = inline("<input>")
32     element = session.find.css("input", all=False)
33     assert element.property("value") == ""
34
35     element_send_keys(session, element, "foo")
36     assert element.property("value") == "foo"
37
38
39 def test_textarea(session):
40     session.url = inline("<textarea>")
41     element = session.find.css("textarea", all=False)
42     assert element.property("value") == ""
43
44     element_send_keys(session, element, "foo")
45     assert element.property("value") == "foo"
46
47
48 def test_input_append(session):
49     session.url = inline("<input value=a>")
50     element = session.find.css("input", all=False)
51     assert element.property("value") == "a"
52
53     element_send_keys(session, element, "b")
54     assert element.property("value") == "ab"
55
56     element_send_keys(session, element, "c")
57     assert element.property("value") == "abc"
58
59
60 def test_textarea_append(session):
61     session.url = inline("<textarea>a</textarea>")
62     element = session.find.css("textarea", all=False)
63     assert element.property("value") == "a"
64
65     element_send_keys(session, element, "b")
66     assert element.property("value") == "ab"
67
68     element_send_keys(session, element, "c")
69     assert element.property("value") == "abc"
70
71
72 @pytest.mark.parametrize("tag", ["input", "textarea"])
73 def test_events(session, tag):
74     session.url = inline("<%s>" % tag)
75     element = session.find.css(tag, all=False)
76     add_event_listeners(element)
77
78     element_send_keys(session, element, "foo")
79     assert element.property("value") == "foo"
80     assert get_events(session) == ["focus",
81                                    "keydown",
82                                    "keypress",
83                                    "input",
84                                    "keyup",
85                                    "keydown",
86                                    "keypress",
87                                    "input",
88                                    "keyup",
89                                    "keydown",
90                                    "keypress",
91                                    "input",
92                                    "keyup",
93                                    "change",
94                                    "blur"]