Unreviewed. Update W3C WebDriver imported tests.
[WebKit-https.git] / WebDriverTests / imported / w3c / webdriver / tests / actions / key.py
1 import pytest
2
3 from tests.actions.support.keys import Keys
4 from tests.actions.support.refine import filter_dict, get_keys, get_events
5
6
7 def test_lone_keyup_sends_no_events(session, key_reporter, key_chain):
8     key_chain.key_up("a").perform()
9     assert len(get_keys(key_reporter)) == 0
10     assert len(get_events(session)) == 0
11     session.actions.release()
12     assert len(get_keys(key_reporter)) == 0
13     assert len(get_events(session)) == 0
14
15
16 @pytest.mark.parametrize("value,code", [
17     (u"a", "KeyA",),
18     ("a", "KeyA",),
19     (u"\"", "Quote"),
20     (u",", "Comma"),
21     (u"\u00E0", ""),
22     (u"\u0416", ""),
23     (u"@", "Digit2"),
24     (u"\u2603", ""),
25     (u"\uF6C2", ""),  # PUA
26 ])
27 def test_single_printable_key_sends_correct_events(session,
28                                                    key_reporter,
29                                                    key_chain,
30                                                    value,
31                                                    code):
32     key_chain \
33         .key_down(value) \
34         .key_up(value) \
35         .perform()
36     expected = [
37         {"code": code, "key": value, "type": "keydown"},
38         {"code": code, "key": value, "type": "keypress"},
39         {"code": code, "key": value, "type": "keyup"},
40     ]
41     all_events = get_events(session)
42     events = [filter_dict(e, expected[0]) for e in all_events]
43     if len(events) > 0 and events[0]["code"] == None:
44         # Remove 'code' entry if browser doesn't support it
45         expected = [filter_dict(e, {"key": "", "type": ""}) for e in expected]
46         events = [filter_dict(e, expected[0]) for e in events]
47     assert events == expected
48     assert get_keys(key_reporter) == value
49
50
51 @pytest.mark.parametrize("value", [
52     (u"\U0001F604"),
53     (u"\U0001F60D"),
54 ])
55 def test_single_emoji_records_correct_key(session, key_reporter, key_chain, value):
56     # Not using key_chain.send_keys() because we always want to treat value as
57     # one character here. `len(value)` varies by platform for non-BMP characters,
58     # so we don't want to iterate over value.
59     key_chain \
60         .key_down(value) \
61         .key_up(value) \
62         .perform()
63     # events sent by major browsers are inconsistent so only check key value
64     assert get_keys(key_reporter) == value
65
66
67 @pytest.mark.parametrize("value,code,key", [
68     (u"\uE050", "ShiftRight", "Shift"),
69     (u"\uE053", "OSRight", "Meta"),
70     (Keys.CONTROL, "ControlLeft", "Control"),
71 ])
72 def test_single_modifier_key_sends_correct_events(session,
73                                                   key_reporter,
74                                                   key_chain,
75                                                   value,
76                                                   code,
77                                                   key):
78     key_chain \
79         .key_down(value) \
80         .key_up(value) \
81         .perform()
82     all_events = get_events(session)
83     expected = [
84         {"code": code, "key": key, "type": "keydown"},
85         {"code": code, "key": key, "type": "keyup"},
86     ]
87     events = [filter_dict(e, expected[0]) for e in all_events]
88     if len(events) > 0 and events[0]["code"] == None:
89         # Remove 'code' entry if browser doesn't support it
90         expected = [filter_dict(e, {"key": "", "type": ""}) for e in expected]
91         events = [filter_dict(e, expected[0]) for e in events]
92     assert events == expected
93     assert len(get_keys(key_reporter)) == 0
94
95
96 @pytest.mark.parametrize("value,code,key", [
97     (Keys.ESCAPE, "Escape", "Escape"),
98     (Keys.RIGHT, "ArrowRight", "ArrowRight"),
99 ])
100 def test_single_nonprintable_key_sends_events(session,
101                                               key_reporter,
102                                               key_chain,
103                                               value,
104                                               code,
105                                               key):
106     key_chain \
107         .key_down(value) \
108         .key_up(value) \
109         .perform()
110     expected = [
111         {"code": code, "key": key, "type": "keydown"},
112         {"code": code, "key": key, "type": "keypress"},
113         {"code": code, "key": key, "type": "keyup"},
114     ]
115     all_events = get_events(session)
116     events = [filter_dict(e, expected[0]) for e in all_events]
117     if len(events) > 0 and events[0]["code"] == None:
118         # Remove 'code' entry if browser doesn't support it
119         expected = [filter_dict(e, {"key": "", "type": ""}) for e in expected]
120         events = [filter_dict(e, expected[0]) for e in events]
121     if len(events) == 2:
122         # most browsers don't send a keypress for non-printable keys
123         assert events == [expected[0], expected[2]]
124     else:
125         assert events == expected
126     assert len(get_keys(key_reporter)) == 0
127
128
129 def test_sequence_of_keydown_printable_keys_sends_events(session,
130                                                          key_reporter,
131                                                          key_chain):
132     key_chain \
133         .key_down("a") \
134         .key_down("b") \
135         .perform()
136     expected = [
137         {"code": "KeyA", "key": "a", "type": "keydown"},
138         {"code": "KeyA", "key": "a", "type": "keypress"},
139         {"code": "KeyB", "key": "b", "type": "keydown"},
140         {"code": "KeyB", "key": "b", "type": "keypress"},
141     ]
142     all_events = get_events(session)
143     events = [filter_dict(e, expected[0]) for e in all_events]
144     if len(events) > 0 and events[0]["code"] == None:
145         # Remove 'code' entry if browser doesn't support it
146         expected = [filter_dict(e, {"key": "", "type": ""}) for e in expected]
147         events = [filter_dict(e, expected[0]) for e in events]
148     assert events == expected
149     assert get_keys(key_reporter) == "ab"
150
151
152 def test_sequence_of_keydown_character_keys(session, key_reporter, key_chain):
153     key_chain.send_keys("ef").perform()
154     expected = [
155         {"code": "KeyE", "key": "e", "type": "keydown"},
156         {"code": "KeyE", "key": "e", "type": "keypress"},
157         {"code": "KeyE", "key": "e", "type": "keyup"},
158         {"code": "KeyF", "key": "f", "type": "keydown"},
159         {"code": "KeyF", "key": "f", "type": "keypress"},
160         {"code": "KeyF", "key": "f", "type": "keyup"},
161     ]
162     all_events = get_events(session)
163     events = [filter_dict(e, expected[0]) for e in all_events]
164     if len(events) > 0 and events[0]["code"] == None:
165         # Remove 'code' entry if browser doesn't support it
166         expected = [filter_dict(e, {"key": "", "type": ""}) for e in expected]
167         events = [filter_dict(e, expected[0]) for e in events]
168     assert events == expected
169     assert get_keys(key_reporter) == "ef"
170
171
172 def test_backspace_erases_keys(session, key_reporter, key_chain):
173     key_chain \
174         .send_keys("efcd") \
175         .send_keys([Keys.BACKSPACE, Keys.BACKSPACE]) \
176         .perform()
177     assert get_keys(key_reporter) == "ef"
178