Skip to content

Commit

Permalink
capabilities as a single dict instead of a list of dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
ElSnoMan committed Apr 27, 2020
1 parent ee89d95 commit bc76c34
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 43 deletions.
6 changes: 3 additions & 3 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def py_config(project_root, request) -> PyleniumConfig:

cli_capabilities = request.config.getoption('--caps')
if cli_capabilities:
# --caps must be in '[{}, {}]' format
# with double quotes around each key and value
# --caps must be in '{"name": "value", "boolean": true}' format
# with double quotes around each key. booleans are lowercase.
config.driver.capabilities = json.loads(cli_capabilities)

# Logging Settings
Expand Down Expand Up @@ -211,5 +211,5 @@ def pytest_addoption(parser):
)
parser.addoption(
'--caps', action='store',
default='', help='List of key-value pairs. Ex. "[{}, {}]"'
default='', help='List of key-value pairs. Ex. \'{"name": "value", "boolean": true}\''
)
2 changes: 1 addition & 1 deletion pylenium/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class DriverConfig(BaseModel):
remote_url: str = ''
wait_time: int = 10
options: List[str] = []
capabilities: List[dict] = []
capabilities: dict = {}
version: str = 'latest'


Expand Down
90 changes: 52 additions & 38 deletions pylenium/webdriver_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,43 @@ class Browser:
OPERA = 'opera'


def build_options(browser, browser_options: List[str], capabilities: Optional[List[dict]]):
def build_capabilities(browser, capabilities: dict):
""" Build the capabilities dictionary for WebDriver.
Args:
browser: The name of the browser.
capabilities: The dict of capabilities to include.
Examples:
caps = WebDriverFactory().build_capabilities({'enableVNC': True, 'enableVideo': False})
"""
caps = {}

if browser == Browser.CHROME:
caps.update(webdriver.DesiredCapabilities.CHROME.copy())
elif browser == Browser.FIREFOX:
caps.update(webdriver.DesiredCapabilities.FIREFOX.copy())
elif browser == Browser.IE:
caps.update(webdriver.DesiredCapabilities.INTERNETEXPLORER.copy())
elif browser == Browser.OPERA:
caps.update(webdriver.DesiredCapabilities.OPERA.copy())
elif browser == Browser.EDGE:
caps.update(webdriver.DesiredCapabilities.EDGE.copy())
else:
raise ValueError(f'{browser} is not supported.')

if capabilities:
caps.update(capabilities)

return caps


def build_options(browser, browser_options: List[str]):
""" Build the Options object for Chrome or Firefox.
Args:
browser: The name of the browser.
browser_options: The list of options/arguments to include.
capabilities: The list of capabilities to include.
Examples:
driver = WebDriverFactory().build_chrome(['headless', 'incognito'])
Expand All @@ -47,11 +77,6 @@ def build_options(browser, browser_options: List[str], capabilities: Optional[Li
for option in browser_options:
options.add_argument(f'--{option}')

if capabilities:
for cap in capabilities:
(key, value), = cap.items()
options.set_capability(key, value)

return options


Expand All @@ -73,7 +98,7 @@ def build_from_config(config: PyleniumConfig) -> WebDriver:
elif browser == Browser.FIREFOX:
return build_firefox(config.driver.version, config.driver.options)
elif browser == Browser.IE:
return build_ie(config.driver.version, config.driver.options)
return build_ie(config.driver.version, config.driver.options, config.driver.capabilities)
elif browser == Browser.OPERA:
return build_opera(config.driver.version, config.driver.options)
elif browser == Browser.EDGE:
Expand All @@ -92,23 +117,25 @@ def build_chrome(version: str, browser_options: List[str]) -> WebDriver:
Examples:
driver = WebDriverFactory().build_chrome('latest', ['headless', 'incognito'])
"""
options = build_options('chrome', browser_options, None)
options = build_options(Browser.CHROME, browser_options)
return webdriver.Chrome(ChromeDriverManager(version=version).install(), options=options)


def build_edge(version: str, browser_options: List[str], capabilities: List[dict]) -> WebDriver:
def build_edge(version: str, browser_options: List[str], capabilities: dict) -> WebDriver:
""" Build a Edge Driver.
Args:
version: The desired version of Edge.
browser_options: The list of options/arguments to include.
capabilities: The list of capabilities to include.
capabilities: The dict of capabilities to include.
Examples:
driver = WebDriverFactory().build_edge('latest', ['headless', 'incognito'])
"""
options = build_options('edge', browser_options, capabilities)
return webdriver.Edge(EdgeChromiumDriverManager(version=version).install(), capabilities=options.to_capabilities())
caps = build_capabilities(Browser.EDGE, capabilities)
options = build_options(Browser.EDGE, browser_options).to_capabilities()
caps.update(options)
return webdriver.Edge(EdgeChromiumDriverManager(version=version).install(), capabilities=caps)


def build_firefox(version: str, browser_options: List[str]) -> WebDriver:
Expand All @@ -121,22 +148,24 @@ def build_firefox(version: str, browser_options: List[str]) -> WebDriver:
Examples:
driver = WebDriverFactory().build_firefox('latest', ['headless', 'incognito'])
"""
options = build_options('firefox', browser_options, None)
options = build_options(Browser.FIREFOX, browser_options)
return webdriver.Firefox(executable_path=GeckoDriverManager(version=version).install(), options=options)


def build_ie(version: str, browser_options: List[str]) -> WebDriver:
def build_ie(version: str, browser_options: List[str], capabilities: dict) -> WebDriver:
""" Build an IEDriver.
Args:
version: The desired version of IE.
browser_options: The list of options/arguments to include.
capabilities: The dict of capabilities.
Examples:
driver = WebDriverFactory().build_ie('latest', ['headless'])
"""
options = build_options('ie', browser_options, None)
return webdriver.Ie(executable_path=IEDriverManager(version=version).install(), options=options)
caps = build_capabilities(Browser.IE, capabilities)
options = build_options(Browser.IE, browser_options)
return webdriver.Ie(executable_path=IEDriverManager(version=version).install(), options=options, capabilities=caps)


def build_opera(version: str, browser_options: List[str]) -> WebDriver:
Expand All @@ -149,43 +178,28 @@ def build_opera(version: str, browser_options: List[str]) -> WebDriver:
Examples:
driver = WebDriverFactory().build_opera('latest', ['--start-maximized'])
"""
options = build_options('opera', browser_options, None)
options = build_options(Browser.OPERA, browser_options)
return webdriver.Opera(executable_path=OperaDriverManager(version=version).install(), options=options)


def build_remote(browser: str, remote_url: str, browser_options: List[str], capabilities: List[dict]) -> WebDriver:
def build_remote(browser: str, remote_url: str, browser_options: List[str], capabilities: dict) -> WebDriver:
""" Build a RemoteDriver connected to a Grid.
Args:
browser: Name of the browser to connect to.
remote_url: The URL to connect to the Grid.
browser_options: The list of options/arguments to include.
capabilities: The list of capabilities to include.
capabilities: The dict of capabilities to include.
Returns:
The instance of WebDriver once the connection is successful
"""
browser = browser.lower()
if browser == Browser.CHROME:
caps = webdriver.DesiredCapabilities.CHROME.copy()
elif browser == Browser.FIREFOX:
caps = webdriver.DesiredCapabilities.FIREFOX.copy()
elif browser == Browser.IE:
caps = webdriver.DesiredCapabilities.INTERNETEXPLORER.copy()
elif browser == Browser.OPERA:
caps = webdriver.DesiredCapabilities.OPERA.copy()
elif browser == Browser.EDGE:
caps = webdriver.DesiredCapabilities.EDGE.copy()
else:
caps = {}

if capabilities:
caps = capabilities

options = build_options(browser, browser_options, caps)
caps = build_capabilities(browser, capabilities)
options = build_options(browser, browser_options)

return webdriver.Remote(
command_executor=remote_url,
desired_capabilities=caps,
options=options,
options=options
)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='pyleniumio',
version='1.5.3',
version='1.5.4',
packages=[
'pylenium'
],
Expand Down
2 changes: 2 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def test_py_config_defaults(py_config):
assert py_config.driver.remote_url == ''
assert py_config.driver.wait_time == 10
assert py_config.driver.options == []
assert py_config.driver.version == 'latest'
assert py_config.driver.capabilities == {'name': 'value', 'boolean': True}

# logging settings
assert py_config.logging.screenshots_on is True
Expand Down

0 comments on commit bc76c34

Please sign in to comment.