From 940f0fdf8e26beedba3855c16d18848e29836896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoffer=20=C3=85str=C3=B6m?= Date: Mon, 30 Oct 2017 15:35:47 +0100 Subject: [PATCH] Enable debugging browser test --- src/cdp/connect.js | 12 +++++++++--- src/cdp/index.js | 23 +++++------------------ src/cdp/instrument.js | 2 +- src/cdp/options.js | 6 ++++++ src/cdp/runner.js | 23 ++++++++++++----------- src/cdp/server.js | 18 ++++++++++++++++++ 6 files changed, 51 insertions(+), 33 deletions(-) create mode 100644 src/cdp/server.js diff --git a/src/cdp/connect.js b/src/cdp/connect.js index 150b22ac..a5bac2f5 100644 --- a/src/cdp/connect.js +++ b/src/cdp/connect.js @@ -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; diff --git a/src/cdp/index.js b/src/cdp/index.js index ba67e0e6..9334dd38 100644 --- a/src/cdp/index.js +++ b/src/cdp/index.js @@ -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}`); }); @@ -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); }()); diff --git a/src/cdp/instrument.js b/src/cdp/instrument.js index fcb868cc..7f0fee53 100644 --- a/src/cdp/instrument.js +++ b/src/cdp/instrument.js @@ -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); diff --git a/src/cdp/options.js b/src/cdp/options.js index a7e3ac26..47ee01a3 100644 --- a/src/cdp/options.js +++ b/src/cdp/options.js @@ -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', }, diff --git a/src/cdp/runner.js b/src/cdp/runner.js index 34a09cc1..9e5dfc7b 100644 --- a/src/cdp/runner.js +++ b/src/cdp/runner.js @@ -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; @@ -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; @@ -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(); } } diff --git a/src/cdp/server.js b/src/cdp/server.js new file mode 100644 index 00000000..30799b91 --- /dev/null +++ b/src/cdp/server.js @@ -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); + } +};