|
| 1 | +const puppeteer = require('puppeteer'); |
| 2 | +const { exec } = require('child_process'); |
| 3 | + |
| 4 | +const SANDBOXES = ['svelte']; |
| 5 | + |
| 6 | +const cp = exec('yarn start:test'); |
| 7 | +cp.stdout.on('data', data => { |
| 8 | + console.log(data.toString()); |
| 9 | + if (data.toString().includes('Compiled with warnings.')) { |
| 10 | + console.log('CSB: Starting tests'); |
| 11 | + runTests(); |
| 12 | + } |
| 13 | +}); |
| 14 | + |
| 15 | +async function runTests() { |
| 16 | + function pageLoaded(page) { |
| 17 | + return new Promise(async resolve => { |
| 18 | + await page.exposeFunction('__puppeteer__', () => { |
| 19 | + if (resolve) { |
| 20 | + resolve(); |
| 21 | + } |
| 22 | + }); |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + let browser = puppeteer.launch({ |
| 27 | + args: ['--no-sandbox', '--disable-setuid-sandbox'], |
| 28 | + }); |
| 29 | + |
| 30 | + await Promise.all( |
| 31 | + SANDBOXES.map(async sandbox => { |
| 32 | + const id = sandbox.id || sandbox; |
| 33 | + |
| 34 | + console.log('Loading browser'); |
| 35 | + browser = await browser; |
| 36 | + const page = await browser.newPage(); |
| 37 | + console.log('Opened new page'); |
| 38 | + const waitFunction = pageLoaded(page); |
| 39 | + console.log('Page loaded'); |
| 40 | + |
| 41 | + page.on('console', msg => |
| 42 | + msg.args().forEach(async arg => { |
| 43 | + console.log(await arg.jsonValue()); |
| 44 | + }) |
| 45 | + ); |
| 46 | + page.on('requestfailed', err => console.log(err)); |
| 47 | + |
| 48 | + console.log('Going to', 'http://localhost:3002/#' + id); |
| 49 | + await page.goto('http://localhost:3002/#' + id, { |
| 50 | + timeout: 60000, |
| 51 | + }); |
| 52 | + console.log('Went to ' + id); |
| 53 | + |
| 54 | + await waitFunction; |
| 55 | + console.log('Waited'); |
| 56 | + await page.waitFor(sandbox.waitFor || 2000); |
| 57 | + console.log('Another wait'); |
| 58 | + |
| 59 | + const screenshot = await page.screenshot(); |
| 60 | + |
| 61 | + require('fs').writeFileSync( |
| 62 | + require('path').join( |
| 63 | + __dirname, |
| 64 | + `__image_snapshots__`, |
| 65 | + `${id.split('/').join('-')}-snap.png` |
| 66 | + ), |
| 67 | + screenshot |
| 68 | + ); |
| 69 | + |
| 70 | + console.log('Saved screenshot'); |
| 71 | + |
| 72 | + await page.close(); |
| 73 | + }) |
| 74 | + ); |
| 75 | + |
| 76 | + process.kill(0); |
| 77 | +} |
0 commit comments