Skip to content

Commit

Permalink
Add driver_args fixture for passing arguments to driver services
Browse files Browse the repository at this point in the history
Fixes #97
  • Loading branch information
davehunt committed Apr 28, 2017
1 parent 1dc679c commit 0096850
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/news.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Release Notes

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

* Add :code:`driver_args` fixture for adding command line arguments to the
driver services. Currently only used by Chrome and PhantomJS.

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

* Add capabilities to metadata during :code:`pytest_configure` hook instead of
Expand Down
31 changes: 31 additions & 0 deletions docs/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ an extension, and passing an argument to start Chrome in kiosk mode:
See the `Chrome options API documentation`_ for full details of what can be
configured.

The ChromeDriver supports various command line arguments. These can be passed
by implementing a ``driver_args`` fixture and returning a list of the desired
arguments. The following example specifies the log file path:

.. code-block:: python
import pytest
@pytest.fixture
def driver_args():
return ['--log-path=chromedriver.log']
For a full list of supported command line arguments, run
``chromedriver --help`` in your terminal.

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

Expand All @@ -188,6 +202,23 @@ the driver executable is not available on your path, you can use the
See the `PhantomJS documentation <http://phantomjs.org/quick-start.html>`_ for
more information.

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

PhantomJS supports various command line arguments. These can be passed by
implementing a ``driver_args`` fixture and returning a list of the desired
arguments. The following example specifies the log file path:

.. code-block:: python
import pytest
@pytest.fixture
def driver_args():
return ['--webdriver-logfile=phantomjs.log']
For a full list of supported command line arguments, run ``phantomjs --help``
in your terminal.

Safari
------

Expand Down
5 changes: 4 additions & 1 deletion pytest_selenium/drivers/chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
from selenium.webdriver.chrome.options import Options


def driver_kwargs(capabilities, driver_path, chrome_options, **kwargs):
def driver_kwargs(capabilities, driver_args, driver_path, chrome_options,
**kwargs):
kwargs = {
'desired_capabilities': capabilities,
'chrome_options': chrome_options}
if driver_args is not None:
kwargs['service_args'] = driver_args
if driver_path is not None:
kwargs['executable_path'] = driver_path
return kwargs
Expand Down
4 changes: 3 additions & 1 deletion pytest_selenium/drivers/phantomjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.


def driver_kwargs(capabilities, driver_path, **kwargs):
def driver_kwargs(capabilities, driver_args, driver_path, **kwargs):
kwargs = {'desired_capabilities': capabilities}
if driver_args is not None:
kwargs['service_args'] = driver_args
if driver_path is not None:
kwargs['executable_path'] = driver_path
return kwargs
11 changes: 9 additions & 2 deletions pytest_selenium/pytest_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,20 @@ def capabilities(request, session_capabilities):


@pytest.fixture
def driver_kwargs(request, capabilities, chrome_options, driver_class,
driver_path, firefox_options, firefox_profile):
def driver_args():
"""Return arguments to pass to the driver service"""
return None


@pytest.fixture
def driver_kwargs(request, capabilities, chrome_options, driver_args,
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_args=driver_args,
driver_path=driver_path,
firefox_options=firefox_options,
firefox_profile=firefox_profile,
Expand Down
17 changes: 17 additions & 0 deletions testing/test_chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# 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 os

import pytest

pytestmark = pytest.mark.nondestructive
Expand Down Expand Up @@ -36,3 +38,18 @@ def test_pass(selenium): pass
assert len(failed) == 1
out = failed[0].longrepr.reprcrash.message
assert 'no chrome binary at /foo/bar' in out


@pytest.mark.chrome
def test_args(testdir):
file_test = testdir.makepyfile("""
import pytest
@pytest.fixture
def driver_args():
return ['--log-path=foo.log']
@pytest.mark.nondestructive
def test_pass(selenium): pass
""")
testdir.quick_qa('--driver', 'Chrome', file_test, passed=1)
assert os.path.exists(testdir.tmpdir.join('foo.log'))
17 changes: 17 additions & 0 deletions testing/test_phantomjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# 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 os

import pytest

pytestmark = pytest.mark.nondestructive
Expand All @@ -17,3 +19,18 @@ def test_pass(webtext):
assert webtext == u'Success!'
""")
testdir.quick_qa('--driver', 'PhantomJS', file_test, passed=1)


@pytest.mark.phantomjs
def test_args(testdir):
file_test = testdir.makepyfile("""
import pytest
@pytest.fixture
def driver_args():
return ['--webdriver-logfile=foo.log']
@pytest.mark.nondestructive
def test_pass(selenium): pass
""")
testdir.quick_qa('--driver', 'PhantomJS', file_test, passed=1)
assert os.path.exists(testdir.tmpdir.join('foo.log'))

0 comments on commit 0096850

Please sign in to comment.