From a16b486f1ebb39e4ebc7fb7e7d830c9c38798654 Mon Sep 17 00:00:00 2001 From: joshgrant Date: Wed, 4 Dec 2019 15:47:51 -0500 Subject: [PATCH 1/3] basic idea --- conftest.py | 94 +++++++++--------------------------------------- requirements.txt | 1 + 2 files changed, 17 insertions(+), 78 deletions(-) diff --git a/conftest.py b/conftest.py index 0235f33..c42b904 100644 --- a/conftest.py +++ b/conftest.py @@ -1,95 +1,33 @@ import pytest -from os import environ -from selenium import webdriver -from selenium.common.exceptions import WebDriverException -from selenium.webdriver.remote.remote_connection import RemoteConnection +from simplesauce.options import SauceOptions +from simplesauce.session import SauceSession import urllib3 urllib3.disable_warnings() browsers = [ - { - "seleniumVersion": '3.4.0', - "platform": "Windows 10", - "browserName": "MicrosoftEdge", - "version": "latest" - }, { - "seleniumVersion": '3.4.0', - "platform": "Windows 10", - "browserName": "firefox", - "version": "latest" - }, { - "seleniumVersion": '3.4.0', - "platform": "Windows 7", - "browserName": "internet explorer", - "version": "latest" - }, { - "seleniumVersion": '3.4.0', - "platform": "OS X 10.13", - "browserName": "safari", - "version": "latest-1" - }, { - "seleniumVersion": '3.4.0', - "platform": "OS X 10.11", - "browserName": "chrome", - "version": "latest", - "extendedDebugging": True - }] + 'internet explorer', + 'chrome', + 'firefox', + 'safari', + 'edge' +] -def pytest_generate_tests(metafunc): - if 'driver' in metafunc.fixturenames: - metafunc.parametrize('browser_config', - browsers, - ids=_generate_param_ids('broswerConfig', browsers), - scope='function') +@pytest.fixture(params=browsers) +def driver(request): + opts = SauceOptions(browserName=request.param) + sauce = SauceSession(options=opts) + sauce.start() -def _generate_param_ids(name, values): - return [("<%s:%s>" % (name, value)).replace('.', '_') for value in values] + yield sauce.driver - -@pytest.yield_fixture(scope='function') -def driver(request, browser_config): - # if the assignment below does not make sense to you please read up on object assignments. - # The point is to make a copy and not mess with the original test spec. - desired_caps = dict() - desired_caps.update(browser_config) - test_name = request.node.name - build_tag = environ.get('BUILD_TAG', None) - tunnel_id = environ.get('TUNNEL_IDENTIFIER', None) - username = environ.get('SAUCE_USERNAME', None) - access_key = environ.get('SAUCE_ACCESS_KEY', None) - - selenium_endpoint = "https://%s:%s@ondemand.saucelabs.com:443/wd/hub" % (username, access_key) - desired_caps['build'] = build_tag - # we can move this to the config load or not, also messing with this on a test to test basis is possible :) - desired_caps['tunnelIdentifier'] = tunnel_id - desired_caps['name'] = test_name - - executor = RemoteConnection(selenium_endpoint, resolve_ip=False) - browser = webdriver.Remote( - command_executor=executor, - desired_capabilities=desired_caps, - keep_alive=True - ) - - # This is specifically for SauceLabs plugin. - # In case test fails after selenium session creation having this here will help track it down. - # creates one file per test non ideal but xdist is awful - if browser is not None: - print("SauceOnDemandSessionID={} job-name={}".format(browser.session_id, test_name)) - else: - raise WebDriverException("Never created!") - - yield browser - # Teardown starts here # report results # use the test result to send the pass/fail status to Sauce Labs sauce_result = "failed" if request.node.rep_call.failed else "passed" - browser.execute_script("sauce:job-result={}".format(sauce_result)) - browser.quit() - + sauce.driver.execute_script("sauce:job-result={}".format(sauce_result)) + sauce.stop() @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_makereport(item, call): diff --git a/requirements.txt b/requirements.txt index 98d5aac..c50f8a5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ selenium==3.14.0 sauceclient>=0.2.1 +simplesauce pytest==4.4.0 pytest-xdist requests From 46e2345e39fdf2b7aa293fda3748e6e52ed7a61c Mon Sep 17 00:00:00 2001 From: joshgrant Date: Wed, 4 Dec 2019 16:17:31 -0500 Subject: [PATCH 2/3] better reporting --- conftest.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/conftest.py b/conftest.py index c42b904..e56f115 100644 --- a/conftest.py +++ b/conftest.py @@ -25,8 +25,11 @@ def driver(request): # report results # use the test result to send the pass/fail status to Sauce Labs - sauce_result = "failed" if request.node.rep_call.failed else "passed" - sauce.driver.execute_script("sauce:job-result={}".format(sauce_result)) + if request.node.rep_call.failed: + sauce.driver.execute_script("sauce:job-result=failed") + else: + sauce.driver.execute_script("sauce:job-result=passed") + sauce.stop() @pytest.hookimpl(hookwrapper=True, tryfirst=True) From 9add39a7b30fd3fdce70826f35bccbbfdcb11d36 Mon Sep 17 00:00:00 2001 From: joshgrant Date: Thu, 5 Dec 2019 10:03:15 -0500 Subject: [PATCH 3/3] update for latest --- conftest.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/conftest.py b/conftest.py index e56f115..8ada03f 100644 --- a/conftest.py +++ b/conftest.py @@ -10,7 +10,6 @@ 'internet explorer', 'chrome', 'firefox', - 'safari', 'edge' ] @@ -18,6 +17,7 @@ @pytest.fixture(params=browsers) def driver(request): opts = SauceOptions(browserName=request.param) + opts.name = request.node.name sauce = SauceSession(options=opts) sauce.start() @@ -26,10 +26,10 @@ def driver(request): # report results # use the test result to send the pass/fail status to Sauce Labs if request.node.rep_call.failed: - sauce.driver.execute_script("sauce:job-result=failed") + sauce.update_test_result('failed') else: - sauce.driver.execute_script("sauce:job-result=passed") - + sauce.update_test_result('passed') + sauce.stop() @pytest.hookimpl(hookwrapper=True, tryfirst=True)