Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add executing custom command to Chrome Python bindings #5989

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions py/selenium/webdriver/chrome/remote_connection.py
Expand Up @@ -25,3 +25,4 @@ def __init__(self, remote_server_addr, keep_alive=True):
self._commands["launchApp"] = ('POST', '/session/$sessionId/chromium/launch_app')
self._commands["setNetworkConditions"] = ('POST', '/session/$sessionId/chromium/network_conditions')
self._commands["getNetworkConditions"] = ('GET', '/session/$sessionId/chromium/network_conditions')
self._commands['executeCdpCommand'] = ('POST', '/session/$sessionId/goog/cdp/execute')
23 changes: 23 additions & 0 deletions py/selenium/webdriver/chrome/webdriver.py
Expand Up @@ -115,6 +115,29 @@ def set_network_conditions(self, **network_conditions):
'network_conditions': network_conditions
})

def execute_cdp_cmd(self, cmd, cmd_args):
"""
Execute Chrome Devtools Protocol command and get returned result

The command and command args should follow chrome devtools protocol domains/commands, refer to link
https://chromedevtools.github.io/devtools-protocol/

:Args:
- cmd: A str, command name
- cmd_args: A dict, command args. empty dict {} if there is no command args

:Usage:
driver.execute_cdp_cmd('Network.getResponseBody', {'requestId': requestId})

:Returns:
A dict, empty dict {} if there is no result to return.
For example to getResponseBody:

{'base64Encoded': False, 'body': 'response body string'}

"""
return self.execute("executeCdpCommand", {'cmd': cmd, 'params': cmd_args})['value']

def quit(self):
"""
Closes the browser and shuts down the ChromeDriver executable
Expand Down
25 changes: 25 additions & 0 deletions py/test/selenium/webdriver/chrome/chrome_execute_cdp_cmd_tests.py
@@ -0,0 +1,25 @@
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from selenium.webdriver import Chrome


def test_execute_cdp_cmd():
driver = Chrome()
version_info = driver.execute_cdp_cmd('Browser.getVersion', {})
assert isinstance(version_info, dict)
assert 'userAgent' in version_info