moving to scripts
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
# 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.
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,176 @@
|
||||
# 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.
|
||||
|
||||
import base64
|
||||
import os
|
||||
from typing import List, NoReturn, Union
|
||||
|
||||
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
||||
from selenium.webdriver.common.options import ArgOptions
|
||||
|
||||
|
||||
class ChromiumOptions(ArgOptions):
|
||||
KEY = "goog:chromeOptions"
|
||||
|
||||
def __init__(self):
|
||||
super(ChromiumOptions, self).__init__()
|
||||
self._binary_location = ''
|
||||
self._extension_files = []
|
||||
self._extensions = []
|
||||
self._experimental_options = {}
|
||||
self._debugger_address = None
|
||||
|
||||
@property
|
||||
def binary_location(self) -> str:
|
||||
"""
|
||||
:Returns: The location of the binary, otherwise an empty string
|
||||
"""
|
||||
return self._binary_location
|
||||
|
||||
@binary_location.setter
|
||||
def binary_location(self, value: str):
|
||||
"""
|
||||
Allows you to set where the chromium binary lives
|
||||
:Args:
|
||||
- value: path to the Chromium binary
|
||||
"""
|
||||
self._binary_location = value
|
||||
|
||||
@property
|
||||
def debugger_address(self: str):
|
||||
"""
|
||||
:Returns: The address of the remote devtools instance
|
||||
"""
|
||||
return self._debugger_address
|
||||
|
||||
@debugger_address.setter
|
||||
def debugger_address(self, value: str):
|
||||
"""
|
||||
Allows you to set the address of the remote devtools instance
|
||||
that the ChromeDriver instance will try to connect to during an
|
||||
active wait.
|
||||
:Args:
|
||||
- value: address of remote devtools instance if any (hostname[:port])
|
||||
"""
|
||||
self._debugger_address = value
|
||||
|
||||
@property
|
||||
def extensions(self) -> List[str]:
|
||||
"""
|
||||
:Returns: A list of encoded extensions that will be loaded
|
||||
"""
|
||||
encoded_extensions = []
|
||||
for ext in self._extension_files:
|
||||
file_ = open(ext, 'rb')
|
||||
# Should not use base64.encodestring() which inserts newlines every
|
||||
# 76 characters (per RFC 1521). Chromedriver has to remove those
|
||||
# unnecessary newlines before decoding, causing performance hit.
|
||||
encoded_extensions.append(base64.b64encode(file_.read()).decode('UTF-8'))
|
||||
|
||||
file_.close()
|
||||
return encoded_extensions + self._extensions
|
||||
|
||||
def add_extension(self, extension: str) -> NoReturn:
|
||||
"""
|
||||
Adds the path to the extension to a list that will be used to extract it
|
||||
to the ChromeDriver
|
||||
|
||||
:Args:
|
||||
- extension: path to the \\*.crx file
|
||||
"""
|
||||
if extension:
|
||||
extension_to_add = os.path.abspath(os.path.expanduser(extension))
|
||||
if os.path.exists(extension_to_add):
|
||||
self._extension_files.append(extension_to_add)
|
||||
else:
|
||||
raise IOError("Path to the extension doesn't exist")
|
||||
else:
|
||||
raise ValueError("argument can not be null")
|
||||
|
||||
def add_encoded_extension(self, extension: str) -> NoReturn:
|
||||
"""
|
||||
Adds Base64 encoded string with extension data to a list that will be used to extract it
|
||||
to the ChromeDriver
|
||||
|
||||
:Args:
|
||||
- extension: Base64 encoded string with extension data
|
||||
"""
|
||||
if extension:
|
||||
self._extensions.append(extension)
|
||||
else:
|
||||
raise ValueError("argument can not be null")
|
||||
|
||||
@property
|
||||
def experimental_options(self) -> dict:
|
||||
"""
|
||||
:Returns: A dictionary of experimental options for chromium
|
||||
"""
|
||||
return self._experimental_options
|
||||
|
||||
def add_experimental_option(self, name: str, value: Union[str, int, dict, List[str]]):
|
||||
"""
|
||||
Adds an experimental option which is passed to chromium.
|
||||
|
||||
:Args:
|
||||
name: The experimental option name.
|
||||
value: The option value.
|
||||
"""
|
||||
self._experimental_options[name] = value
|
||||
|
||||
@property
|
||||
def headless(self) -> bool:
|
||||
"""
|
||||
:Returns: True if the headless argument is set, else False
|
||||
"""
|
||||
return '--headless' in self._arguments
|
||||
|
||||
@headless.setter
|
||||
def headless(self, value: bool):
|
||||
"""
|
||||
Sets the headless argument
|
||||
:Args:
|
||||
value: boolean value indicating to set the headless option
|
||||
"""
|
||||
args = {'--headless'}
|
||||
if value is True:
|
||||
self._arguments.extend(args)
|
||||
else:
|
||||
self._arguments = list(set(self._arguments) - args)
|
||||
|
||||
def to_capabilities(self) -> dict:
|
||||
"""
|
||||
Creates a capabilities with all the options that have been set
|
||||
:Returns: A dictionary with everything
|
||||
"""
|
||||
caps = self._caps
|
||||
chrome_options = self.experimental_options.copy()
|
||||
if self.mobile_options:
|
||||
chrome_options.update(self.mobile_options)
|
||||
chrome_options["extensions"] = self.extensions
|
||||
if self.binary_location:
|
||||
chrome_options["binary"] = self.binary_location
|
||||
chrome_options["args"] = self._arguments
|
||||
if self.debugger_address:
|
||||
chrome_options["debuggerAddress"] = self.debugger_address
|
||||
|
||||
caps[self.KEY] = chrome_options
|
||||
|
||||
return caps
|
||||
|
||||
@property
|
||||
def default_capabilities(self) -> dict:
|
||||
return DesiredCapabilities.CHROME.copy()
|
||||
@@ -0,0 +1,35 @@
|
||||
# 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.remote.remote_connection import RemoteConnection
|
||||
|
||||
|
||||
class ChromiumRemoteConnection(RemoteConnection):
|
||||
def __init__(self, remote_server_addr, vendor_prefix, browser_name, keep_alive=True, ignore_proxy=False):
|
||||
RemoteConnection.__init__(self, remote_server_addr, keep_alive, ignore_proxy=ignore_proxy)
|
||||
self.browser_name = browser_name
|
||||
self._commands["launchApp"] = ('POST', '/session/$sessionId/chromium/launch_app')
|
||||
self._commands["setPermissions"] = ('POST', '/session/$sessionId/permissions')
|
||||
self._commands["setNetworkConditions"] = ('POST', '/session/$sessionId/chromium/network_conditions')
|
||||
self._commands["getNetworkConditions"] = ('GET', '/session/$sessionId/chromium/network_conditions')
|
||||
self._commands["deleteNetworkConditions"] = ('DELETE', '/session/$sessionId/chromium/network_conditions')
|
||||
self._commands['executeCdpCommand'] = ('POST', '/session/$sessionId/{}/cdp/execute'.format(vendor_prefix))
|
||||
self._commands['getSinks'] = ('GET', '/session/$sessionId/{}/cast/get_sinks'.format(vendor_prefix))
|
||||
self._commands['getIssueMessage'] = ('GET', '/session/$sessionId/{}/cast/get_issue_message'.format(vendor_prefix))
|
||||
self._commands['setSinkToUse'] = ('POST', '/session/$sessionId/{}/cast/set_sink_to_use'.format(vendor_prefix))
|
||||
self._commands['startTabMirroring'] = ('POST', '/session/$sessionId/{}/cast/start_tab_mirroring'.format(vendor_prefix))
|
||||
self._commands['stopCasting'] = ('POST', '/session/$sessionId/{}/cast/stop_casting'.format(vendor_prefix))
|
||||
@@ -0,0 +1,48 @@
|
||||
# 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 typing import List
|
||||
from selenium.webdriver.common import service
|
||||
|
||||
|
||||
class ChromiumService(service.Service):
|
||||
"""
|
||||
Object that manages the starting and stopping the WebDriver instance of the ChromiumDriver
|
||||
"""
|
||||
|
||||
def __init__(self, executable_path: str, port: int = 0, service_args: List[str] = None,
|
||||
log_path: str = None, env: str = None, start_error_message: str = None):
|
||||
"""
|
||||
Creates a new instance of the Service
|
||||
|
||||
:Args:
|
||||
- executable_path : Path to the WebDriver executable
|
||||
- port : Port the service is running on
|
||||
- service_args : List of args to pass to the WebDriver service
|
||||
- log_path : Path for the WebDriver service to log to"""
|
||||
|
||||
self.service_args = service_args or []
|
||||
if log_path:
|
||||
self.service_args.append('--log-path=%s' % log_path)
|
||||
|
||||
if not start_error_message:
|
||||
raise AttributeError("start_error_message should not be empty")
|
||||
|
||||
service.Service.__init__(self, executable_path, port=port, env=env, start_error_message=start_error_message)
|
||||
|
||||
def command_line_args(self) -> List[str]:
|
||||
return ["--port=%d" % self.port] + self.service_args
|
||||
@@ -0,0 +1,237 @@
|
||||
# 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 typing import NoReturn
|
||||
from selenium.webdriver.common.options import BaseOptions
|
||||
from selenium.webdriver.common.service import Service
|
||||
from selenium.webdriver.edge.options import Options as EdgeOptions
|
||||
from selenium.webdriver.chrome.options import Options as ChromeOptions
|
||||
import warnings
|
||||
|
||||
from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
|
||||
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
|
||||
|
||||
DEFAULT_PORT = 0
|
||||
DEFAULT_SERVICE_LOG_PATH = None
|
||||
DEFAULT_KEEP_ALIVE = None
|
||||
|
||||
|
||||
class ChromiumDriver(RemoteWebDriver):
|
||||
"""
|
||||
Controls the WebDriver instance of ChromiumDriver and allows you to drive the browser.
|
||||
"""
|
||||
|
||||
def __init__(self, browser_name, vendor_prefix,
|
||||
port=DEFAULT_PORT, options: BaseOptions = None, service_args=None,
|
||||
desired_capabilities=None, service_log_path=DEFAULT_SERVICE_LOG_PATH,
|
||||
service: Service = None, keep_alive=DEFAULT_KEEP_ALIVE):
|
||||
"""
|
||||
Creates a new WebDriver instance of the ChromiumDriver.
|
||||
Starts the service and then creates new WebDriver instance of ChromiumDriver.
|
||||
|
||||
:Args:
|
||||
- browser_name - Browser name used when matching capabilities.
|
||||
- vendor_prefix - Company prefix to apply to vendor-specific WebDriver extension commands.
|
||||
- port - Deprecated: port you would like the service to run, if left as 0, a free port will be found.
|
||||
- options - this takes an instance of ChromiumOptions
|
||||
- service_args - Deprecated: List of args to pass to the driver service
|
||||
- desired_capabilities - Deprecated: Dictionary object with non-browser specific
|
||||
capabilities only, such as "proxy" or "loggingPref".
|
||||
- service_log_path - Deprecated: Where to log information from the driver.
|
||||
- keep_alive - Deprecated: Whether to configure ChromiumRemoteConnection to use HTTP keep-alive.
|
||||
"""
|
||||
if desired_capabilities:
|
||||
warnings.warn('desired_capabilities has been deprecated, please pass in a Service object',
|
||||
DeprecationWarning, stacklevel=2)
|
||||
if port != DEFAULT_PORT:
|
||||
warnings.warn('port has been deprecated, please pass in a Service object',
|
||||
DeprecationWarning, stacklevel=2)
|
||||
self.port = port
|
||||
if service_log_path != DEFAULT_SERVICE_LOG_PATH:
|
||||
warnings.warn('service_log_path has been deprecated, please pass in a Service object',
|
||||
DeprecationWarning, stacklevel=2)
|
||||
if keep_alive != DEFAULT_KEEP_ALIVE and type(self) == __class__:
|
||||
warnings.warn('keep_alive has been deprecated, please pass in a Service object',
|
||||
DeprecationWarning, stacklevel=2)
|
||||
else:
|
||||
keep_alive = True
|
||||
|
||||
self.vendor_prefix = vendor_prefix
|
||||
|
||||
_ignore_proxy = None
|
||||
if not options:
|
||||
options = self.create_options()
|
||||
|
||||
if desired_capabilities:
|
||||
for key, value in desired_capabilities.items():
|
||||
options.set_capability(key, value)
|
||||
|
||||
if options._ignore_local_proxy:
|
||||
_ignore_proxy = options._ignore_local_proxy
|
||||
|
||||
if not service:
|
||||
raise AttributeError('service cannot be None')
|
||||
|
||||
self.service = service
|
||||
self.service.start()
|
||||
|
||||
try:
|
||||
RemoteWebDriver.__init__(
|
||||
self,
|
||||
command_executor=ChromiumRemoteConnection(
|
||||
remote_server_addr=self.service.service_url,
|
||||
browser_name=browser_name, vendor_prefix=vendor_prefix,
|
||||
keep_alive=keep_alive, ignore_proxy=_ignore_proxy),
|
||||
options=options)
|
||||
except Exception:
|
||||
self.quit()
|
||||
raise
|
||||
self._is_remote = False
|
||||
|
||||
def launch_app(self, id):
|
||||
"""Launches Chromium app specified by id."""
|
||||
return self.execute("launchApp", {'id': id})
|
||||
|
||||
def get_network_conditions(self):
|
||||
"""
|
||||
Gets Chromium network emulation settings.
|
||||
|
||||
:Returns:
|
||||
A dict. For example:
|
||||
{'latency': 4, 'download_throughput': 2, 'upload_throughput': 2,
|
||||
'offline': False}
|
||||
"""
|
||||
return self.execute("getNetworkConditions")['value']
|
||||
|
||||
def set_network_conditions(self, **network_conditions) -> NoReturn:
|
||||
"""
|
||||
Sets Chromium network emulation settings.
|
||||
|
||||
:Args:
|
||||
- network_conditions: A dict with conditions specification.
|
||||
|
||||
:Usage:
|
||||
::
|
||||
|
||||
driver.set_network_conditions(
|
||||
offline=False,
|
||||
latency=5, # additional latency (ms)
|
||||
download_throughput=500 * 1024, # maximal throughput
|
||||
upload_throughput=500 * 1024) # maximal throughput
|
||||
|
||||
Note: 'throughput' can be used to set both (for download and upload).
|
||||
"""
|
||||
self.execute("setNetworkConditions", {
|
||||
'network_conditions': network_conditions
|
||||
})
|
||||
|
||||
def delete_network_conditions(self) -> NoReturn:
|
||||
"""
|
||||
Resets Chromium network emulation settings.
|
||||
"""
|
||||
self.execute("deleteNetworkConditions")
|
||||
|
||||
def set_permissions(self, name: str, value: str) -> NoReturn:
|
||||
"""
|
||||
Sets Applicable Permission.
|
||||
|
||||
:Args:
|
||||
- name: The item to set the permission on.
|
||||
- value: The value to set on the item
|
||||
|
||||
:Usage:
|
||||
::
|
||||
driver.set_permissions('clipboard-read', 'denied')
|
||||
"""
|
||||
self.execute("setPermissions", {'descriptor': {'name': name}, 'state': value})
|
||||
|
||||
def execute_cdp_cmd(self, cmd: str, cmd_args: dict):
|
||||
"""
|
||||
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 get_sinks(self) -> list:
|
||||
"""
|
||||
:Returns: A list of sinks avaliable for Cast.
|
||||
"""
|
||||
return self.execute('getSinks')['value']
|
||||
|
||||
def get_issue_message(self):
|
||||
"""
|
||||
:Returns: An error message when there is any issue in a Cast session.
|
||||
"""
|
||||
return self.execute('getIssueMessage')['value']
|
||||
|
||||
def set_sink_to_use(self, sink_name: str) -> str:
|
||||
"""
|
||||
Sets a specific sink, using its name, as a Cast session receiver target.
|
||||
|
||||
:Args:
|
||||
- sink_name: Name of the sink to use as the target.
|
||||
"""
|
||||
return self.execute('setSinkToUse', {'sinkName': sink_name})
|
||||
|
||||
def start_tab_mirroring(self, sink_name: str) -> str:
|
||||
"""
|
||||
Starts a tab mirroring session on a specific receiver target.
|
||||
|
||||
:Args:
|
||||
- sink_name: Name of the sink to use as the target.
|
||||
"""
|
||||
return self.execute('startTabMirroring', {'sinkName': sink_name})
|
||||
|
||||
def stop_casting(self, sink_name: str) -> str:
|
||||
"""
|
||||
Stops the existing Cast session on a specific receiver target.
|
||||
|
||||
:Args:
|
||||
- sink_name: Name of the sink to stop the Cast session.
|
||||
"""
|
||||
return self.execute('stopCasting', {'sinkName': sink_name})
|
||||
|
||||
def quit(self) -> NoReturn:
|
||||
"""
|
||||
Closes the browser and shuts down the ChromiumDriver executable
|
||||
that is started when starting the ChromiumDriver
|
||||
"""
|
||||
try:
|
||||
RemoteWebDriver.quit(self)
|
||||
except Exception:
|
||||
# We don't care about the message because something probably has gone wrong
|
||||
pass
|
||||
finally:
|
||||
self.service.stop()
|
||||
|
||||
def create_options(self) -> BaseOptions:
|
||||
if self.vendor_prefix == "ms":
|
||||
return EdgeOptions()
|
||||
else:
|
||||
return ChromeOptions()
|
||||
Reference in New Issue
Block a user