Skip to content

Commit

Permalink
Merge browser options within capabilities when using a remote driver. F…
Browse files Browse the repository at this point in the history
…ixes #160
  • Loading branch information
davehunt committed May 22, 2018
1 parent de4572d commit 9db9654
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
26 changes: 25 additions & 1 deletion pytest_selenium/pytest_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@
'TestingBot': webdriver.Remote})


def _merge(a, b):
""" merges b and a configurations.
Based on http://bit.ly/2uFUHgb
"""
for key in b:
if key in a:
if isinstance(a[key], dict) and isinstance(b[key], dict):
_merge(a[key], b[key], [] + [str(key)])
elif a[key] == b[key]:
pass # same leaf value
elif isinstance(a[key], list):
if isinstance(b[key], list):
a[key].extend(b[key])
else:
a[key].append(b[key])
else:
# b wins
a[key] = b[key]
else:
a[key] = b[key]
return a


def pytest_addhooks(pluginmanager):
from . import hooks
method = getattr(pluginmanager, 'add_hookspecs', None)
Expand Down Expand Up @@ -68,7 +91,8 @@ def capabilities(request, driver_class, chrome_options, firefox_options,
key = firefox_options.KEY
options = firefox_options.to_capabilities()
if all([key, options]):
capabilities.setdefault(key, {}).update(options.get(key, {}))
capabilities[key] = _merge(
capabilities.get(key, {}), options.get(key, {}))
capabilities_marker = request.node.get_marker('capabilities')
if capabilities_marker is not None:
# add capabilities from the marker
Expand Down
18 changes: 18 additions & 0 deletions testing/test_capabilities.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 json

import pytest

pytestmark = pytest.mark.nondestructive
Expand All @@ -26,6 +28,22 @@ def test_file(testfile, testdir):
testdir.quick_qa('--variables', variables, testfile, passed=1)


def test_file_remote(testdir):
key = 'goog:chromeOptions'
capabilities = {'browserName': 'chrome', key: {'args': ['foo']}}
variables = testdir.makefile('.json', '{{"capabilities": {}}}'.format(
json.dumps(capabilities)))
file_test = testdir.makepyfile("""
import pytest
@pytest.mark.nondestructive
def test_capabilities(session_capabilities, capabilities):
assert session_capabilities['{0}']['args'] == ['foo']
assert capabilities['{0}']['args'] == ['foo']
""".format(key))
testdir.quick_qa(
'--driver', 'Remote', '--variables', variables, file_test, passed=1)


def test_fixture(testfile, testdir):
testdir.makeconftest("""
import pytest
Expand Down

0 comments on commit 9db9654

Please sign in to comment.