Skip to content

Commit 3d02a6c

Browse files
committed
Replace Chromeless with Puppeteer
1 parent 8139afd commit 3d02a6c

File tree

3 files changed

+59
-54
lines changed

3 files changed

+59
-54
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
"babel-loader": "^7.1.4",
3030
"babel-polyfill": "^6.22.0",
3131
"babel-preset-env": "^1.6.1",
32-
"chromeless": "^1.5.1",
3332
"copy-webpack-plugin": "^4.5.1",
3433
"docdash": "^0.4.0",
3534
"eslint": "^4.6.1",
3635
"eslint-config-scratch": "^5.0.0",
3736
"gh-pages": "^1.0.0",
3837
"jsdoc": "^3.5.5",
3938
"json": "^9.0.4",
39+
"puppeteer": "^2.0.0",
4040
"scratch-vm": "0.2.0-prerelease.20191227164934",
4141
"tap": "^11.0.0",
4242
"travis-after-all": "^1.4.4",

test/integration/pick-tests.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
/* global vm, render, Promise */
2-
const {Chromeless} = require('chromeless');
2+
const puppeteer = require('puppeteer');
33
const test = require('tap').test;
44
const path = require('path');
5-
const chromeless = new Chromeless();
65

76
const indexHTML = path.resolve(__dirname, 'index.html');
87
const testDir = (...args) => path.resolve(__dirname, 'pick-tests', ...args);
98

10-
const runFile = (file, action, script) =>
9+
const runFile = async (file, action, page, script) => {
1110
// start each test by going to the index.html, and loading the scratch file
12-
chromeless.goto(`file://${indexHTML}`)
13-
.setFileInput('#file', testDir(file))
14-
// the index.html handler for file input will add a #loaded element when it
15-
// finishes.
16-
.wait('#loaded')
17-
.evaluate(`function () {return (${script})(${action});}`)
18-
;
11+
await page.goto(`file://${indexHTML}`);
12+
const fileInput = await page.$('#file');
13+
await fileInput.uploadFile(testDir(file));
14+
// the index.html handler for file input will add a #loaded element when it
15+
// finishes.
16+
await page.waitForSelector('#loaded');
17+
return page.evaluate(`(function () {return (${script})(${action});})()`);
18+
};
1919

2020
// immediately invoked async function to let us wait for each test to finish before starting the next.
2121
(async () => {
22+
const browser = await puppeteer.launch();
23+
const page = await browser.newPage();
2224

2325
const testOperation = async function (name, action, expect) {
2426
await test(name, async t => {
2527

26-
const results = await runFile('test-mouse-touch.sb2', action, boundAction => {
28+
const results = await runFile('test-mouse-touch.sb2', action, page, boundAction => {
2729
vm.greenFlag();
2830
const sendResults = [];
2931

@@ -97,5 +99,5 @@ const runFile = (file, action, script) =>
9799
}
98100

99101
// close the browser window we used
100-
await chromeless.end();
102+
await browser.close();
101103
})();

test/integration/scratch-tests.js

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,54 @@
11
/* global vm, Promise */
2-
const {Chromeless} = require('chromeless');
2+
const puppeteer = require('puppeteer');
33
const test = require('tap').test;
44
const path = require('path');
55
const fs = require('fs');
6-
const chromeless = new Chromeless();
76

87
const indexHTML = path.resolve(__dirname, 'index.html');
98
const testDir = (...args) => path.resolve(__dirname, 'scratch-tests', ...args);
109

11-
const testFile = file => test(file, async t => {
10+
const testFile = (file, page) => test(file, async t => {
1211
// start each test by going to the index.html, and loading the scratch file
13-
const says = await chromeless.goto(`file://${indexHTML}`)
14-
.setFileInput('#file', testDir(file))
15-
// the index.html handler for file input will add a #loaded element when it
16-
// finishes.
17-
.wait('#loaded')
18-
.evaluate(() => {
19-
// This function is run INSIDE the integration chrome browser via some
20-
// injection and .toString() magic. We can return some "simple data"
21-
// back across as a promise, so we will just log all the says that happen
22-
// for parsing after.
23-
24-
// this becomes the `says` in the outer scope
25-
const messages = [];
26-
const TIMEOUT = 5000;
27-
28-
vm.runtime.on('SAY', (_, __, message) => {
29-
messages.push(message);
30-
});
12+
await page.goto(`file://${indexHTML}`);
13+
const fileInput = await page.$('#file');
14+
await fileInput.uploadFile(testDir(file));
15+
// the index.html handler for file input will add a #loaded element when it
16+
// finishes.
17+
await page.waitForSelector('#loaded');
18+
const says = await page.evaluate(() => {
19+
// This function is run INSIDE the integration chrome browser via some
20+
// injection and .toString() magic. We can return some "simple data"
21+
// back across as a promise, so we will just log all the says that happen
22+
// for parsing after.
23+
24+
// this becomes the `says` in the outer scope
25+
const messages = [];
26+
const TIMEOUT = 5000;
27+
28+
vm.runtime.on('SAY', (_, __, message) => {
29+
messages.push(message);
30+
});
3131

32-
vm.greenFlag();
33-
const startTime = Date.now();
34-
35-
return Promise.resolve()
36-
.then(async () => {
37-
// waiting for all threads to complete, then we return
38-
while (vm.runtime.threads.some(thread => vm.runtime.isActiveThread(thread))) {
39-
if ((Date.now() - startTime) >= TIMEOUT) {
40-
// if we push the message after end, the failure from tap is not very useful:
41-
// "not ok test after end() was called"
42-
messages.unshift(`fail Threads still running after ${TIMEOUT}ms`);
43-
break;
44-
}
45-
46-
await new Promise(resolve => setTimeout(resolve, 50));
32+
vm.greenFlag();
33+
const startTime = Date.now();
34+
35+
return Promise.resolve()
36+
.then(async () => {
37+
// waiting for all threads to complete, then we return
38+
while (vm.runtime.threads.some(thread => vm.runtime.isActiveThread(thread))) {
39+
if ((Date.now() - startTime) >= TIMEOUT) {
40+
// if we push the message after end, the failure from tap is not very useful:
41+
// "not ok test after end() was called"
42+
messages.unshift(`fail Threads still running after ${TIMEOUT}ms`);
43+
break;
4744
}
4845

49-
return messages;
50-
});
51-
});
46+
await new Promise(resolve => setTimeout(resolve, 50));
47+
}
48+
49+
return messages;
50+
});
51+
});
5252

5353
// Map string messages to tap reporting methods. This will be used
5454
// with events from scratch's runtime emitted on block instructions.
@@ -103,13 +103,16 @@ const testFile = file => test(file, async t => {
103103

104104
// immediately invoked async function to let us wait for each test to finish before starting the next.
105105
(async () => {
106+
const browser = await puppeteer.launch();
107+
const page = await browser.newPage();
108+
106109
const files = fs.readdirSync(testDir())
107110
.filter(uri => uri.endsWith('.sb2') || uri.endsWith('.sb3'));
108111

109112
for (const file of files) {
110-
await testFile(file);
113+
await testFile(file, page);
111114
}
112115

113116
// close the browser window we used
114-
await chromeless.end();
117+
await browser.close();
115118
})();

0 commit comments

Comments
 (0)