Skip to content

Commit 41f9cfd

Browse files
author
Maryam Huntsperger
committed
Add trusted mouseClick command to WPT
1 parent ad42151 commit 41f9cfd

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-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, commandOptions):
1364+
"""Press down the mouse"""
1365+
params = {
1366+
'type': 'mousePressed',
1367+
'x': commandOptions['x'],
1368+
'y': commandOptions['y'],
1369+
'button': commandOptions['button'],
1370+
'clickCount': commandOptions['clickCount']
1371+
}
1372+
self.send_command('Input.dispatchMouseEvent', params)
1373+
1374+
def mouse_release(self, commandOptions):
1375+
"""Let up the mouse"""
1376+
self.send_command('Input.dispatchMouseEvent', {
1377+
'type': 'mouseReleased',
1378+
'x': commandOptions['x'],
1379+
'y': commandOptions['y'],
1380+
'button': commandOptions['button'],
1381+
'clickCount': commandOptions['clickCount']
1382+
})
13631383

1384+
def mouseClick(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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,46 @@ 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+
mouseclickError = False
779+
try:
780+
query = "JSON.stringify(document.querySelector('[{0}=\"{1}\"]').getBoundingClientRect())".format(
781+
attribute, attr_value)
782+
resp = self.devtools.execute_js(query, use_execution_context = True)
783+
resp_json = json.loads(resp)
784+
785+
value = command['value']
786+
button = 'left'
787+
clickCount = 1
788+
if value in ['left', 'right']:
789+
button = value
790+
elif value == 'double':
791+
clickCount = 2
792+
elif value is not None:
793+
logging.info("Click type is not defined.")
794+
mouseclickError = True
795+
796+
if 'x' in resp_json and 'y' in resp_json and 'width' in resp_json and 'height' in resp_json:
797+
x = int(float(resp_json['x'])) + int(float(resp_json['width']))/2
798+
y = int(float(resp_json['y'])) + int(float(resp_json['height']))/2
799+
commandOptions = {}
800+
commandOptions['x'] = x
801+
commandOptions['y'] = y
802+
commandOptions['button'] = button
803+
commandOptions['clickCount'] = clickCount
804+
self.devtools.mouseClick(commandOptions)
805+
except:
806+
self.task['error'] = 'Exception parsing mouseClick arguments.'
807+
logging.error(self.task['error'])
808+
mouseclickError = True
769809
elif command['command'] == 'waitfor':
770810
try:
771811
self.devtools.wait_for_script = command['target'] if command['target'] else None

0 commit comments

Comments
 (0)