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 (
4     assert_element_has_focus,
5     assert_error,
6     assert_same_element,
7     assert_success,
8 )
9 from tests.support.inline import inline
10
11
12 def send_keys_to_element(session, element, text):
13     return session.transport.send(
14         "POST", "/session/{session_id}/element/{element_id}/value".format(
15             session_id=session.session_id,
16             element_id=element.id),
17         {"text": text})
18
19
20 def add_event_listeners(element):
21     element.session.execute_script("""
22         window.events = [];
23         var trackedEvents = ["focus", "change", "keypress", "keydown", "keyup", "input"];
24         for (var i = 0; i < trackedEvents.length; i++) {
25           arguments[0].addEventListener(trackedEvents[i], function(eventObject) { window.events.push(eventObject.type) });
26         }
27         """, args=(element,))
28
29
30 def get_events(session):
31     return session.execute_script("return window.events")
32
33
34 def test_input(session):
35     session.url = inline("<input>")
36     element = session.find.css("input", all=False)
37     assert element.property("value") == ""
38
39     send_keys_to_element(session, element, "foo")
40     assert element.property("value") == "foo"
41     assert_element_has_focus(element)
42
43
44 def test_textarea(session):
45     session.url = inline("<textarea>")
46     element = session.find.css("textarea", all=False)
47     assert element.property("value") == ""
48
49     send_keys_to_element(session, element, "foo")
50     assert element.property("value") == "foo"
51     assert_element_has_focus(element)
52
53
54 def test_input_append(session):
55     session.url = inline("<input value=a>")
56     element = session.find.css("input", all=False)
57     assert element.property("value") == "a"
58
59     send_keys_to_element(session, element, "b")
60     assert element.property("value") == "ab"
61
62     send_keys_to_element(session, element, "c")
63     assert element.property("value") == "abc"
64
65
66 def test_textarea_append(session):
67     session.url = inline("<textarea>a</textarea>")
68     element = session.find.css("textarea", all=False)
69     assert element.property("value") == "a"
70
71     send_keys_to_element(session, element, "b")
72     assert element.property("value") == "ab"
73
74     send_keys_to_element(session, element, "c")
75     assert element.property("value") == "abc"
76
77
78 @pytest.mark.parametrize("tag", ["input", "textarea"])
79 def test_events(session, tag):
80     session.url = inline("<%s>" % tag)
81     element = session.find.css(tag, all=False)
82     add_event_listeners(element)
83
84     send_keys_to_element(session, element, "foo")
85     assert element.property("value") == "foo"
86     assert get_events(session) == ["focus",
87                                    "keydown",
88                                    "keypress",
89                                    "input",
90                                    "keyup",
91                                    "keydown",
92                                    "keypress",
93                                    "input",
94                                    "keyup",
95                                    "keydown",
96                                    "keypress",
97                                    "input",
98                                    "keyup"]
99
100
101 @pytest.mark.parametrize("tag", ["input", "textarea"])
102 def test_not_blurred(session, tag):
103     session.url = inline("<%s>" % tag)
104     element = session.find.css(tag, all=False)
105
106     send_keys_to_element(session, element, "")
107     assert_element_has_focus(element)