Skip to content

Commit 2bfce8b

Browse files
authored
Merge pull request #654 from MaryamHuntsperger/mouseClick
Add trusted mouseClick command to WPT
2 parents a6a7b82 + 5a9a3e2 commit 2bfce8b

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

internal/devtools.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,35 @@ def type_text(self, string):
13601360
self.send_character(char)
13611361
except Exception:
13621362
logging.exception('Error running type command')
1363+
def mouse_press(self, command_options):
1364+
"""Press down the mouse"""
1365+
params = {
1366+
'type': 'mousePressed',
1367+
'x': command_options['x'],
1368+
'y': command_options['y'],
1369+
'button': command_options['button'],
1370+
'clickCount': command_options['clickCount']
1371+
}
1372+
self.send_command('Input.dispatchMouseEvent', params)
1373+
1374+
def mouse_release(self, command_options):
1375+
"""Let up the mouse"""
1376+
self.send_command('Input.dispatchMouseEvent', {
1377+
'type': 'mouseReleased',
1378+
'x': command_options['x'],
1379+
'y': command_options['y'],
1380+
'button': command_options['button'],
1381+
'clickCount': command_options['clickCount']
1382+
})
13631383

1384+
def mouse_click(self, params):
1385+
"""Simulate pressing the mouse"""
1386+
try:
1387+
self.mouse_press(params)
1388+
self.mouse_release(params)
1389+
except Exception:
1390+
logging.exception('Error running mouse click command')
1391+
13641392
def enable_target(self, target_id=None):
13651393
"""Hook up the necessary network (or other) events for the given target"""
13661394
try:

internal/devtools_browser.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,43 @@ def process_command(self, command):
766766
self.devtools.type_text(command['target'])
767767
elif command['command'] == 'keypress':
768768
self.devtools.keypress(command['target'])
769+
elif command['command'] == 'mouseClick':
770+
if 'target' in command:
771+
target = command['target']
772+
separator = target.find('=')
773+
if separator == -1:
774+
separator = target.find("'")
775+
if separator >= 0:
776+
attribute = target[:separator]
777+
attr_value = target[separator + 1:]
778+
try:
779+
query = "JSON.stringify(document.querySelector('[{0}=\"{1}\"]').getBoundingClientRect())".format(
780+
attribute, attr_value)
781+
resp = self.devtools.execute_js(query, use_execution_context = True)
782+
resp_json = json.loads(resp)
783+
784+
value = command['value']
785+
button = 'left'
786+
clickCount = 1
787+
if value in ['left', 'right']:
788+
button = value
789+
elif value == 'double':
790+
clickCount = 2
791+
elif value is not None:
792+
logging.info("Click type is not defined.")
793+
794+
if 'x' in resp_json and 'y' in resp_json and 'width' in resp_json and 'height' in resp_json:
795+
x = int(float(resp_json['x'])) + int(float(resp_json['width']))/2
796+
y = int(float(resp_json['y'])) + int(float(resp_json['height']))/2
797+
command_options = {}
798+
command_options['x'] = x
799+
command_options['y'] = y
800+
command_options['button'] = button
801+
command_options['clickCount'] = clickCount
802+
self.devtools.mouse_click(command_options)
803+
except:
804+
self.task['error'] = 'Exception parsing mouseClick arguments.'
805+
logging.error(self.task['error'])
769806
elif command['command'] == 'waitfor':
770807
try:
771808
self.devtools.wait_for_script = command['target'] if command['target'] else None

0 commit comments

Comments
 (0)