Skip to content

Commit

Permalink
Initial puppeteer
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoffer Åström committed Nov 2, 2017
1 parent a51f70e commit 003d5b4
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ yargs
.command(require('./node'))
.command(require('./protractor'))
.command(require('./cdp'))
.command(require('./puppeteer'))
.argv;
75 changes: 75 additions & 0 deletions src/puppeteer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* eslint global-require: 0, no-console: 0, import/no-unresolved: 0, import/no-extraneous-dependencies: 0, import/no-dynamic-require: 0, max-len: 0 */
const path = require('path');
const fs = require('fs');
const globby = require('globby');
const findUp = require('find-up');
const options = require('./options');
const Mocha = require('mocha');

process.on('unhandledRejection', (err) => {
console.error(`Promise Rejection:${err}`);
});

function runTests(files, mochaOpts) {
return (resolve) => {
const mocha = new Mocha(mochaOpts);
files.forEach(f => mocha.addFile(f));
mocha.run((failures) => {
resolve(failures);
});
};
}
function run(files, mochaOpts) {
return new Promise(runTests(files, mochaOpts));
}

const puppet = {
command: ['puppeteer [options]', 'puppet'],
desc: 'Run tests with puppeteer',
builder(yargs) {
return yargs
.usage('puppeteer [options]')
.options(options)
.config('config', (configPath) => {
let foundConfigPath = configPath;
if (!fs.existsSync(configPath)) {
foundConfigPath = findUp.sync(options.config.default);
}
let config = {};
try {
config = require(foundConfigPath);
} catch (_) { } //eslint-disable-line
return config;
});
},
handler(argv) {
let puppeteer;
try {
puppeteer = require('puppeteer');
} catch (_) {
console.log('Could not load puppeteer');
const p = `${path.resolve(process.cwd())}/node_modules/puppeteer`;
console.log(`Trying: ${p}`);
try {
puppeteer = require(p);
} catch (__) {
console.log('Puppeteer could not be found by after-work.js! Please verify that it has been added as a devDependencies in your package.json');
process.exit(1);
}
}
const files = globby.sync(argv.glob);
if (!files.length) {
console.log('No test files found for:', argv.glob);
process.exit(1);
}
(async function launchAndRun() {
const browser = await puppeteer.launch(argv.chrome);
global.browser = browser;
global.page = await browser.newPage();
const failures = await run(files, argv.mocha);
process.exit(failures);
}());
},
};

module.exports = puppet;
22 changes: 22 additions & 0 deletions src/puppeteer/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
config: {
description: 'Path to config file',
type: 'string',
default: 'aw.config.js',
},
glob: {
description: 'Glob pattern',
default: ['test/**/*.spec.js'],
type: 'array',
},
'mocha.enableTimeouts': {
description: 'Enable timeouts',
default: false,
type: 'boolean',
},
'chrome.headless': {
description: 'Run chrome headless',
default: false,
type: 'boolean',
},
};

0 comments on commit 003d5b4

Please sign in to comment.