-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbrowserstack.runner.js
executable file
·96 lines (80 loc) · 2.69 KB
/
browserstack.runner.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
/*
Copyright 2020 Splunk Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
require('dotenv').config();
const Nightwatch = require('nightwatch');
const browserstack = require('browserstack-local');
const { clearBuildId, generateBuildId } = require('./buildApi');
function generateHex(length) {
return [...Array(length).keys()].map(() => parseInt(Math.random() * 16).toString(16)).join('');
}
let browserstackLocal;
function createTunnel({localIdentifier}) {
return new Promise((resolve, reject) => {
Nightwatch.browserstackLocal = browserstackLocal = new browserstack.Local();
const key = process.env.BROWSERSTACK_KEY;
if (!key) {
throw new Error('You need to provide environment variable: BROWSERSTACK_KEY.');
}
browserstackLocal.start({
key,
verbose: true,
localIdentifier,
}, function(error) {
if (error) {
reject(error);
} else {
resolve({
close: () => new Promise(r => browserstackLocal.stop(r)),
});
}
});
});
}
async function runTests(argv) {
let buildId;
try {
buildId = generateBuildId();
console.log(`Starting build ${buildId}`);
console.log('Starting tunnel.');
const localIdentifier = generateHex(32);
const tunnelHandle = await createTunnel({localIdentifier});
console.log('Tunnel started.');
const runner = new Nightwatch.CliRunner({
...argv,
// note: this can be used to scope down tests, leaving here so I don't need to search for this in the future
// test: path.join(__dirname, '..', 'tests', 'websocket', 'websockets.spec.js')
});
runner.setup({
desiredCapabilities: {
'browserstack.localIdentifier': localIdentifier,
build: generateBuildId(),
},
});
await runner.runTests();
if (runner.testRunner.hasTestFailures) {
console.warn('Tests failed.');
process.exitCode = 1;
} else {
console.log('Tests finished.');
}
await tunnelHandle.close();
} catch(e) {
console.log('There was an error while starting the test runner:\n\n');
process.stderr.write(e.stack + '\n');
process.exit(2);
} finally {
clearBuildId();
console.log(`Finished build ${buildId}`);
}
}
Nightwatch.cli(runTests);