Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options parameter to start_firefox(...) #22

Merged
merged 1 commit into from May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions helium/__init__.py
Expand Up @@ -40,12 +40,14 @@
'SPACE', 'SUBTRACT', 'TAB', 'UP'
]

def start_firefox(url=None, headless=False):
def start_firefox(url=None, headless=False, options=None):
"""
:param url: URL to open.
:type url: str
:param headless: Whether to start Firefox in headless mode.
:type headless: bool
:param options: FirefoxOptions to use for starting the browser.
:type options: :py:class:`selenium.webdriver.FirefoxOptions`

Starts an instance of Firefox. You can optionally open a URL::

Expand All @@ -58,14 +60,22 @@ def start_firefox(url=None, headless=False):
start_firefox(headless=True)
start_firefox("google.com", headless=True)

For more advanced configuration, use the `options` parameter::

from selenium.webdriver import FirefoxOptions
options = FirefoxOptions()
options.add_argument("--width=2560")
options.add_argument("--height=1440")
start_firefox(options=options)

On shutdown of the Python interpreter, Helium cleans up all resources used
for controlling the browser (such as the geckodriver process), but does
not close the browser itself. If you want to terminate the browser at the
end of your script, use the following command::

kill_browser()
"""
return _get_api_impl().start_firefox_impl(url, headless)
return _get_api_impl().start_firefox_impl(url, headless, options)

def start_chrome(url=None, headless=False, options=None):
"""
Expand Down
12 changes: 6 additions & 6 deletions helium/_impl/__init__.py
Expand Up @@ -74,17 +74,17 @@ class APIImpl:
" * set_driver(...)"
def __init__(self):
self.driver = None
def start_firefox_impl(self, url=None, headless=False):
firefox_driver = self._start_firefox_driver(headless)
def start_firefox_impl(self, url=None, headless=False, options=None):
firefox_driver = self._start_firefox_driver(headless, options)
return self._start(firefox_driver, url)
def _start_firefox_driver(self, headless):
firefox_options = self._get_firefox_options(headless)
def _start_firefox_driver(self, headless, options):
firefox_options = self._get_firefox_options(headless, options)
kwargs = self._get_firefox_driver_kwargs(firefox_options)
result = Firefox(**kwargs)
atexit.register(self._kill_service, result.service)
return result
def _get_firefox_options(self, headless):
result = FirefoxOptions()
def _get_firefox_options(self, headless, options):
result = FirefoxOptions() if options is None else options
if headless:
result.headless = True
return result
Expand Down