Skip to content

Commit

Permalink
Enable debugging browser test
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoffer Åström committed Oct 30, 2017
1 parent 013b4ee commit 940f0fd
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 33 deletions.
12 changes: 9 additions & 3 deletions src/cdp/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ function getContent(fileName) {
return fs.readFileSync(filePath, 'utf-8');
}

module.exports = async function connect(options) {
module.exports = async function connect(options, files) {
const client = await CDP(options.client);
const { DOM, DOMStorage, Console, Network, Page, Runtime } = client;
const mochaOptions = `window.mochaOptions = ${JSON.stringify(options.mocha || { ui: 'bdd', reporter: 'min', useColors: true })}`;
const mochaOptions = Object.assign({}, { ui: 'bdd', reporter: 'min', useColors: true }, options.mocha);
if (options.debug) {
mochaOptions.timeout = 0;
}
const injectMochaOptions = `window.mochaOptions = ${JSON.stringify(mochaOptions)}`;
const awFiles = `window.awFiles = ${JSON.stringify(files)}`;

await Promise.all([DOM.enable(), DOMStorage.enable(), Network.enable(), Page.enable(), Runtime.enable(), Console.enable()]);
await Page.addScriptToEvaluateOnLoad({ scriptSource: mochaOptions });
await Page.addScriptToEvaluateOnLoad({ scriptSource: injectMediator });
await Page.addScriptToEvaluateOnLoad({ scriptSource: injectMochaOptions });
await Page.addScriptToEvaluateOnLoad({ scriptSource: awFiles });
await Page.addScriptToEvaluateOnLoad({ scriptSource: getContent('browser-shim.js') });

return client;
Expand Down
23 changes: 5 additions & 18 deletions src/cdp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ const fs = require('fs');
const globby = require('globby');
const options = require('./options');
const Runner = require('./runner');
const Koa = require('koa');
const serve = require('koa-static');
const favicon = require('koa-favicon');
const instrument = require('./instrument');
const server = require('./server');
const NYC = require('nyc');

const app = new Koa();

process.on('unhandledRejection', (err) => {
console.error(`Promise Rejection:${err}`);
});
Expand Down Expand Up @@ -48,19 +43,11 @@ const cdp = {
argv.url = cdp.getUrl(argv.url);
const nyc = new NYC(argv.nyc);

if (/^(http(s?)):\/\//.test(argv.url)) {
app.use(favicon(path.resolve(__dirname, '../../aw.png')));
if (argv.coverage) {
app.use(instrument(relativeFiles, nyc));
}
app.use(...argv.http.root.map(root => serve(path.resolve(process.cwd(), root))));
app.listen(argv.http.port);
}
server(argv.url, relativeFiles, argv.coverage, nyc, argv.http);

const runner = new Runner(argv, nyc);
runner.on('exit', (code) => {
process.exitCode = code;
process.exit(code);
});
runner.on('exit', code => process.exit(code));

(async function run() {
await runner.run(relativeFiles);
}());
Expand Down
2 changes: 1 addition & 1 deletion src/cdp/instrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function instrument(files, nyc) {
return async (ctx, next) => {
await next();
const { request, response } = ctx;
const url = request.url.substring(1);
const { url } = request;

if (nyc.exclude.shouldInstrument(url)) {
const filePath = path.relative(process.cwd(), url);
Expand Down
6 changes: 6 additions & 0 deletions src/cdp/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ module.exports = {
default: ['test/**/*.spec.js'],
type: 'array',
},
'chrome.port': {
description: 'Chrome port',
default: 9222,
type: 'number',
},
'chrome.chromeFlags': {
description: 'Chrome flags',
// default: [],
default: ['--headless', '--disable-gpu', '--allow-file-access-from-files'],
type: 'array',
},
Expand Down
23 changes: 12 additions & 11 deletions src/cdp/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ class Runner {
console.error(msg);
this.exit(1);
}
injectFiles(files) {
return this.client.Page.addScriptToEvaluateOnLoad({ scriptSource: `window.awFiles = ${JSON.stringify(files)}` });
}
async extractCoverage() {
const { result: { value } } = await this.client.Runtime.evaluate({ expression: 'window.__coverage__;', returnByValue: true });
return value;
Expand Down Expand Up @@ -82,17 +79,21 @@ class Runner {
this.nyc.report();
}
await this.client.close();
await this.chrome.kill();
if (!this.options.debug) {
await this.chrome.kill();
}
this.mediator.emit('exit', code);
}
launch(options) {
return this.chromeLauncher.launch(options);
}
async setup() {
this.chrome = await this.launch(this.options.chrome);
const { port } = this.chrome;
const options = { client: { port }, mocha: this.options.mocha };
this.client = await connect(options);
async setup(files) {
if (!this.options.debug) {
this.chrome = await this.launch(this.options.chrome);
const { port } = this.chrome;
this.options.client.port = port;
}
this.client = await connect(this.options, files);
if (!this.client) {
this.fail('CDP Client could not connect');
return;
Expand All @@ -115,8 +116,8 @@ class Runner {
await this.client.Page.navigate({ url: this.options.url });
}
async run(files) {
await this.setup();
await this.injectFiles(files);
await this.setup(files);

await this.navigate();
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/cdp/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const path = require('path');
const Koa = require('koa');
const serve = require('koa-static');
const favicon = require('koa-favicon');
const instrument = require('./instrument');

const app = new Koa();

module.exports = function server(url, files, coverage, nyc, options) {
if (/^(http(s?)):\/\//.test(url)) {
app.use(favicon(path.resolve(__dirname, '../../aw.png')));
if (coverage) {
app.use(instrument(files, nyc));
}
app.use(...options.root.map(root => serve(path.resolve(process.cwd(), root))));
app.listen(options.port);
}
};

0 comments on commit 940f0fd

Please sign in to comment.