-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathheadless-tests.js
More file actions
executable file
·138 lines (115 loc) · 3.61 KB
/
Copy pathheadless-tests.js
File metadata and controls
executable file
·138 lines (115 loc) · 3.61 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
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
#!/usr/bin/env node
const pup = require('puppeteer-core');
const http = require('http');
const fs = require('fs');
const url = require('url');
async function describe(jsHandle) {
try {
return await jsHandle.jsonValue();
} catch (_) {
return jsHandle.toString();
}
}
function delay(t, value) {
return new Promise(resolve => setTimeout(resolve, t || 0, value));
}
async function timeout(promise, t) {
var ret = Promise.race([promise, delay(t || 1000, 'timeout')]);
if (ret == 'timeout')
throw new Error(ret);
return ret;
}
const RED = '\x1b[31m%s\x1b[0m';
const GREEN = '\x1b[32m%s\x1b[0m';
const YELLOW = '\x1b[33m%s\x1b[0m';
function indicator(success) {
return success ? '🟢' : '🔴';
}
async function runTests(browser, base, url, needsTrigger) {
console.log(YELLOW, `🔄 Tests at ${url}`);
let page = await browser.newPage();
page.on('console', async msg => {
if (msg.type() == 'debug')
return;
var args = await Promise.all(msg.args().map(describe));
console.log(msg.type() == 'error' ? RED : '%s',
args.join(' '))
});
page.on('pageerror', e => console.log(e.toString()));
await page.goto(base + url);
await page.setViewport({width: 1080, height: 1024});
var res = new Promise(async resolve => {
var timeout = setTimeout(async () => {
console.log('TIMEOUT WAITING FOR TESTS', url);
await page.close();
resolve(false);
}, 10000);
await page.exposeFunction('headlessRunnerDone', async (success) => {
clearTimeout(timeout);
// wait for all logs to resolve
await new Promise(r => setTimeout(r, 10));
await page.close();
resolve(success);
});
await page.evaluate(() => {
document.querySelector('#tinytest').addEventListener('tt-done', (e) => {
console.log('Event:', e.type, 'Success:', e.detail.success);
setTimeout(() => window.headlessRunnerDone(e.detail.success));
});
});
if (needsTrigger) {
await page.evaluate(() => {
window.dispatchEvent(new CustomEvent('run-tests', {detail: {sync: true}}));
});
}
});
var success = await res;
console.log(success ? GREEN : RED,
`${indicator(success)} ${url} is done, success: ${success}`);
return success;
}
function staticHandler(root) {
return function (req, res) {
var path = url.parse(req.url).pathname;
if (path.endsWith('/')) {
path += 'index.html';
}
fs.readFile(root + '/' + path.slice(1), (err, data) => {
if (err) {
res.writeHead(404, 'Not Found');
res.write('Not Found');
return res.end();
}
res.writeHead(200);
res.write(data);
return res.end();
});
}
}
function iff(path) {
try {
if (fs.statSync(path).isFile()) {
return path
}
} catch (e) {}
}
(async () => {
var path = process.env.CHROMIUM_BIN ||
iff('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome') ||
iff('/Applications/Brave Browser.app/Contents/MacOS/Brave Browser');
console.log('Running tests with', path);
let browser = await pup.launch({
headless: "new",
args: ['--no-sandbox'],
executablePath: path,
});
var root = process.argv[2] || 'public';
var server = await http.createServer(staticHandler(root)).listen(0);
var base = 'http://localhost:' + server.address().port;
var success = await runTests(browser, base, '/test/morph/', false) &&
await runTests(browser, base, '/examples/', true);
console.log(success ? GREEN : RED,
`${indicator(success)} ALL TESTS DONE, SUCCESS: ${success}`);
await browser.close();
process.exit(success ? 0 : 1);
})();