-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowserStackTestRunner
More file actions
104 lines (86 loc) · 2.7 KB
/
BrowserStackTestRunner
File metadata and controls
104 lines (86 loc) · 2.7 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
/* eslint-disable no-console */
import dotenv from 'dotenv';
import { join } from 'path';
import * as Browsers from './Browsers';
import Browserstack from 'browserstack-local';
import glob from 'glob-promise';
import createTestCafe from 'testcafe';
dotenv.config();
const supportedBrowsers: { [key: string]: string[] } = Browsers;
const createTestCafeInstance = async (
browser: string
): Promise<number> => {
const files = await glob(getTestFiles(testFilesArg));
const tc = await createTestCafe();
const runner = await tc.createRunner();
const failures = await runner
.src(files)
.browsers(browser)
.run({ skipJsErrors: !!process.env.SKIP_JS_ERRORS });
console.log(`Tests failed for ${JSON.stringify(browser)}: ${failures}`);
await tc.close();
return failures;
};
const [, , testTypeArg, testFilesArg] = process.argv;
const getTestFiles = (testFiles: string) => {
if (testFiles === undefined) {
console.log(join(__dirname, '../**/*.testcafe.*'));
return join(__dirname, '../**/*.testcafe.*');
}
return join(__dirname, testFiles);
};
const getBrowserDevices = (testType: string): string[] => {
if (testType.startsWith('browserstack:')) {
return [testType];
}
const browserDevices = supportedBrowsers[testType];
if (!browserDevices) {
throw new Error(`testType: ${testType} does not match Browsers.ts`);
}
return browserDevices;
};
const getBrowserStack = (): Promise<any> =>
new Promise((resolve) => {
const bs = new Browserstack.Local();
bs.start(
{
key: process.env.BROWSERSTACK_ACCESS_KEY,
'local-identifier': process.env.BROWSERSTACK_LOCAL_IDENTIFIER,
'parallel-runs': process.env.BROWSERSTACK_PARALLEL_RUNS,
force: true,
},
() => {
resolve(bs);
console.log(`Started BrowserStackLocal, isRunning: ${bs.isRunning()}`);
}
);
});
const stopBrowserStackLocal = (bs: any): Promise<void> =>
new Promise((resolve) =>
bs.stop(() => {
resolve(bs);
console.log('Stopped BrowserStackLocal');
})
);
(async () => {
const browserstack = await getBrowserStack();
process.on('SIGINT', () => {
console.log('Killing BrowserStackLocal');
process.kill(browserstack.pid); // browserstack.stop() doesn't run quick enough here
});
const browserDevices = getBrowserDevices(testTypeArg.toLowerCase());
console.log(
`Running tests for ${testTypeArg} with browsers ${JSON.stringify(
browserDevices
)}`
);
const actions = browserDevices.map(createTestCafeInstance);
const results = await Promise.all(actions);
let numberOfFailures = 0;
results.forEach(result => numberOfFailures += result);
await stopBrowserStackLocal(browserstack);
if ( numberOfFailures > 0) {
throw new Error('Some tests failed');
}
process.exit(numberOfFailures > 0 ? 1 : 0);
})();