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