This repository was archived by the owner on Dec 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathsauce-service.js
More file actions
197 lines (164 loc) · 5.41 KB
/
sauce-service.js
File metadata and controls
197 lines (164 loc) · 5.41 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
197
import request from 'request'
const jobDataProperties = ['name', 'tags', 'public', 'build', 'custom-data']
const jasmineTopLevelSuite = 'Jasmine__TopLevel__Suite'
const REGION_MAPPING = {
'eu': 'eu-central-1'
}
function getSauceEndpoint (region) {
const dc = region ? (REGION_MAPPING[region] || region) + '.' : ''
return `${dc}saucelabs.com`
}
class SauceService {
/**
* gather information about runner
*/
before (capabilities) {
this.sessionId = global.browser.sessionId
this.capabilities = capabilities
this.auth = global.browser.requestHandler.auth || {}
this.sauceUser = this.auth.user
this.sauceKey = this.auth.pass
this.testCnt = 0
this.failures = 0 // counts failures between reloads
}
beforeSession (config) {
this.hostname = getSauceEndpoint(config.region)
}
getSauceRestUrl (sessionId) {
return `https://${this.hostname}/rest/v1/${this.sauceUser}/jobs/${sessionId}`
}
beforeSuite (suite) {
this.suiteTitle = suite.title
}
beforeTest (test) {
if (!this.sauceUser || !this.sauceKey) {
return
}
/**
* in jasmine we get Jasmine__TopLevel__Suite as title since service using test
* framework hooks in order to execute async functions.
* This tweak allows us to set the real suite name for jasmine jobs.
*/
if (this.suiteTitle === jasmineTopLevelSuite) {
this.suiteTitle = test.fullName.slice(0, test.fullName.indexOf(test.title) - 1)
}
const context = test.parent === jasmineTopLevelSuite ? test.fullName : test.parent + ' - ' + test.title
global.browser.execute('sauce:context=' + context)
}
afterSuite (suite) {
if (suite.hasOwnProperty('err')) {
++this.failures
}
}
afterTest (test) {
if (!test.passed) {
++this.failures
}
}
beforeFeature (feature) {
if (!this.sauceUser || !this.sauceKey) {
return
}
this.suiteTitle = feature.name || feature.getName()
global.browser.execute('sauce:context=Feature: ' + this.suiteTitle)
}
afterStep (feature) {
if (
/**
* Cucumber v1
*/
feature.failureException ||
/**
* Cucumber v2
*/
(typeof feature.getFailureException === 'function' && feature.getFailureException()) ||
/**
* Cucumber v3, v4
*/
(feature.status === 'failed')
) {
++this.failures
}
}
beforeScenario (scenario) {
if (!this.sauceUser || !this.sauceKey) {
return
}
const scenarioName = scenario.name || scenario.getName()
global.browser.execute('sauce:context=Scenario: ' + scenarioName)
}
/**
* update Sauce Labs job
*/
after (result) {
if (!this.sauceUser || !this.sauceKey) {
return
}
let failures = this.failures
/**
* set failures if user has bail option set in which case afterTest and
* afterSuite aren't executed before after hook
*/
if (global.browser.config.mochaOpts && global.browser.config.mochaOpts.bail && Boolean(result)) {
failures = 1
}
// const status = 'status: ' + (failures > 0 ? 'failing' : 'passing')
if (!global.browser.isMultiremote) {
// log.info(`Update job with sessionId ${global.browser.sessionId}, ${status}`)
return this.updateJob(global.browser.sessionId, failures)
}
return Promise.all(Object.keys(this.capabilities).map((browserName) => {
// log.info(`Update multiremote job for browser "${browserName}" and sessionId ${global.browser[browserName].sessionId}, ${status}`)
return this.updateJob(global.browser[browserName].sessionId, failures, false, browserName)
}))
}
onReload (oldSessionId, newSessionId) {
if (!this.sauceUser || !this.sauceKey) {
return
}
this.sessionId = newSessionId
return this.updateJob(oldSessionId, this.failures, true)
}
updateJob (sessionId, failures, calledOnReload = false) {
return new Promise((resolve, reject) => request.put(this.getSauceRestUrl(sessionId), {
json: true,
auth: {
user: this.sauceUser,
pass: this.sauceKey
},
body: this.getBody(failures, calledOnReload)
}, (e, res, body) => {
if (e) {
return reject(e)
}
global.browser.jobData = body
this.failures = 0
resolve(body)
}))
}
/**
* massage data
*/
getBody (failures, calledOnReload = false) {
let body = {}
/**
* set default values
*/
body.name = this.suiteTitle
/**
* add reload count to title if reload is used
*/
if (calledOnReload || this.testCnt) {
body.name += ` (${++this.testCnt})`
}
for (let prop of jobDataProperties) {
if (!this.capabilities[prop]) {
continue
}
body[prop] = this.capabilities[prop]
}
body.passed = failures === 0
return body
}
}
export default SauceService