-
Notifications
You must be signed in to change notification settings - Fork 128
/
webdriver.js
299 lines (273 loc) · 8.16 KB
/
webdriver.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* MIT License
*
* Copyright (c) 2017-present, Elasticsearch BV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
const { join } = require('path')
const glob = require('glob')
const { getSauceConnectOptions, getBrowserList } = require('./test-config')
const logLevels = {
ALL: { value: Number.MIN_VALUE },
DEBUG: { value: 700 },
INFO: { value: 800 },
WARNING: { value: 900 },
SEVERE: { value: 1000 },
OFF: { value: Number.MAX_VALUE }
}
const debugMode = false
const debugLevel = logLevels.INFO.value
function isChrome() {
const browserName = browser.capabilities.browserName.toLowerCase()
return browserName.indexOf('chrome') !== -1
}
function assertNoBrowserErrors(whitelist) {
return new Promise((resolve, reject) => {
/**
* browser.log API is available only in chrome
*/
if (!isChrome()) {
return resolve()
}
var response = browser.getLogs('browser')
var failureEntries = []
var debugLogs = []
var browserLog = response
for (var i = 0; i < browserLog.length; i++) {
var logEntry = browserLog[i]
if (isLogEntryATestFailure(logEntry, whitelist)) {
failureEntries.push(logEntry)
}
if (logLevels[logEntry.level].value >= debugLevel) {
debugLogs.push(logEntry)
}
}
if (failureEntries.length > 0 || debugMode) {
console.log('------------> FailuresLogs')
console.log(JSON.stringify(failureEntries, undefined, 2))
}
if (debugMode) {
console.log('------------> debugLogs')
console.log(JSON.stringify(debugLogs, undefined, 2))
}
if (failureEntries.length > 0) {
return reject(
new Error(
'Expected no errors in the browserLog but got ' +
failureEntries.length +
' error(s)'
)
)
}
resolve()
})
}
function isLogEntryATestFailure(entry, whitelist) {
var result = false
if (logLevels[entry.level].value > logLevels.WARNING.value) {
result = true
if (whitelist) {
for (var i = 0, l = whitelist.length; i < l; i++) {
if (entry.message.indexOf(whitelist[i]) !== -1) {
result = false
break
}
}
}
}
return result
}
function getWebdriveBaseConfig(
path,
specs = './test/e2e/**/*.e2e-spec.js',
capabilities
) {
const { tunnelIdentifier, username, accessKey } = getSauceConnectOptions()
if (!capabilities) {
/**
* Skip the ios platform on E2E tests because of script
* timeout issue in Appium
*/
capabilities = getBrowserList().filter(
({ platformName }) => platformName !== 'iOS'
)
}
capabilities = capabilities.map(capability => ({
tunnelIdentifier,
...capability
}))
return {
runner: 'local',
specs: glob.sync(join(path, specs)),
maxInstancesPerCapability: 3,
services: ['sauce'],
user: username,
key: accessKey,
sauceConnect: false,
capabilities,
logLevel: 'error',
bail: 1,
screenshotPath: join(path, 'error-screenshot'),
baseUrl: 'http://localhost:8000',
waitforTimeout: 30000,
framework: 'jasmine',
reporters: ['dot', 'spec'],
jasmineNodeOpts: {
defaultTimeoutInterval: 90000
},
beforeTest() {
/**
* Sets timeout for scripts executed in the browser
* via browser.executeAsync method
*/
browser.setTimeout({ script: 20000 })
},
afterTest(test) {
/**
* Log only on failures
* Log api is only available in chrome driver
* */
if (!test.passed && isChrome()) {
const response = browser.getLogs('browser')
console.log('[Browser Logs]:', JSON.stringify(response, undefined, 2))
}
}
}
}
function waitForApmServerCalls(errorCount = 0, transactionCount = 0) {
console.log(
`Waiting for minimum ${errorCount} Errors and ${transactionCount} Transactions.`
)
const serverCalls = browser.executeAsync(
function(errorCount, transactionCount, done) {
var apmServerMock = window.elasticApm.serviceFactory.getService(
'ApmServer'
)
function checkCalls() {
var serverCalls = apmServerMock.calls
var validCalls = true
if (errorCount) {
validCalls =
validCalls &&
serverCalls.sendErrors &&
serverCalls.sendErrors.length >= errorCount
}
if (transactionCount) {
validCalls =
validCalls &&
serverCalls.sendTransactions &&
serverCalls.sendTransactions.length >= transactionCount
}
if (validCalls) {
console.log('calls', serverCalls)
var promises = []
if (serverCalls.sendErrors) {
promises = promises.concat(
serverCalls.sendErrors.map(function(s) {
return s.returnValue
})
)
}
if (serverCalls.sendTransactions) {
promises = promises.concat(
serverCalls.sendTransactions.map(function(s) {
return s.returnValue
})
)
}
Promise.all(promises)
.then(function() {
function mapCall(c) {
return { args: c.args, mocked: c.mocked }
}
try {
var calls = {
sendErrors: serverCalls.sendErrors
? serverCalls.sendErrors.map(mapCall)
: undefined,
sendTransactions: serverCalls.sendTransactions
? serverCalls.sendTransactions.map(mapCall)
: undefined
}
done(calls)
} catch (e) {
throw e
}
})
.catch(function(reason) {
console.log('reason', reason)
try {
done({ error: reason.message || JSON.stringify(reason) })
} catch (e) {
done({
error: 'Failed serializing rejection reason: ' + e.message
})
}
})
}
}
checkCalls()
apmServerMock.subscription.subscribe(checkCalls)
},
errorCount,
transactionCount
)
if (!serverCalls) {
throw new Error('serverCalls is undefined!')
}
console.log(JSON.stringify(serverCalls, null, 2))
if (serverCalls.error) {
fail(serverCalls.error)
}
return serverCalls
}
module.exports = {
allowSomeBrowserErrors: function allowSomeBrowserErrors(whitelist, done) {
if (typeof done === 'function') {
assertNoBrowserErrors(whitelist).then(() => {
done()
})
} else {
return assertNoBrowserErrors(whitelist)
}
},
verifyNoBrowserErrors: function verifyNoBrowserErrors(done) {
if (typeof done === 'function') {
assertNoBrowserErrors([]).then(() => {
done()
})
} else {
return assertNoBrowserErrors([])
}
},
handleError: function handleError(done) {
return function(error) {
console.log(error, error.stack)
if (error.message.indexOf('received Inspector.detached event') === -1) {
done.fail(error)
}
// done()
}
},
isChrome,
getWebdriveBaseConfig,
waitForApmServerCalls
}