-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browserstack.js
321 lines (270 loc) · 7.77 KB
/
browserstack.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* eslint-disable no-console,no-await-in-loop */
const webdriver = require('selenium-webdriver')
const BrowserStackLocal = require('browserstack-local')
const dotenv = require('dotenv')
const path = require('path')
const fs = require('fs')
const kleur = require('kleur')
const {server, port, start, stop} = require('./tests/server')
if (!process.env.TRAVIS) {
dotenv.config()
}
const CAPABILITIES = [
{
browserName: 'Chrome',
browserVersion: '55',
os: 'Windows',
osVersion: '10',
},
{
browserName: 'Firefox',
browserVersion: '52',
os: 'Windows',
osVersion: '10',
},
{
browserName: 'Edge',
browserVersion: '15',
os: 'Windows',
osVersion: '10',
},
{
browserName: 'Safari',
browserVersion: '10',
os: 'OS X',
osVersion: 'Sierra',
},
]
const TESTS_DIST = './tests/dist'
const TEST_FILE_NAME_MARKER = '.html'
const bsLocal = new BrowserStackLocal.Local()
const bsOptions = {
key: process.env.BROWSERSTACK_KEY,
}
const startBsLocal = () => {
return new Promise((resolve, reject) => {
console.log('Starting BrowserStackLocal...')
try {
bsLocal.start(bsOptions, () => {
// Check if BrowserStack local instance is running
console.log('BrowserStackLocal running:', bsLocal.isRunning())
resolve()
})
} catch (e) {
reject(e)
}
})
}
const stopBsLocal = () => {
return new Promise((resolve, reject) => {
console.log('Stopping BrowserStackLocal...')
try {
bsLocal.stop(() => {
console.log('Stopped BrowserStackLocal')
resolve()
})
} catch (e) {
reject(e)
}
})
}
const formatTestResults = (testResults) => {
const formattedTestEnds = testResults
.map((testResult) => {
const {name, status, runtime} = testResult
const statusStr = status === 'passed' ? kleur.green('✓') : kleur.red('✕')
const nameStr = kleur.dim(`${name} (${runtime} ms`)
return `${statusStr} ${nameStr})`
})
.map((line) => ` ${line}`)
.join('\n')
return formattedTestEnds
}
const getCapabilityInfo = (capability) => {
// eslint-disable-next-line max-len
return `[${capability.os} ${capability.osVersion}, ${capability.browserName} ${capability.browserVersion}]`
}
const printResult = (context, result) => {
const {moduleEnds, testEnds} = result
if (moduleEnds.length > 1) {
throw new Error('Function prints results only for one module')
}
const moduleEnd = moduleEnds[0]
const formattedTestEnds = formatTestResults(testEnds)
console.log(`${moduleEnd.name}\n${formattedTestEnds}`)
}
const printResults = (results) => {
const moduleEnds = results.map((result) => {
const {moduleEnds} = result
const moduleEnd = moduleEnds[0]
return moduleEnd
})
const passedModules = moduleEnds.filter(
(moduleEnd) => moduleEnd.status === 'passed',
)
const notPassedModules = moduleEnds.filter(
(moduleEnd) => moduleEnd.status !== 'passed',
)
const testEnds = results
.map(({testEnds}) => {
return testEnds
})
.flat()
const passedTests = testEnds.filter((testEnd) => testEnd.status === 'passed')
const notPassedTests = testEnds.filter(
(testEnd) => testEnd.status !== 'passed',
)
const formatTestsString = (passed, notPassed, total) => {
const testModulesStringsArr = []
if (notPassed.length > 0) {
testModulesStringsArr.push(kleur.red(`${notPassed.length} failed`))
}
testModulesStringsArr.push(kleur.green(`${passed.length} passed`))
testModulesStringsArr.push(`${total.length} total`)
return testModulesStringsArr.join(', ')
}
console.log(
`Test modules:\t${formatTestsString(
passedModules,
notPassedModules,
moduleEnds,
)}`,
)
console.log(
`Tests:\t\t${formatTestsString(passedTests, notPassedTests, testEnds)}`,
)
return notPassedModules.length === 0
}
const addScriptletsData = (capability) => {
const resultCapability = {
...capability,
project: 'Scriptlets',
name: `${getCapabilityInfo(capability)} Scriptlets`,
'browserstack.local': 'true',
'browserstack.debug': 'true',
'browserstack.user': process.env.BROWSERSTACK_USER,
'browserstack.key': process.env.BROWSERSTACK_KEY,
}
// only this selenium version support executeScript method
if (capability.browserName === 'Edge') {
resultCapability['browserstack.selenium_version'] = '4.0.0-beta-1'
}
return resultCapability
}
const runTestsForFile = async (context, filename) => {
console.log(`${getCapabilityInfo(context.capability)} ${filename}`)
const {driver} = context
await driver.get(`http://localhost:${port}/${filename}`)
// wait for testsFinished
await driver.wait(() => {
return driver.executeScript(
'return window && window.jsReporter && window.jsReporter.testsFinished',
)
})
/**
* Use old browsers compatible syntax inside executeAsyncScript
* as it would be executed in the context of the tested browser
*/
const response = await driver.executeScript(() => {
function getReporterResults(reporter) {
return {
moduleEnds: reporter.moduleEnds.map((moduleEnd) => {
return {
name: moduleEnd.name,
status: moduleEnd.status,
runtime: moduleEnd.runtime,
}
}),
testEnds: Object.keys(reporter.testEnds).map((testEndKey) => {
const testEnd = reporter.testEnds[testEndKey]
return {
name: testEnd.name,
status: testEnd.status,
runtime: testEnd.runtime,
}
}),
}
}
return getReporterResults(window.jsReporter)
})
return response
}
const getTestFiles = () => {
const dirPath = path.resolve(__dirname, TESTS_DIST)
const testFiles = fs
.readdirSync(dirPath, {encoding: 'utf8'})
.filter((el) => el.includes(TEST_FILE_NAME_MARKER))
return testFiles
}
const runTestsForFiles = async (context, testFiles) => {
const results = []
for (let i = 0; i < testFiles.length; i += 1) {
const file = testFiles[i]
try {
const result = await runTestsForFile(context, file)
results.push(result)
printResult(context, result)
} catch (e) {
console.log(e)
}
}
return results
}
const runTestByCapability = async (context, rawCapability) => {
const capability = addScriptletsData(rawCapability)
context.capability = capability
const driver = new webdriver.Builder()
.usingServer('http://hub.browserstack.com/wd/hub')
.withCapabilities(capability)
.build()
context.driver = driver
const testFiles = getTestFiles()
const start = Date.now()
const results = await runTestsForFiles(context, testFiles)
const allPassed = printResults(results)
const end = Date.now()
console.log(`Time: ${(end - start) / 1000} sec`)
await driver.quit()
if (!allPassed) {
throw new Error('Not all tests passed')
}
}
const runTests = async (context) => {
let testsFailed = false
for (let i = 0; i < CAPABILITIES.length; i += 1) {
const capability = CAPABILITIES[i]
try {
// eslint-disable-next-line no-await-in-loop
await runTestByCapability(context, capability)
} catch (e) {
console.log(e.message)
testsFailed = true
}
}
if (testsFailed) {
throw new Error('Not all tests passed, or an error occurred')
}
}
const main = async () => {
const localServer = server.init()
await start(localServer, port)
await startBsLocal()
const context = {}
// correctly stop bsLocal and server on sigint
process.on('SIGINT', async () => {
console.log('Caught interrupt signal')
await stopBsLocal()
await stop(localServer)
process.exit(1)
})
try {
await runTests(context)
} catch (e) {
console.log(e.message)
await stopBsLocal()
process.exit(1)
}
await stopBsLocal()
await stop(localServer)
}
main()