Skip to content

Commit

Permalink
Merge pull request #150 from ElSnoMan/instantiate-custom-pylenium
Browse files Browse the repository at this point in the history
Update webdriver_factory to allow for local driver binary paths
  • Loading branch information
ElSnoMan authored Dec 14, 2020
2 parents 7f8b811 + 3c7a295 commit 0e30017
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 13 deletions.
3 changes: 2 additions & 1 deletion pylenium.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"capabilities": {},
"version": "latest",
"experimental_options": null,
"extension_paths": []
"extension_paths": [],
"local_path": ""
},

"logging":{
Expand Down
1 change: 1 addition & 0 deletions pylenium/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class DriverConfig(BaseModel):
experimental_options: List[dict] = None
extension_paths: List[str] = None
version: str = 'latest'
local_path: str = ''


class LoggingConfig(BaseModel):
Expand Down
3 changes: 2 additions & 1 deletion pylenium/scripts/pylenium.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"capabilities": {},
"version": "latest",
"experimental_options": null,
"extension_paths": []
"extension_paths": [],
"local_path": ""
},

"logging":{
Expand Down
73 changes: 62 additions & 11 deletions pylenium/webdriver_factory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Factory to build WebDrivers. """
from pylenium import driver
from typing import List, Optional

from selenium import webdriver
Expand Down Expand Up @@ -81,7 +82,10 @@ def build_options(browser,
raise ValueError(f'{browser} is not supported. https://elsnoman.gitbook.io/pylenium/configuration/driver')

for option in browser_options:
options.add_argument(f'--{option}')
if option.startswith('--'):
options.add_argument(option)
else:
options.add_argument(f'--{option}')

if experimental_options:
for exp_option in experimental_options:
Expand Down Expand Up @@ -111,43 +115,75 @@ def build_from_config(config: PyleniumConfig) -> WebDriver:
)
browser = config.driver.browser.lower()
if browser == Browser.CHROME:
return build_chrome(config.driver.version, config.driver.options, config.driver.experimental_options, config.driver.extension_paths)
return build_chrome(
config.driver.version,
config.driver.options,
config.driver.experimental_options,
config.driver.extension_paths,
config.driver.local_path)
elif browser == Browser.FIREFOX:
return build_firefox(config.driver.version, config.driver.options, config.driver.experimental_options, config.driver.extension_paths)
return build_firefox(
config.driver.version,
config.driver.options,
config.driver.experimental_options,
config.driver.extension_paths,
config.driver.local_path)
elif browser == Browser.IE:
return build_ie(config.driver.version, config.driver.options, config.driver.capabilities, config.driver.experimental_options, config.driver.extension_paths)
return build_ie(
config.driver.version,
config.driver.options,
config.driver.capabilities,
config.driver.experimental_options,
config.driver.extension_paths,
config.driver.local_path)
elif browser == Browser.OPERA:
return build_opera(config.driver.version, config.driver.options, config.driver.experimental_options, config.driver.extension_paths)
return build_opera(
config.driver.version,
config.driver.options,
config.driver.experimental_options,
config.driver.extension_paths,
config.driver.local_path)
elif browser == Browser.EDGE:
return build_edge(config.driver.version, config.driver.options, config.driver.capabilities, config.driver.experimental_options, config.driver.extension_paths)
return build_edge(
config.driver.version,
config.driver.options,
config.driver.capabilities,
config.driver.experimental_options,
config.driver.extension_paths,
config.driver.local_path)
else:
raise ValueError(f'{config.driver.browser} is not supported. https://elsnoman.gitbook.io/pylenium/configuration/driver')


def build_chrome(version: str,
browser_options: List[str],
experimental_options: Optional[List[dict]],
extension_paths: Optional[List[str]]) -> WebDriver:
extension_paths: Optional[List[str]],
local_path: Optional[str]) -> WebDriver:
""" Build a ChromeDriver.
Args:
version: The desired version of Chrome.
browser_options: The list of options/arguments to include.
experimental_options: The list of experimental options to include.
extension_paths: The list of extensions to add to the browser.
local_path: The path to the driver binary.
Examples:
driver = WebDriverFactory().build_chrome('latest', ['headless', 'incognito'], None)
"""
options = build_options(Browser.CHROME, browser_options, experimental_options, extension_paths)
if local_path:
return webdriver.Chrome(local_path, options=options)
return webdriver.Chrome(ChromeDriverManager(version=version).install(), options=options)


def build_edge(version: str,
browser_options: List[str],
capabilities: dict,
experimental_options: Optional[List[dict]],
extension_paths: Optional[List[str]]) -> WebDriver:
extension_paths: Optional[List[str]],
local_path: Optional[str]) -> WebDriver:
""" Build a Edge Driver.
Args:
Expand All @@ -156,40 +192,48 @@ def build_edge(version: str,
capabilities: The dict of capabilities to include.
experimental_options: The list of experimental options to include.
extension_paths: The list of extensions to add to the browser.
local_path: The path to the driver binary.
Examples:
driver = WebDriverFactory().build_edge('latest', ['headless', 'incognito'], None)
"""
caps = build_capabilities(Browser.EDGE, capabilities)
options = build_options(Browser.EDGE, browser_options, experimental_options, extension_paths).to_capabilities()
caps.update(options)
if local_path:
return webdriver.Edge(local_path, capabilities=caps)
return webdriver.Edge(EdgeChromiumDriverManager(version=version).install(), capabilities=caps)


def build_firefox(version: str,
browser_options: List[str],
experimental_options: Optional[List[dict]],
extension_paths: Optional[List[str]]) -> WebDriver:
extension_paths: Optional[List[str]],
local_path: Optional[str]) -> WebDriver:
""" Build a FirefoxDriver.
Args:
version: The desired version of Firefox.
browser_options: The list of options/arguments to include.
experimental_options: The list of experimental options to include.
extension_paths: The list of extensions to add to the browser.
local_path: The path to the driver binary.
Examples:
driver = WebDriverFactory().build_firefox('latest', ['headless', 'incognito'], None)
"""
options = build_options(Browser.FIREFOX, browser_options, experimental_options, extension_paths)
if local_path:
return webdriver.Firefox(executable_path=local_path, options=options)
return webdriver.Firefox(executable_path=GeckoDriverManager(version=version).install(), options=options)


def build_ie(version: str,
browser_options: List[str],
capabilities: dict,
experimental_options: Optional[List[dict]],
extension_paths: Optional[List[str]]) -> WebDriver:
extension_paths: Optional[List[str]],
local_path: Optional[str]) -> WebDriver:
""" Build an IEDriver.
Args:
Expand All @@ -198,31 +242,38 @@ def build_ie(version: str,
capabilities: The dict of capabilities.
experimental_options: The list of experimental options to include.
extension_paths: The list of extensions to add to the browser.
local_path: The path to the driver binary.
Examples:
driver = WebDriverFactory().build_ie('latest', ['headless'], None)
"""
caps = build_capabilities(Browser.IE, capabilities)
options = build_options(Browser.IE, browser_options, experimental_options, extension_paths)
if local_path:
return webdriver.Ie(executable_path=local_path, options=options, capabilities=caps)
return webdriver.Ie(executable_path=IEDriverManager(version=version).install(), options=options, capabilities=caps)


def build_opera(version: str,
browser_options: List[str],
experimental_options: Optional[List[dict]],
extension_paths: Optional[List[str]]) -> WebDriver:
extension_paths: Optional[List[str]],
local_path: Optional[str]) -> WebDriver:
""" Build an OperaDriver.
Args:
version: The desired version of Opera.
browser_options: The list of options/arguments to include.
experimental_options: The list of experimental options to include.
extension_paths: The list of extensions to add to the browser.
local_path: The path to the driver binary.
Examples:
driver = WebDriverFactory().build_opera('latest', ['--start-maximized'], None)
"""
options = build_options(Browser.OPERA, browser_options, experimental_options, extension_paths)
if local_path:
return webdriver.Opera(executable_path=local_path, options=options)
return webdriver.Opera(executable_path=OperaDriverManager(version=version).install(), options=options)


Expand Down

0 comments on commit 0e30017

Please sign in to comment.