Unreviewed. Update W3C WebDriver imported tests.
[WebKit-https.git] / WebDriverTests / imported / w3c / webdriver / tests / actions / mouse.py
1 import pytest
2
3 from tests.actions.support.mouse import get_center
4 from tests.actions.support.refine import get_events, filter_dict
5 from tests.support.asserts import assert_move_to_coordinates
6 from tests.support.inline import inline
7 from tests.support.wait import wait
8
9
10 def link_doc(dest):
11     content = "<a href=\"{}\" id=\"link\">destination</a>".format(dest)
12     return inline(content)
13
14
15 # TODO use pytest.approx once we upgrade to pytest > 3.0
16 def approx(n, m, tolerance=1):
17     return abs(n - m) <= tolerance
18
19
20 def test_click_at_coordinates(session, test_actions_page, mouse_chain):
21     div_point = {
22         "x": 82,
23         "y": 187,
24     }
25     mouse_chain \
26         .pointer_move(div_point["x"], div_point["y"], duration=1000) \
27         .click() \
28         .perform()
29     events = get_events(session)
30     assert len(events) == 4
31     assert_move_to_coordinates(div_point, "outer", events)
32     for e in events:
33         if e["type"] != "mousedown":
34             assert e["buttons"] == 0
35         assert e["button"] == 0
36     expected = [
37         {"type": "mousedown", "buttons": 1},
38         {"type": "mouseup",  "buttons": 0},
39         {"type": "click", "buttons": 0},
40     ]
41     filtered_events = [filter_dict(e, expected[0]) for e in events]
42     assert expected == filtered_events[1:]
43
44
45 def test_context_menu_at_coordinates(session, test_actions_page, mouse_chain):
46     div_point = {
47         "x": 82,
48         "y": 187,
49     }
50     mouse_chain \
51         .pointer_move(div_point["x"], div_point["y"]) \
52         .pointer_down(button=2) \
53         .pointer_up(button=2) \
54         .perform()
55     events = get_events(session)
56     expected = [
57         {"type": "mousedown", "button": 2},
58         {"type": "contextmenu",  "button": 2},
59     ]
60     assert len(events) == 4
61     filtered_events = [filter_dict(e, expected[0]) for e in events]
62     mousedown_contextmenu_events = [
63         x for x in filtered_events
64         if x["type"] in ["mousedown", "contextmenu"]
65     ]
66     assert expected == mousedown_contextmenu_events
67
68
69 def test_click_element_center(session, test_actions_page, mouse_chain):
70     outer = session.find.css("#outer", all=False)
71     center = get_center(outer.rect)
72     mouse_chain.click(element=outer).perform()
73     events = get_events(session)
74     assert len(events) == 4
75     event_types = [e["type"] for e in events]
76     assert ["mousemove", "mousedown", "mouseup", "click"] == event_types
77     for e in events:
78         if e["type"] != "mousemove":
79             assert approx(e["pageX"], center["x"])
80             assert approx(e["pageY"], center["y"])
81             assert e["target"] == "outer"
82
83
84 def test_click_navigation(session, url, release_actions):
85     destination = url("/webdriver/tests/actions/support/test_actions_wdspec.html")
86     start = link_doc(destination)
87
88     def click(link):
89         mouse_chain = session.actions.sequence(
90             "pointer", "pointer_id", {"pointerType": "mouse"})
91         mouse_chain.click(element=link).perform()
92
93     session.url = start
94     error_message = "Did not navigate to %s" % destination
95
96     click(session.find.css("#link", all=False))
97     wait(session, lambda s: s.url == destination, error_message)
98     # repeat steps to check behaviour after document unload
99     session.url = start
100     click(session.find.css("#link", all=False))
101     wait(session, lambda s: s.url == destination, error_message)
102
103
104 @pytest.mark.parametrize("drag_duration", [0, 300, 800])
105 @pytest.mark.parametrize("dx, dy",
106     [(20, 0), (0, 15), (10, 15), (-20, 0), (10, -15), (-10, -15)])
107 def test_drag_and_drop(session,
108                        test_actions_page,
109                        mouse_chain,
110                        dx,
111                        dy,
112                        drag_duration):
113     drag_target = session.find.css("#dragTarget", all=False)
114     initial_rect = drag_target.rect
115     initial_center = get_center(initial_rect)
116     # Conclude chain with extra move to allow time for last queued
117     # coordinate-update of drag_target and to test that drag_target is "dropped".
118     mouse_chain \
119         .pointer_move(0, 0, origin=drag_target) \
120         .pointer_down() \
121         .pointer_move(dx, dy, duration=drag_duration, origin="pointer") \
122         .pointer_up() \
123         .pointer_move(80, 50, duration=100, origin="pointer") \
124         .perform()
125     # mouseup that ends the drag is at the expected destination
126     e = get_events(session)[1]
127     assert e["type"] == "mouseup"
128     assert approx(e["pageX"], initial_center["x"] + dx)
129     assert approx(e["pageY"], initial_center["y"] + dy)
130     # check resulting location of the dragged element
131     final_rect = drag_target.rect
132     assert initial_rect["x"] + dx == final_rect["x"]
133     assert initial_rect["y"] + dy == final_rect["y"]