-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
puppeteerRunner.js
132 lines (114 loc) · 3.45 KB
/
puppeteerRunner.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
const puppeteer = require('../../dev_bundle/lib/node_modules/puppeteer');
let testNumber = 0;
async function runNextUrl(browser) {
const page = await browser.newPage();
page.on('console', async msg => {
// this is a way to make sure the travis does not timeout
// if the test is running for too long without any output to the console (10 minutes)
if (msg._text !== undefined) console.log(msg._text);
else {
testNumber++;
const currentClientTest =
await page.evaluate(() => __Tinytest._getCurrentRunningTestOnClient());
if (currentClientTest !== '') {
console.log(`Currently running on the client test: ${ currentClientTest }`)
return;
}
// If we get here is because we have not yet started the test on the client
const currentServerTest =
await page.evaluate(async () => await __Tinytest._getCurrentRunningTestOnServer());
if (currentServerTest !== '') {
console.log(`Currently running on the server test: ${ currentServerTest }`);
return;
}
// we were not able to find the name of the test, this is a way to make sure the test is still running
console.log(`Test number: ${ testNumber }`);
}
});
if (!process.env.URL) {
process.exit(1);
return;
}
await page.goto(process.env.URL);
async function poll() {
if (await isDone(page)) {
let failCount = await getFailCount(page);
console.log(`Tests complete with ${ failCount } failures`);
console.log(`Tests complete with ${ await getPassCount(page) } passes`);
if (failCount > 0) {
const failed = await getFailed(page);
failed.map((f) => console.log(`${ f.name } failed: ${ f.info }`));
await page.close();
await browser.close();
process.exit(1);
} else {
await page.close();
await browser.close();
process.exit(0);
}
} else {
setTimeout(poll, 1000);
}
}
poll();
}
/**
*
* @param page
* @return {Promise<boolean>}
*/
async function isDone(page) {
return await page.evaluate(function () {
if (typeof TEST_STATUS !== 'undefined') {
return TEST_STATUS.DONE;
}
return typeof DONE !== 'undefined' && DONE;
});
}
/**
*
* @param page
* @return {Promise<number>}
*/
async function getPassCount(page) {
return await page.evaluate(function () {
if (typeof TEST_STATUS !== 'undefined') {
return TEST_STATUS.PASSED;
}
return typeof PASSED !== 'undefined' && PASSED;
});
}
/**
*
* @param page
* @return {Promise<number>}
*/
async function getFailCount(page) {
return await page.evaluate(function () {
if (typeof TEST_STATUS !== 'undefined') {
return TEST_STATUS.FAILURES;
}
return typeof FAILURES !== 'undefined' && FAILURES;
});
}
/**
*
* @param page
* @return {Promise<[{name: string, info: string}]>}
*/
async function getFailed(page) {
return await page.evaluate(function () {
if (typeof TEST_STATUS !== 'undefined') {
return TEST_STATUS.WHERE_FAILED;
}
return typeof WHERE_FAILED !== 'undefined' && WHERE_FAILED;
});
}
async function runTests() {
console.log(`Running test with Puppeteer at ${ process.env.URL }`);
// --no-sandbox and --disable-setuid-sandbox must be disabled for CI compatibility
const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
console.log(`Using version: ${ await browser.version() }`);
runNextUrl(browser);
}
runTests();