ee2d13faf4eff4aabe75a4d21ff3705792642c7b
[WebKit-https.git] / WebDriverTests / imported / w3c / webdriver / tests / actions / special_keys.py
1 # META: timeout=long
2
3 import pytest
4 from tests.actions.support.keys import ALL_EVENTS, Keys
5 from tests.actions.support.refine import filter_dict, get_keys, get_events
6
7
8 @pytest.mark.parametrize("name,expected", ALL_EVENTS.items())
9 def test_webdriver_special_key_sends_keydown(session,
10                                              key_reporter,
11                                              key_chain,
12                                              name,
13                                              expected):
14     if name.startswith("F"):
15         # Prevent default behavior for F1, etc., but only after keydown
16         # bubbles up to body. (Otherwise activated browser menus/functions
17         # may interfere with subsequent tests.)
18         session.execute_script("""
19             document.body.addEventListener("keydown",
20                     (e) => e.preventDefault());
21         """)
22     key_chain.key_down(getattr(Keys, name)).perform()
23     # only interested in keydown
24     first_event = get_events(session)[0]
25     # make a copy so we can throw out irrelevant keys and compare to events
26     expected = dict(expected)
27
28     del expected["value"]
29     # check and remove keys that aren't in expected
30     assert first_event["type"] == "keydown"
31     assert first_event["repeat"] == False
32     first_event = filter_dict(first_event, expected)
33     assert first_event == expected
34     # only printable characters should be recorded in input field
35     entered_keys = get_keys(key_reporter)
36     if len(expected["key"]) == 1:
37         assert entered_keys == expected["key"]
38     else:
39         assert len(entered_keys) == 0