forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-sandboxes.js
More file actions
77 lines (63 loc) · 1.85 KB
/
generate-sandboxes.js
File metadata and controls
77 lines (63 loc) · 1.85 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
const puppeteer = require('puppeteer');
const { exec } = require('child_process');
const SANDBOXES = ['svelte'];
const cp = exec('yarn start:test');
cp.stdout.on('data', data => {
console.log(data.toString());
if (data.toString().includes('Compiled with warnings.')) {
console.log('CSB: Starting tests');
runTests();
}
});
async function runTests() {
function pageLoaded(page) {
return new Promise(async resolve => {
await page.exposeFunction('__puppeteer__', () => {
if (resolve) {
resolve();
}
});
});
}
let browser = puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
await Promise.all(
SANDBOXES.map(async sandbox => {
const id = sandbox.id || sandbox;
console.log('Loading browser');
browser = await browser;
const page = await browser.newPage();
console.log('Opened new page');
const waitFunction = pageLoaded(page);
console.log('Page loaded');
page.on('console', msg =>
msg.args().forEach(async arg => {
console.log(await arg.jsonValue());
})
);
page.on('requestfailed', err => console.log(err));
console.log('Going to', 'http://localhost:3002/#' + id);
await page.goto('http://localhost:3002/#' + id, {
timeout: 60000,
});
console.log('Went to ' + id);
await waitFunction;
console.log('Waited');
await page.waitFor(sandbox.waitFor || 2000);
console.log('Another wait');
const screenshot = await page.screenshot();
require('fs').writeFileSync(
require('path').join(
__dirname,
`__image_snapshots__`,
`${id.split('/').join('-')}-snap.png`
),
screenshot
);
console.log('Saved screenshot');
await page.close();
})
);
process.kill(0);
}