Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run-browser: Replaced PhantomJS with Puppeteer #1

Merged
merged 1 commit into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The simplest way to run testling type tests in the browser

Options:
-p --port <number> The port number to run the server on (default: 3000)
-b --phantom Use the phantom headless browser to run tests and then exit with the correct status code (if tests output TAP)
-b --chromium Use the chromium headless browser to run tests and then exit with the correct status code (if tests output TAP)
-r --report Generate coverage Istanbul report. Repeat for each type of coverage report desired. (default: text only)
-t --timeout Global timeout in milliseconds for tests to finish. (default: Infinity)
-m --mock Include given JS file and use for handling requests to /mock*
Expand Down Expand Up @@ -51,7 +51,7 @@ var server = http.createServer(function (req, res) {
server.listen(3000);
```

For advanced phantomjs usage, just read the source in `./bin/cli.js`
For advanced chromium-browser usage, just read the source in `./bin/cli.js`

## Mock server

Expand Down
24 changes: 14 additions & 10 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var args = parseArgs(process.argv.slice(2));
var filename = args._[0];
var port = Number(args.p || args.port) || 3000;
var help = args.help || args.h || args._.length === 0;
var phantom = args.b || args.phantom || args.phantomjs;
var chromium = args.b || args.chromium || args.chromium-browser;
var report = args.p || args.report || args.istanbul;
var debug = args.d || args.debug;
var timeout = args.t || args.timeout || Infinity;
Expand All @@ -27,7 +27,7 @@ if (help) {
'',
'Options:',
' -p --port <number> The port number to run the server on (default: 3000)',
' -b --phantom Use the phantom headless browser to run tests and then exit with the correct status code (if tests output TAP)',
' -b --chromium Use the chromium headless browser to run tests and then exit with the correct status code (if tests output TAP)',
' -r --report Generate coverage Istanbul report. Repeat for each type of coverage report desired. (default: text only)',
' -t --timeout Global timeout in milliseconds for tests to finish. (default: Infinity)',
' -m --mock Include given JS file and use for handling requests to /mock*',
Expand All @@ -40,22 +40,26 @@ if (help) {
process.exit(process.argv.length === 3 ? 0 : 1);
}

var server = runbrowser(filename, report, phantom, mockserver);
var server = runbrowser(filename, report, chromium, mockserver);
server.listen(port);

if (!phantom) {

if (!chromium) {
console.log('Open a browser and navigate to "http://localhost:' + port + '"');
} else {
var proc = runbrowser.runPhantom('http://localhost:' + port + '/');

proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
(async () => {
const puppeteer = require('puppeteer');
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://localhost:' + port + '/');
page.on('console', msg => console.log(msg.text()));

if (timeout < Infinity) {
setTimeout(function() {
setTimeout(async function() {
console.log(fmt('Timeout of %dms exceeded', timeout));
proc.kill();
await browser.close();
server.close();
}, timeout);
}
})()
}
14 changes: 4 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ var istanbulTransform = require('browserify-istanbul');
var JSONStream = require('jsonstream2');
var istanbul = require('istanbul');

var runPhantom = require('./lib/run-phantom.js')
var html = fs.readFileSync(__dirname + '/lib/test-page.html', 'utf8');

module.exports = createServer;
module.exports.runPhantom = runPhantom;
module.exports.createHandler = createHandler;
module.exports.handles = handles;

function createServer(filename, reports, phantom, mockserver) {
function createServer(filename, reports, chromium, mockserver) {
var mockserverHandler;
if(mockserver){
mockserverHandler = require(path.resolve('./',mockserver));
}
var handler = createHandler(filename, reports, phantom, mockserverHandler);
var handler = createHandler(filename, reports, chromium, mockserverHandler);
return http.createServer(handler);
}

Expand All @@ -46,7 +44,7 @@ function handleError(err, res) {
if (err) console.error(err.stack || err.message || err);
}

function createHandler(filename, reports, phantom, mockserverHandler) {
function createHandler(filename, reports, chromium, mockserverHandler) {

if (typeof reports === 'boolean' && reports) reports = [ 'text' ];
else if (typeof reports === 'string') reports = [ reports ];
Expand All @@ -69,10 +67,6 @@ function createHandler(filename, reports, phantom, mockserverHandler) {
files = files.map(normalizePath);
files.unshift(path.join(__dirname, '/lib/override-log.js'));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there might be a better way to get logs from puppeteer than this, but it's ok to leave it as is for now.


if (phantom) {
files.unshift(path.join(__dirname, '/lib/phantom-function-bind-shim.js'));
}

Comment on lines -72 to -75
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wow, that's great :)

var b = browserify(files, {debug: true});
if (reports) b.transform(instrumentTransform());
return b.bundle(onBrowserifySrc)
Expand Down Expand Up @@ -107,7 +101,7 @@ function createHandler(filename, reports, phantom, mockserverHandler) {
res.statusCode === 200;
res.end('OK');
var passed = results.tap.fail.length === 0;
if (phantom) process.exit(passed ? 0 : 1);
if (chromium) process.exit(passed ? 0 : 1);
}
})
}
Expand Down
2 changes: 0 additions & 2 deletions lib/phantom-function-bind-shim.js

This file was deleted.

12 changes: 0 additions & 12 deletions lib/phantom-script.js

This file was deleted.

16 changes: 0 additions & 16 deletions lib/run-phantom.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"istanbul": "^0.3.2",
"jsonstream2": "^1.1.0",
"minimist": "0.0.8",
"phantomjs": "~1.9.7-1",
"puppeteer": "~2.0.0",
"process": "^0.9.0",
"tap-finished": "0.0.1",
"through2-spy": "^1.2.0",
Expand Down