-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathservice.js
216 lines (178 loc) · 6.35 KB
/
service.js
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import SauceLabs from 'saucelabs'
import logger from '@wdio/logger'
const jobDataProperties = ['name', 'tags', 'public', 'build', 'custom-data']
const log = logger('@wdio/sauce-service')
export default class SauceService {
constructor (options) {
this.testCnt = 0
this.failures = 0 // counts failures between reloads
this.options = options || {}
}
/**
* gather information about runner
*/
beforeSession (config, capabilities) {
this.config = config
this.capabilities = capabilities
this.api = new SauceLabs(this.config)
this.isRDC = 'testobject_api_key' in this.capabilities
this.isServiceEnabled = true
/**
* if no user and key is specified even though a sauce service was
* provided set user and key with values so that the session request
* will fail (not for RDC tho due to other auth mechansim)
*/
if (!this.isRDC && !config.user) {
this.isServiceEnabled = false
config.user = 'unknown_user'
}
if (!this.isRDC && !config.key) {
this.isServiceEnabled = false
config.key = 'unknown_key'
}
}
beforeSuite (suite) {
this.suiteTitle = suite.title
if (this.options.setJobNameInBeforeSuite) {
global.browser.execute('sauce:job-name=' + this.suiteTitle)
}
}
beforeTest (test) {
if (!this.isServiceEnabled || this.isRDC) {
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.
*/
/* istanbul ignore if */
if (this.suiteTitle === 'Jasmine__TopLevel__Suite') {
this.suiteTitle = test.fullName.slice(0, test.fullName.indexOf(test.title) - 1)
}
const fullTitle = (
/**
* Jasmine
*/
test.fullName ||
/**
* Mocha
*/
`${test.parent} - ${test.title}`
)
global.browser.execute('sauce:context=' + fullTitle)
}
afterSuite (suite) {
if (Object.prototype.hasOwnProperty.call(suite, 'error')) {
++this.failures
}
}
afterTest (test, context, results) {
if (!results.passed) {
++this.failures
}
}
/**
* For CucumberJS
*/
beforeFeature (uri, feature) {
if (!this.isServiceEnabled || this.isRDC) {
return
}
this.suiteTitle = feature.document.feature.name
global.browser.execute('sauce:context=Feature: ' + this.suiteTitle)
}
beforeScenario (uri, feature, scenario) {
if (!this.isServiceEnabled || this.isRDC) {
return
}
const scenarioName = scenario.name
global.browser.execute('sauce:context=Scenario: ' + scenarioName)
}
afterScenario(uri, feature, pickle, result) {
if (result.status === 'failed') {
++this.failures
}
}
/**
* update Sauce Labs job
*/
after (result) {
if (!this.isServiceEnabled && !this.isRDC) {
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.isServiceEnabled && !this.isRDC) {
return
}
const status = 'status: ' + (this.failures > 0 ? 'failing' : 'passing')
if (!global.browser.isMultiremote) {
log.info(`Update (reloaded) job with sessionId ${oldSessionId}, ${status}`)
return this.updateJob(oldSessionId, this.failures, true)
}
const browserName = global.browser.instances.filter(
(browserName) => global.browser[browserName].sessionId === newSessionId)[0]
log.info(`Update (reloaded) multiremote job for browser "${browserName}" and sessionId ${oldSessionId}, ${status}`)
return this.updateJob(oldSessionId, this.failures, true, browserName)
}
async updateJob (sessionId, failures, calledOnReload = false, browserName) {
if (this.isRDC) {
await this.api.updateTest(sessionId, { passed: failures === 0 })
this.failures = 0
return
}
const body = this.getBody(failures, calledOnReload, browserName)
await this.api.updateJob(this.config.user, sessionId, body)
this.failures = 0
}
/**
* VM message data
*/
getBody (failures, calledOnReload = false, browserName) {
let body = {}
/**
* set default values
*/
body.name = this.suiteTitle
if (browserName) {
body.name = `${browserName}: ${body.name}`
}
/**
* add reload count to title if reload is used
*/
if (calledOnReload || this.testCnt) {
let testCnt = ++this.testCnt
if (global.browser.isMultiremote) {
testCnt = Math.ceil(testCnt / global.browser.instances.length)
}
body.name += ` (${testCnt})`
}
let caps = this.capabilities['sauce:options'] || this.capabilities
for (let prop of jobDataProperties) {
if (!caps[prop]) {
continue
}
body[prop] = caps[prop]
}
body.passed = failures === 0
return body
}
}