-
Notifications
You must be signed in to change notification settings - Fork 761
/
scan-for-error.js
108 lines (91 loc) · 3.14 KB
/
scan-for-error.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
const puppeteer = require('puppeteer');
const httpServer = require('http-server');
const fs = require('fs-extra');
const p = require('path');
const maxDepth = 25;
var server = httpServer.createServer();
server.listen(8080, 'localhost', () => {});
let examples = [];
const getExamples = (path, depth = 0) => {
const files = fs.readdirSync(p.resolve(path));
if (depth > maxDepth || path.includes('archived')) {
return;
}
if (files.includes('boot.json')){
examples.push({
url: path + '/' + 'boot.json',
path: p.resolve(path, 'boot.json'),
});
} else {
for(let file of files) {
const fileInfo = fs.statSync(p.resolve(path, file));
if (fileInfo.isDirectory() && file[0] !== '_') {
getExamples(path + '/' + file, depth + 1);
} else if (file[0] !== '_' && file[0] !== '.') {
examples.push({
url: path + '/' + file,
path: p.resolve(path, file),
});
}
}
}
}
getExamples('./public/src');
examples = examples
.filter(e => !e.url.includes('rbush'))
.filter(e => e.url.match(/[^\.]+$/)[0].slice(0,2) === 'js')
.map(e => ({ url: e.url = 'http://localhost:8080/' + (e.url.includes('boot.json') ? 'boot.html?src=' : 'view.html?src=') + e.url.slice(9).replace(/\//g, '\\'), path: e.path.toLowerCase() }));
const scanForError = async (page, example) => {
return new Promise(async (resolve, reject) => {
const path = example.path;
const filename = path.match(/[^\/\\]+$/)[0];
fs.mkdirp(path.slice(0, -(filename.length + 1)));
const logError = (e) => {
console.log('- ' + example.url);
console.log(' .' + example.path.slice(__dirname.length));
console.log('');
console.log(' Script', e.toString().split('\n')[0], '\n\n\n');
};
page.on('error', logError);
page.on('pageerror', logError);
await page.goto(example.url);
try {
await page.waitForSelector('canvas', {
timeout: 3000,
});
} catch (e) {
console.log('- ' + example.url);
console.log(' .' + example.path.slice(__dirname.length));
console.log('');
console.log(' No canvas on load:', e.toString().split('\n')[0], '\n\n\n');
page.removeListener('error', logError);
page.removeListener('pageerror', logError);
resolve();
return;
}
await page.waitFor(500);
page.mouse.click(200, 150);
await page.waitFor(500);
const canvas = await page.$('canvas');
if (!canvas) {
console.log('- ' + example.url);
console.log(' .' + example.path.slice(__dirname.length));
console.log('');
console.log(' No canvas after click\n\n\n');
}
page.removeListener('error', logError);
page.removeListener('pageerror', logError);
resolve();
});
};
async function run() {
const browser = await puppeteer.launch({ headless: true });
const [page] = await browser.pages();
await page.setUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1');
for (let example of examples) {
await scanForError(page, example);
}
await browser.close();
server.close();
}
run();