forked from sauce-archives/Python-Pytest-Selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
64 lines (53 loc) · 2.39 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pytest
from os import environ
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.remote.remote_connection import RemoteConnection
@pytest.fixture(scope='function')
def driver(request):
# 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 = {}
browser = {
"platform": "Windows 10",
"browserName": "firefox",
"version": "49.0"
}
desired_caps.update(browser)
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 = "http://{}:{}@ondemand.saucelabs.com:80/wd/hub".format(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
)
yield browser
# 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:
with open("{}.testlog".format(browser.session_id), 'w') as f:
f.write("SauceOnDemandSessionID={} job-name={}\n".format(browser.session_id, test_name))
else:
raise WebDriverException("Never created!")
def fin():
browser.execute_script("sauce:job-result={}".format(str(not request.node.rep_call.failed).lower()))
browser.quit()
request.addfinalizer(fin)
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
# this sets the result as a test attribute for SauceLabs reporting.
# execute all other hooks to obtain the report object
outcome = yield
rep = outcome.get_result()
# set an report attribute for each phase of a call, which can
# be "setup", "call", "teardown"
setattr(item, "rep_" + rep.when, rep)