Skip to content

Commit

Permalink
Add support for Sauce Labs W3C compliant capabilities (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
BeyondEvil authored and davehunt committed May 25, 2018
1 parent 0f474e4 commit 59b45f2
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
10 changes: 10 additions & 0 deletions docs/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,14 @@ the ``--capability`` command line arguments. See the
`test configuration documentation <https://docs.saucelabs.com/reference/test-configuration/>`_
for full details of what can be configured.

W3C compliant capabilities
~~~~~~~~~~~~~~~~~~~~~~~~~~

Starting with selenium version 3.11.0 Sauce Labs supports the selenium W3C compliant capabilities.
To use the W3C capabilities you have to set the ``SAUCELABS_W3C`` environment variable to ``true``
and update your :ref:`capabilities <capabilities>` according to the Sauce labs
`W3C documentation <https://wiki.saucelabs.com/display/DOCS/Selenium+W3C+Capabilities+Support+-+Beta>`_.

Test result links
~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -486,6 +494,8 @@ arguments. See the
`automation capabilities <https://help.crossbrowsertesting.com/selenium-testing/general/crossbrowsertesting-automation-capabilities/>`_
for full details of what can be configured.

.. _capabilities:

Specifying Capabilities
***********************

Expand Down
13 changes: 10 additions & 3 deletions pytest_selenium/drivers/saucelabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# 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 json

from _pytest.mark import MarkInfo
Expand Down Expand Up @@ -89,11 +90,17 @@ def pytest_selenium_runtest_makereport(item, report, summary, extra):
def driver_kwargs(request, test, capabilities, **kwargs):
provider = SauceLabs()
keywords = request.node.keywords
capabilities.setdefault('name', test)
markers = [m for m in keywords.keys() if isinstance(keywords[m], MarkInfo)]
tags = capabilities.get('tags', []) + markers

_capabilities = capabilities
if os.getenv('SAUCELABS_W3C') == 'true':
_capabilities = capabilities.setdefault('sauce:options', {})

_capabilities.setdefault('name', test)
tags = _capabilities.get('tags', []) + markers
if tags:
capabilities['tags'] = tags
_capabilities['tags'] = tags

kwargs = {
'command_executor': provider.executor,
'desired_capabilities': capabilities}
Expand Down
68 changes: 68 additions & 0 deletions testing/test_capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,71 @@ def test_capabilities(session_capabilities, capabilities):
assert capabilities['foo'] == 'bar'
""")
testdir.quick_qa(file_test, passed=1)


def test_no_sauce_options(monkeypatch, testdir):
monkeypatch.setenv('SAUCELABS_USERNAME', 'foo')
monkeypatch.setenv('SAUCELABS_API_KEY', 'bar')

capabilities = {'browserName': 'chrome'}
variables = testdir.makefile('.json', '{{"capabilities": {}}}'.format(
json.dumps(capabilities)))

file_test = testdir.makepyfile("""
import pytest
@pytest.mark.nondestructive
def test_sauce_capabilities(driver_kwargs):
try:
driver_kwargs['desired_capabilities']['sauce:options']
raise AssertionError('<sauce:options> should not be present!')
except KeyError:
pass
""")

testdir.quick_qa(
'--driver', 'saucelabs', '--variables',
variables, file_test, passed=1)


def test_empty_sauce_options(monkeypatch, testdir):
capabilities = {'browserName': 'chrome'}
expected = {'name': 'test_empty_sauce_options.test_sauce_capabilities',
'tags': ['nondestructive']}
run_sauce_test(capabilities, expected, monkeypatch, testdir)


def test_merge_sauce_options(monkeypatch, testdir):
version = {'seleniumVersion': '3.8.1'}
capabilities = {'browserName': 'chrome', 'sauce:options': version}
expected = {'name': 'test_merge_sauce_options.test_sauce_capabilities',
'tags': ['nondestructive']}
expected.update(version)
run_sauce_test(capabilities, expected, monkeypatch, testdir)


def test_merge_sauce_options_with_conflict(monkeypatch, testdir):
name = 'conflict'
capabilities = {'browserName': 'chrome', 'sauce:options': {'name': name}}
expected = {'name': name, 'tags': ['nondestructive']}
run_sauce_test(capabilities, expected, monkeypatch, testdir)


def run_sauce_test(capabilities, expected_result, monkeypatch, testdir):
monkeypatch.setenv('SAUCELABS_USERNAME', 'foo')
monkeypatch.setenv('SAUCELABS_API_KEY', 'bar')
monkeypatch.setenv('SAUCELABS_W3C', 'true')

variables = testdir.makefile('.json', '{{"capabilities": {}}}'.format(
json.dumps(capabilities)))

file_test = testdir.makepyfile("""
import pytest
@pytest.mark.nondestructive
def test_sauce_capabilities(driver_kwargs):
actual = driver_kwargs['desired_capabilities']['sauce:options']
assert actual == {}
""".format(expected_result))

testdir.quick_qa(
'--driver', 'saucelabs', '--variables',
variables, file_test, passed=1)

0 comments on commit 59b45f2

Please sign in to comment.