Skip to content
Permalink
Browse files

Add executing custom command to Chrome Python bindings (#5989)

  • Loading branch information...
zhangwenqiang00 authored and lmtierney committed Jun 25, 2018
1 parent 5fad608 commit 4082b77504a1e13eecae9ea97b6ad304646fc0ea
@@ -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')
@@ -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
@@ -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

0 comments on commit 4082b77

Please sign in to comment.
You can’t perform that action at this time.