Skip to content

Commit

Permalink
Add chrome_options fixture for configuring Google Chrome
Browse files Browse the repository at this point in the history
Fixes #116
  • Loading branch information
davehunt committed Apr 27, 2017
1 parent 0d6a775 commit 1dc679c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/news.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Release Notes
* Only create a Firefox profile if :code:`--firefox-extension`,
:code:`--firefox-preference`, or :code:`--firefox-profile` is specified.

* Add :code:`chrome_options` fixture for configuring Google Chrome.

**1.9.1 (2017-03-01)**

* Add capabilities to metadata during :code:`pytest_configure` hook instead of
Expand Down
21 changes: 21 additions & 0 deletions docs/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,26 @@ option to indicate where it can be found::
See the `ChromeDriver documentation <https://sites.google.com/a/chromium.org/chromedriver/>`_
for more information.

Configuration
~~~~~~~~~~~~~

A ``chrome_options`` fixture is available to configure various options for
Chrome. The following example demonstrates specifying a binary path, adding
an extension, and passing an argument to start Chrome in kiosk mode:

.. code-block:: python
import pytest
@pytest.fixture
def chrome_options(chrome_options):
chrome_options.binary_location = '/path/to/chrome'
chrome_options.add_extension('/path/to/extension.crx')
chrome_options.add_argument('--kiosk')
return chrome_options
See the `Chrome options API documentation`_ for full details of what can be
configured.

Internet Explorer
-----------------

Expand Down Expand Up @@ -515,3 +535,4 @@ set ``SELENIUM_EXCLUDE_DEBUG`` to ``html:logs:screenshot``.
.. _Jenkins CI: https://jenkins.io/
.. _using environment variables in Jenkins pipelines: https://jenkins.io/doc/pipeline/tour/environment/
.. _Firefox options API documentation: https://seleniumhq.github.io/selenium/docs/api/py/webdriver_firefox/selenium.webdriver.firefox.options.html
.. _Chrome options API documentation: https://seleniumhq.github.io/selenium/docs/api/py/webdriver_chrome/selenium.webdriver.chrome.options.html
14 changes: 12 additions & 2 deletions pytest_selenium/drivers/chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import pytest
from selenium.webdriver.chrome.options import Options

def driver_kwargs(capabilities, driver_path, **kwargs):
kwargs = {'desired_capabilities': capabilities}

def driver_kwargs(capabilities, driver_path, chrome_options, **kwargs):
kwargs = {
'desired_capabilities': capabilities,
'chrome_options': chrome_options}
if driver_path is not None:
kwargs['executable_path'] = driver_path
return kwargs


@pytest.fixture
def chrome_options():
return Options()
5 changes: 3 additions & 2 deletions pytest_selenium/pytest_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ def capabilities(request, session_capabilities):


@pytest.fixture
def driver_kwargs(request, capabilities, driver_class, driver_path,
firefox_options, firefox_profile):
def driver_kwargs(request, capabilities, chrome_options, driver_class,
driver_path, firefox_options, firefox_profile):
kwargs = {}
driver = request.config.getoption('driver').lower()
kwargs.update(getattr(drivers, driver).driver_kwargs(
capabilities=capabilities,
chrome_options=chrome_options,
driver_path=driver_path,
firefox_options=firefox_options,
firefox_profile=firefox_profile,
Expand Down
19 changes: 19 additions & 0 deletions testing/test_chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,22 @@ def test_pass(webtext):
assert webtext == u'Success!'
""")
testdir.quick_qa('--driver', 'Chrome', file_test, passed=1)


@pytest.mark.chrome
def test_options(testdir):
testdir.makepyfile("""
import pytest
@pytest.fixture
def chrome_options(chrome_options):
chrome_options.binary_location = '/foo/bar'
return chrome_options
@pytest.mark.nondestructive
def test_pass(selenium): pass
""")
reprec = testdir.inline_run('--driver', 'Chrome')
passed, skipped, failed = reprec.listoutcomes()
assert len(failed) == 1
out = failed[0].longrepr.reprcrash.message
assert 'no chrome binary at /foo/bar' in out

0 comments on commit 1dc679c

Please sign in to comment.