Skip to content

Commit

Permalink
Merge pull request #654 from MaryamHuntsperger/mouseClick
Browse files Browse the repository at this point in the history
Add trusted mouseClick command to WPT
  • Loading branch information
lbartoli79 authored Oct 10, 2024
2 parents a6a7b82 + 5a9a3e2 commit 2bfce8b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
28 changes: 28 additions & 0 deletions internal/devtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,35 @@ def type_text(self, string):
self.send_character(char)
except Exception:
logging.exception('Error running type command')
def mouse_press(self, command_options):
"""Press down the mouse"""
params = {
'type': 'mousePressed',
'x': command_options['x'],
'y': command_options['y'],
'button': command_options['button'],
'clickCount': command_options['clickCount']
}
self.send_command('Input.dispatchMouseEvent', params)

def mouse_release(self, command_options):
"""Let up the mouse"""
self.send_command('Input.dispatchMouseEvent', {
'type': 'mouseReleased',
'x': command_options['x'],
'y': command_options['y'],
'button': command_options['button'],
'clickCount': command_options['clickCount']
})

def mouse_click(self, params):
"""Simulate pressing the mouse"""
try:
self.mouse_press(params)
self.mouse_release(params)
except Exception:
logging.exception('Error running mouse click command')

def enable_target(self, target_id=None):
"""Hook up the necessary network (or other) events for the given target"""
try:
Expand Down
37 changes: 37 additions & 0 deletions internal/devtools_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,43 @@ def process_command(self, command):
self.devtools.type_text(command['target'])
elif command['command'] == 'keypress':
self.devtools.keypress(command['target'])
elif command['command'] == 'mouseClick':
if 'target' in command:
target = command['target']
separator = target.find('=')
if separator == -1:
separator = target.find("'")
if separator >= 0:
attribute = target[:separator]
attr_value = target[separator + 1:]
try:
query = "JSON.stringify(document.querySelector('[{0}=\"{1}\"]').getBoundingClientRect())".format(
attribute, attr_value)
resp = self.devtools.execute_js(query, use_execution_context = True)
resp_json = json.loads(resp)

value = command['value']
button = 'left'
clickCount = 1
if value in ['left', 'right']:
button = value
elif value == 'double':
clickCount = 2
elif value is not None:
logging.info("Click type is not defined.")

if 'x' in resp_json and 'y' in resp_json and 'width' in resp_json and 'height' in resp_json:
x = int(float(resp_json['x'])) + int(float(resp_json['width']))/2
y = int(float(resp_json['y'])) + int(float(resp_json['height']))/2
command_options = {}
command_options['x'] = x
command_options['y'] = y
command_options['button'] = button
command_options['clickCount'] = clickCount
self.devtools.mouse_click(command_options)
except:
self.task['error'] = 'Exception parsing mouseClick arguments.'
logging.error(self.task['error'])
elif command['command'] == 'waitfor':
try:
self.devtools.wait_for_script = command['target'] if command['target'] else None
Expand Down

0 comments on commit 2bfce8b

Please sign in to comment.