Skip to content

Commit

Permalink
[py] Update driver initialisation to use service and option objects
Browse files Browse the repository at this point in the history
This deprecates all the other variables in the initialization simplifying the code in the
future to look like "driver = webdriver.<browser>(service, options)" or use the default of
"driver = webdriver.<browser>()"
  • Loading branch information
AutomatedTester committed Apr 16, 2019
1 parent fcd8ec0 commit 19887a4
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 37 deletions.
47 changes: 34 additions & 13 deletions py/selenium/webdriver/chrome/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
from .options import Options


DEFAULT_PORT = 0
DEFAULT_SERVICE_LOG_PATH = None

class WebDriver(RemoteWebDriver):
"""
Controls the ChromeDriver and allows you to drive the browser.
Expand All @@ -29,25 +32,40 @@ class WebDriver(RemoteWebDriver):
http://chromedriver.storage.googleapis.com/index.html
"""

def __init__(self, executable_path="chromedriver", port=0,
def __init__(self, executable_path="chromedriver", port=DEFAULT_PORT,
options=None, service_args=None,
desired_capabilities=None, service_log_path=None,
chrome_options=None, keep_alive=True):
desired_capabilities=None, service_log_path=DEFAULT_SERVICE_LOG_PATH,
chrome_options=None, service=None, keep_alive=True):
"""
Creates a new instance of the chrome driver.
Starts the service and then creates new instance of chrome driver.
:Args:
- executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH
- port - port you would like the service to run, if left as 0, a free port will be found.
- executable_path - Deprecated: path to the executable. If the default is used it assumes the executable is in the $PATH
- 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 ChromeOptions
- service_args - List of args to pass to the driver service
- desired_capabilities - Dictionary object with non-browser specific
- 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 - Where to log information from the driver.
- service_log_path - Deprecated: Where to log information from the driver.
- keep_alive - Whether to configure ChromeRemoteConnection to use HTTP keep-alive.
"""
if executable_path != 'chromedriver':
warnings.warn('executable_path has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
if desired_capabilities is not None:
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 chrome_options:
warnings.warn('use options instead of chrome_options',
DeprecationWarning, stacklevel=2)
Expand All @@ -63,11 +81,14 @@ def __init__(self, executable_path="chromedriver", port=0,
else:
desired_capabilities.update(options.to_capabilities())

self.service = Service(
executable_path,
port=port,
service_args=service_args,
log_path=service_log_path)
if service:
self.service = service
else:
self.service = Service(
executable_path,
port=port,
service_args=service_args,
log_path=service_log_path)
self.service.start()

try:
Expand Down
38 changes: 31 additions & 7 deletions py/selenium/webdriver/edge/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@
from .service import Service


DEFAULT_PORT = 0
DEFAULT_SERVICE_LOG_PATH = None


class WebDriver(RemoteWebDriver):

def __init__(self, executable_path='MicrosoftWebDriver.exe',
capabilities=None, port=0, verbose=False, service_log_path=None,
log_path=None, keep_alive=False):
capabilities=None, port=DEFAULT_PORT, verbose=False,
service_log_path=None, log_path=DEFAULT_SERVICE_LOG_PATH,
service=None, options=None, keep_alive=False):
"""
Creates a new instance of the chrome driver.
Expand All @@ -38,21 +43,40 @@ def __init__(self, executable_path='MicrosoftWebDriver.exe',
- port - port you would like the service to run, if left as 0, a free port will be found.
- verbose - whether to set verbose logging in the service
- service_log_path - Where to log information from the driver.
- keep_alive - Whether to configure ChromeRemoteConnection to use HTTP keep-alive.
- keep_alive - Whether to configure EdgeRemoteConnection to use HTTP keep-alive.
"""
if port != DEFAULT_PORT:
warnings.warn('port has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
self.port = port
if self.port == 0:
self.port = utils.free_port()

self.edge_service = Service(executable_path, port=self.port, verbose=verbose, log_path=service_log_path)
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 capabilities is not None:
warnings.warn('capabilities has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
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 verbose:
warnings.warn('verbose has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)

if service:
self.edge_service = service
else:
self.edge_service = Service(executable_path,
port=self.port, verbose=verbose,
log_path=service_log_path)
self.edge_service.start()

if capabilities is None:
capabilities = DesiredCapabilities.EDGE

RemoteWebDriver.__init__(
self,
command_executor=RemoteConnection('http://localhost:%d' % self.port,
command_executor=RemoteConnection(self.service.service_url,
resolve_ip=False,
keep_alive=keep_alive),
desired_capabilities=capabilities)
Expand Down
34 changes: 28 additions & 6 deletions py/selenium/webdriver/firefox/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
basestring = str

import shutil
import warnings
from contextlib import contextmanager

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
Expand All @@ -33,6 +34,8 @@
from .webelement import FirefoxWebElement


DEFAULT_SERVICE_LOG_PATH = None

class WebDriver(RemoteWebDriver):

CONTEXT_CHROME = "chrome"
Expand All @@ -44,7 +47,7 @@ def __init__(self, firefox_profile=None, firefox_binary=None,
timeout=30, capabilities=None, proxy=None,
executable_path="geckodriver", options=None,
service_log_path="geckodriver.log", firefox_options=None,
service_args=None, desired_capabilities=None, log_path=None,
service_args=None, service=None, desired_capabilities=None, log_path=None,
keep_alive=True):
"""Starts a new local session of Firefox.
Expand Down Expand Up @@ -96,9 +99,27 @@ def __init__(self, firefox_profile=None, firefox_binary=None,
:param keep_alive: Whether to configure remote_connection.RemoteConnection to use
HTTP keep-alive.
"""

if executable_path != 'geckodriver':
warnings.warn('executable_path has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
if capabilities is not None:
warnings.warn('capabilities has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
if firefox_binary is not None:
warnings.warn('firefox_binary has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
self.binary = None
if firefox_profile is not None:
warnings.warn('firefox_profile has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
self.profile = None
self.service = None

if log_path != DEFAULT_SERVICE_LOG_PATH:
warnings.warn('log_path has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)

self.service = service

# If desired capabilities is set, alias it to capabilities.
# If both are set ignore desired capabilities.
Expand Down Expand Up @@ -135,10 +156,11 @@ def __init__(self, firefox_profile=None, firefox_binary=None,
self.profile = firefox_profile
options.profile = firefox_profile

self.service = Service(
executable_path,
service_args=service_args,
log_path=service_log_path)
if self.service is None:
self.service = Service(
executable_path,
service_args=service_args,
log_path=service_log_path)
self.service.start()

capabilities.update(options.to_capabilities())
Expand Down
45 changes: 34 additions & 11 deletions py/selenium/webdriver/ie/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,49 @@ class WebDriver(RemoteWebDriver):

def __init__(self, executable_path='IEDriverServer.exe', capabilities=None,
port=DEFAULT_PORT, timeout=DEFAULT_TIMEOUT, host=DEFAULT_HOST,
log_level=DEFAULT_LOG_LEVEL, service_log_path=DEFAULT_SERVICE_LOG_PATH, options=None,
ie_options=None, desired_capabilities=None, log_file=None, keep_alive=False):
log_level=DEFAULT_LOG_LEVEL, service_log_path=DEFAULT_SERVICE_LOG_PATH,
options=None, service=None,
desired_capabilities=None, keep_alive=False):
"""
Creates a new instance of the chrome driver.
Starts the service and then creates new instance of chrome driver.
:Args:
- executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH
- capabilities: capabilities Dictionary object
- port - port you would like the service to run, if left as 0, a free port will be found.
- timeout - no longer used, kept for backward compatibility
- host - IP address for the service
- log_level - log level you would like the service to run.
- service_log_path - target of logging of service, may be "stdout", "stderr" or file path.
- executable_path - Deprecated: path to the executable. If the default is used it assumes the executable is in the $PATH
- capabilities - Deprecated: capabilities Dictionary object
- port - Deprecated: port you would like the service to run, if left as 0, a free port will be found.
- timeout - Deprecated: no longer used, kept for backward compatibility
- host - Deprecated: IP address for the service
- log_level - Deprecated: log level you would like the service to run.
- service_log_path - Deprecated: target of logging of service, may be "stdout", "stderr" or file path.
- options - IE Options instance, providing additional IE options
- desired_capabilities - alias of capabilities; this will make the signature consistent with RemoteWebDriver.
- desired_capabilities - Deprecated: alias of capabilities; this will make the signature consistent with RemoteWebDriver.
- keep_alive - Whether to configure RemoteConnection to use HTTP keep-alive.
"""
if executable_path != 'IEDriverServer.exe':
warnings.warn('executable_path has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
if capabilities is not None:
warnings.warn('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 timeout != DEFAULT_TIMEOUT:
warnings.warn('timeout has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
if host != DEFAULT_HOST:
warnings.warn('host has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
self.host = host
if log_level != DEFAULT_LOG_LEVEL:
warnings.warn('log_level has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
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 both capabilities and desired capabilities are set, ignore desired capabilities.
if capabilities is None and desired_capabilities:
Expand All @@ -65,7 +87,8 @@ def __init__(self, executable_path='IEDriverServer.exe', capabilities=None,
else:
# desired_capabilities stays as passed in
capabilities.update(options.to_capabilities())

if service is None:
service = Service()
self.iedriver = Service(
executable_path,
port=self.port,
Expand Down

0 comments on commit 19887a4

Please sign in to comment.