Skip to content

Commit

Permalink
[py] Subclass options classes from a common base class (#6522)
Browse files Browse the repository at this point in the history
  • Loading branch information
lmtierney committed Jan 4, 2019
1 parent 6a1e850 commit 4f3df20
Show file tree
Hide file tree
Showing 14 changed files with 389 additions and 131 deletions.
44 changes: 7 additions & 37 deletions py/selenium/webdriver/chrome/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@
import base64
import os
import platform
import warnings

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.options import ArgOptions


class Options(object):
class Options(ArgOptions):
KEY = "goog:chromeOptions"

def __init__(self):
super(Options, self).__init__()
self._binary_location = ''
self._arguments = []
self._extension_files = []
self._extensions = []
self._experimental_options = {}
self._debugger_address = None
self._caps = DesiredCapabilities.CHROME.copy()

@property
def binary_location(self):
Expand All @@ -52,14 +51,6 @@ def binary_location(self, value):
"""
self._binary_location = value

@property
def capabilities(self):
return self._caps

def set_capability(self, name, value):
"""Sets a capability."""
self._caps[name] = value

@property
def debugger_address(self):
"""
Expand All @@ -79,25 +70,6 @@ def debugger_address(self, value):
"""
self._debugger_address = value

@property
def arguments(self):
"""
Returns a list of arguments needed for the browser
"""
return self._arguments

def add_argument(self, argument):
"""
Adds an argument to the list
:Args:
- Sets the arguments
"""
if argument:
self._arguments.append(argument)
else:
raise ValueError("argument can not be null")

@property
def extensions(self):
"""
Expand Down Expand Up @@ -185,12 +157,6 @@ def headless(self, value):
else:
self._arguments = list(set(self._arguments) - args)

def set_headless(self, headless=True):
""" Deprecated, options.headless = True """
warnings.warn('use setter for headless property instead of set_headless',
DeprecationWarning, stacklevel=2)
self.headless = headless

def to_capabilities(self):
"""
Creates a capabilities with all the options that have been set and
Expand All @@ -209,3 +175,7 @@ def to_capabilities(self):
caps[self.KEY] = chrome_options

return caps

@property
def default_capabilities(self):
return DesiredCapabilities.CHROME.copy()
74 changes: 74 additions & 0 deletions py/selenium/webdriver/common/options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# 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 abc import ABCMeta, abstractmethod


class BaseOptions(object):
"""
Base class for individual browser options
"""
__metaclass__ = ABCMeta

def __init__(self):
self._caps = self.default_capabilities

@property
def capabilities(self):
return self._caps

def set_capability(self, name, value):
""" Sets a capability """
self._caps[name] = value

@abstractmethod
def to_capabilities(self):
return

@property
@abstractmethod
def default_capabilities(self):
return {}


class ArgOptions(BaseOptions):

def __init__(self):
super(ArgOptions, self).__init__()
self._arguments = []

@property
def arguments(self):
"""
Returns a list of arguments needed for the browser
"""
return self._arguments

def add_argument(self, argument):
"""
Adds an argument to the list
:Args:
- Sets the arguments
"""
if argument:
self._arguments.append(argument)
else:
raise ValueError('argument can not be null')

def to_capabilities(self):
return self._caps
17 changes: 7 additions & 10 deletions py/selenium/webdriver/edge/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
# under the License.

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.options import BaseOptions


class Options(object):
class Options(BaseOptions):

def __init__(self):
super(Options, self).__init__()
self._page_load_strategy = "normal"
self._caps = DesiredCapabilities.EDGE.copy()

@property
def page_load_strategy(self):
Expand All @@ -34,14 +35,6 @@ def page_load_strategy(self, value):
raise ValueError("Page Load Strategy should be 'normal', 'eager' or 'none'.")
self._page_load_strategy = value

@property
def capabilities(self):
return self._caps

def set_capability(self, name, value):
"""Sets a capability."""
self._caps[name] = value

def to_capabilities(self):
"""
Creates a capabilities with all the options that have been set and
Expand All @@ -52,3 +45,7 @@ def to_capabilities(self):
caps['pageLoadStrategy'] = self._page_load_strategy

return caps

@property
def default_capabilities(self):
return DesiredCapabilities.EDGE.copy()
29 changes: 7 additions & 22 deletions py/selenium/webdriver/firefox/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.common.options import ArgOptions


class Log(object):
Expand All @@ -33,16 +34,15 @@ def to_capabilities(self):
return {}


class Options(object):
class Options(ArgOptions):
KEY = "moz:firefoxOptions"

def __init__(self):
super(Options, self).__init__()
self._binary = None
self._preferences = {}
self._profile = None
self._proxy = None
self._caps = DesiredCapabilities.FIREFOX.copy()
self._arguments = []
self.log = Log()

@property
Expand Down Expand Up @@ -78,14 +78,6 @@ def accept_insecure_certs(self):
def accept_insecure_certs(self, value):
self._caps['acceptInsecureCerts'] = value

@property
def capabilities(self):
return self._caps

def set_capability(self, name, value):
"""Sets a capability."""
self._caps[name] = value

@property
def preferences(self):
"""Returns a dict of preferences."""
Expand Down Expand Up @@ -121,17 +113,6 @@ def profile(self, new_profile):
new_profile = FirefoxProfile(new_profile)
self._profile = new_profile

@property
def arguments(self):
"""Returns a list of browser process arguments."""
return self._arguments

def add_argument(self, argument):
"""Add argument to be used for the browser process."""
if argument is None:
raise ValueError()
self._arguments.append(argument)

@property
def headless(self):
"""
Expand Down Expand Up @@ -187,3 +168,7 @@ def to_capabilities(self):
caps[Options.KEY] = opts

return caps

@property
def default_capabilities(self):
return DesiredCapabilities.FIREFOX.copy()
29 changes: 7 additions & 22 deletions py/selenium/webdriver/ie/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
# specific language governing permissions and limitations
# under the License.
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.options import ArgOptions


class ElementScrollBehavior(object):
TOP = 0
BOTTOM = 1


class Options(object):
class Options(ArgOptions):

KEY = 'se:ieOptions'
SWITCHES = 'ie.browserCommandLineSwitches'
Expand All @@ -44,35 +45,15 @@ class Options(object):
VALIDATE_COOKIE_DOCUMENT_TYPE = 'ie.validateCookieDocumentType'

def __init__(self):
self._arguments = []
super(Options, self).__init__()
self._options = {}
self._additional = {}
self._caps = DesiredCapabilities.INTERNETEXPLORER.copy()

@property
def arguments(self):
""" Returns a list of browser process arguments """
return self._arguments

def add_argument(self, argument):
""" Add argument to be used for the browser process """
if argument is None:
raise ValueError()
self._arguments.append(argument)

@property
def options(self):
""" Returns a dictionary of browser options """
return self._options

@property
def capabilities(self):
return self._caps

def set_capability(self, name, value):
"""Sets a capability."""
self._caps[name] = value

@property
def browser_attach_timeout(self):
""" Returns the options Browser Attach Timeout in milliseconds """
Expand Down Expand Up @@ -349,3 +330,7 @@ def to_capabilities(self):
if len(opts) > 0:
caps[Options.KEY] = opts
return caps

@property
def default_capabilities(self):
return DesiredCapabilities.INTERNETEXPLORER.copy()
13 changes: 4 additions & 9 deletions py/selenium/webdriver/opera/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ def __init__(self):
self._android_package_name = ''
self._android_device_socket = ''
self._android_command_line_file = ''
self._caps = DesiredCapabilities.OPERA.copy()

@property
def capabilities(self):
return self._caps

def set_capability(self, name, value):
"""Sets a capability."""
self._caps[name] = value

@property
def android_package_name(self):
Expand Down Expand Up @@ -107,6 +98,10 @@ def to_capabilities(self):
self.android_command_line_file
return capabilities

@property
def default_capabilities(self):
return DesiredCapabilities.OPERA.copy()


class AndroidOptions(Options):

Expand Down

0 comments on commit 4f3df20

Please sign in to comment.