1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from six.moves.urllib.parse import quote
from marionette_driver import By, errors, Wait
from marionette_driver.keys import Keys
from marionette_harness import MarionetteTestCase
def inline(doc):
return "data:text/html;charset=utf-8,{}".format(quote(doc))
class BaseMouseAction(MarionetteTestCase):
def setUp(self):
super(BaseMouseAction, self).setUp()
self.mouse_chain = self.marionette.actions.sequence(
"pointer", "pointer_id", {"pointerType": "mouse"}
)
if self.marionette.session_capabilities["platformName"] == "mac":
self.mod_key = Keys.META
else:
self.mod_key = Keys.CONTROL
def tearDown(self):
self.marionette.actions.release()
super(BaseMouseAction, self).tearDown()
@property
def click_position(self):
return self.marionette.execute_script(
"""
if (window.click_x && window.click_y) {
return {x: window.click_x, y: window.click_y};
}
""",
sandbox=None,
)
def get_element_center_point(self, elem):
# pylint --py3k W1619
return {
"x": elem.rect["x"] + elem.rect["width"] / 2,
"y": elem.rect["y"] + elem.rect["height"] / 2,
}
class TestPointerActions(BaseMouseAction):
def test_click_action(self):
test_html = self.marionette.absolute_url("test.html")
self.marionette.navigate(test_html)
link = self.marionette.find_element(By.ID, "mozLink")
self.mouse_chain.click(element=link).perform()
self.assertEqual(
"Clicked",
self.marionette.execute_script(
"return document.getElementById('mozLink').innerHTML"
),
)
def test_clicking_element_out_of_view(self):
self.marionette.navigate(
inline(
"""
<div style="position:relative;top:200vh;">foo</div>
"""
)
)
el = self.marionette.find_element(By.TAG_NAME, "div")
with self.assertRaises(errors.MoveTargetOutOfBoundsException):
self.mouse_chain.click(element=el).perform()
def test_double_click_action(self):
self.marionette.navigate(
inline(
"""
<script>window.eventCount = 0;</script>
<button onclick="window.eventCount++">foobar</button>
"""
)
)
el = self.marionette.find_element(By.CSS_SELECTOR, "button")
self.mouse_chain.click(el).pause(100).click(el).perform()
event_count = self.marionette.execute_script(
"return window.eventCount", sandbox=None
)
self.assertEqual(event_count, 2)
def test_context_click_action(self):
test_html = self.marionette.absolute_url("clicks.html")
self.marionette.navigate(test_html)
click_el = self.marionette.find_element(By.ID, "normal")
def context_menu_state():
with self.marionette.using_context("chrome"):
cm_el = self.marionette.find_element(By.ID, "contentAreaContextMenu")
return cm_el.get_property("state")
self.assertEqual("closed", context_menu_state())
self.mouse_chain.click(element=click_el, button=2).perform()
Wait(self.marionette).until(
lambda _: context_menu_state() == "open",
message="Context menu did not open",
)
with self.marionette.using_context("chrome"):
cm_el = self.marionette.find_element(By.ID, "contentAreaContextMenu")
self.marionette.execute_script(
"arguments[0].hidePopup()", script_args=(cm_el,)
)
Wait(self.marionette).until(
lambda _: context_menu_state() == "closed",
message="Context menu did not close",
)
def test_middle_click_action(self):
test_html = self.marionette.absolute_url("clicks.html")
self.marionette.navigate(test_html)
self.marionette.find_element(By.ID, "addbuttonlistener").click()
el = self.marionette.find_element(By.ID, "showbutton")
self.mouse_chain.click(element=el, button=1).perform()
Wait(self.marionette).until(
lambda _: el.get_property("innerHTML") == "1",
message="Middle-click hasn't been fired",
)
class TestNonSpecCompliantPointerOrigin(BaseMouseAction):
def setUp(self):
super(TestNonSpecCompliantPointerOrigin, self).setUp()
self.marionette.delete_session()
self.marionette.start_session({"moz:useNonSpecCompliantPointerOrigin": True})
def tearDown(self):
self.marionette.delete_session()
self.marionette.start_session()
super(TestNonSpecCompliantPointerOrigin, self).tearDown()
def test_click_element_smaller_than_viewport(self):
self.marionette.navigate(
inline(
"""
<div id="div" style="width: 10vw; height: 10vh; background: green;"
onclick="window.click_x = event.clientX; window.click_y = event.clientY" />
"""
)
)
elem = self.marionette.find_element(By.ID, "div")
elem_center_point = self.get_element_center_point(elem)
self.mouse_chain.click(element=elem).perform()
click_position = Wait(self.marionette).until(
lambda _: self.click_position, message="No click event has been detected"
)
self.assertAlmostEqual(click_position["x"], elem_center_point["x"], delta=1)
self.assertAlmostEqual(click_position["y"], elem_center_point["y"], delta=1)
def test_click_element_larger_than_viewport_with_center_point_inside(self):
self.marionette.navigate(
inline(
"""
<div id="div" style="width: 150vw; height: 150vh; background: green;"
onclick="window.click_x = event.clientX; window.click_y = event.clientY" />
"""
)
)
elem = self.marionette.find_element(By.ID, "div")
elem_center_point = self.get_element_center_point(elem)
self.mouse_chain.click(element=elem).perform()
click_position = Wait(self.marionette).until(
lambda _: self.click_position, message="No click event has been detected"
)
self.assertAlmostEqual(click_position["x"], elem_center_point["x"], delta=1)
self.assertAlmostEqual(click_position["y"], elem_center_point["y"], delta=1)
def test_click_element_larger_than_viewport_with_center_point_outside(self):
self.marionette.navigate(
inline(
"""
<div id="div" style="width: 300vw; height: 300vh; background: green;"
onclick="window.click_x = event.clientX; window.click_y = event.clientY" />
"""
)
)
elem = self.marionette.find_element(By.ID, "div")
with self.assertRaises(errors.MoveTargetOutOfBoundsException):
self.mouse_chain.click(element=elem).perform()
|