-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathsaucelabs.py
More file actions
196 lines (157 loc) · 6.83 KB
/
saucelabs.py
File metadata and controls
196 lines (157 loc) · 6.83 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# This Source Code Form is subject to the terms of the Mozilla Public
# 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
import pytest
from selenium.webdriver.common.options import ArgOptions
from pytest_selenium.drivers.cloud import Provider
from pytest_selenium.exceptions import MissingCloudSettingError
class SauceLabs(Provider):
API = "https://api.{data_center}.saucelabs.com/v1/{username}/jobs/{session}"
JOB = "https://api.{data_center}.saucelabs.com/v1/jobs/{session}"
def __init__(self, data_center="us-west-1"):
super(SauceLabs, self).__init__()
self._data_center = data_center
@property
def auth(self):
return self.username, self.key
@property
def data_center(self):
try:
return self.get_setting("data_center", [], "options")
except MissingCloudSettingError:
return self._data_center
@property
def executor(self):
return f"https://ondemand.{self.data_center}.saucelabs.com/wd/hub"
@property
def username(self):
return self.get_credential(
"username", ["SAUCELABS_USERNAME", "SAUCELABS_USR", "SAUCE_USERNAME"]
)
@property
def key(self):
return self.get_credential(
"key", ["SAUCELABS_API_KEY", "SAUCELABS_PSW", "SAUCE_ACCESS_KEY"]
)
def uses_driver(self, driver):
return driver.lower() == self.name.lower()
@pytest.hookimpl(optionalhook=True)
def pytest_selenium_capture_debug(item, report, extra):
provider = SauceLabs(item.config.getini("saucelabs_data_center"))
if not provider.uses_driver(item.config.getoption("driver")):
return
pytest_html = item.config.pluginmanager.getplugin("html")
extra.append(pytest_html.extras.html(_video_html(item._driver.session_id)))
@pytest.hookimpl(optionalhook=True)
def pytest_selenium_runtest_makereport(item, report, summary, extra):
provider = SauceLabs(item.config.getini("saucelabs_data_center"))
if not provider.uses_driver(item.config.getoption("driver")):
return
passed = report.passed or (report.failed and hasattr(report, "wasxfail"))
session_id = item._driver.session_id
# Add the job URL to the summary
provider = SauceLabs(item.config.getini("saucelabs_data_center"))
job_url = get_job_url(item.config, provider, session_id)
summary.append("{0} Job: {1}".format(provider.name, job_url))
pytest_html = item.config.pluginmanager.getplugin("html")
# Add the job URL to the HTML report
extra.append(pytest_html.extras.url(job_url, "{0} Job".format(provider.name)))
# lazy import requests for projects that don't need requests
import requests
try:
# Update the job result
api_url = provider.API.format(
session=session_id,
username=provider.username,
data_center=provider.data_center,
)
job_info = requests.get(api_url, auth=provider.auth, timeout=10).json()
if report.when == "setup" or job_info.get("passed") is not False:
# Only update the result if it's not already marked as failed
data = json.dumps({"passed": passed})
requests.put(api_url, data=data, auth=provider.auth, timeout=10)
except Exception as e:
summary.append(
"WARNING: Failed to update {0} job status: {1}".format(provider.name, e)
)
def driver_kwargs(request, test, capabilities, **kwargs):
provider = SauceLabs(request.config.getini("saucelabs_data_center"))
_capabilities = capabilities
if os.getenv("SAUCELABS_W3C") == "true":
_capabilities = capabilities.setdefault("sauce:options", {})
_capabilities.setdefault("username", provider.username)
_capabilities.setdefault("accessKey", provider.key)
_capabilities.setdefault("name", test)
markers = [x.name for x in request.node.iter_markers()]
tags = _capabilities.get("tags", []) + markers
if tags:
_capabilities["tags"] = tags
return {
"command_executor": provider.executor,
"options": ArgOptions(),
}
def _video_html(session):
flash_vars = 'config={{\
"clip":{{\
"url":"https://assets.saucelabs.com/jobs/{session}/video.flv",\
"provider":"streamer",\
"autoPlay":false,\
"autoBuffering":true}},\
"play": {{\
"opacity":1,\
"label":null,\
"replayLabel":null}},\
"plugins":{{\
"streamer":{{\
"url":"https://cdn1.saucelabs.com/sauce_skin_deprecated\
/lib/flowplayer/flowplayer.pseudostreaming-3.2.13.swf",\
"queryString":"%%3Fstart%%3D%%24%%7Bstart%%7D"}},\
"controls":{{\
"mute":false,\
"volume":false,\
"backgroundColor":"rgba(0,0,0,0.7)"}}}},\
"playerId":"player{session}",\
"playlist":[{{\
"url":"https://assets.saucelabs.com/jobs/{session}/video.flv",\
"provider":"streamer",\
"autoPlay":false,\
"autoBuffering":true}}]}}'.format(session=session)
return (
f'<div id="player{session}" style="border:1px solid #e6e6e6; float:right; height:240px; margin-left:5px;'
'overflow:hidden; width:320px">'
'<object data="https://cdn1.saucelabs.com/sauce_skin_deprecated/lib/flowplayer/flowplayer-3.2.17.swf"'
'height="100%" id="player_api" name="player_api" type="application/x-shockwave-flash" width="100%">'
'<param name="allowfullscreen" value="true"/>'
'<param name="allowscriptaccess" value="always"/>'
'<param name="quality" value="high"/>'
'<param name="bgcolor" value="#000000"/>'
f'<param name="flashvars" value="{flash_vars.replace(" ", "")}"/>'
"</object>"
"</div>"
)
def get_job_url(config, provider, session_id):
from datetime import datetime
job_url = provider.JOB.format(session=session_id, data_center=provider.data_center)
job_auth = config.getini("saucelabs_job_auth").lower()
if job_auth == "none":
return job_url
if job_auth == "token":
return get_auth_url(job_url, provider, session_id)
elif job_auth == "hour":
time_format = "%Y-%m-%d-%H"
elif job_auth == "day":
time_format = "%Y-%m-%d"
else:
raise ValueError("Invalid authorization type: {}".format(job_auth))
ttl = datetime.utcnow().strftime(time_format)
return get_auth_url(job_url, provider, session_id, ttl)
def get_auth_url(url, provider, session_id, ttl=None):
import hmac
from hashlib import md5
key = "{0.username}:{0.key}".format(provider)
if ttl:
key += ":{}".format(ttl)
token = hmac.new(key.encode("utf-8"), session_id.encode("utf-8"), md5).hexdigest()
return "{}?auth={}".format(url, token)