Skip to content

Commit

Permalink
add usage for npm as module and CLI catch
Browse files Browse the repository at this point in the history
  • Loading branch information
jaymoulin committed Nov 14, 2017
1 parent 34a439b commit f7aa50c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 41 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ patch:
docker run --rm -v $$HOME:/root -v `pwd`:/app -ti -w /app node npm version patch
clean:
rm *.tgz
rm -Rf node_modules
install-npm: clean
npm pack
publish-npm: install-npm
Expand Down
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ NodeJS / NPM Package
npm install -g google-lighthouse-puppeteer --unsafe-perm=true
```

### Usage
### CLI Usage

```
lighthouse-puppeteer /path/to/your/test/case.js [options]
Expand All @@ -82,6 +82,33 @@ Default:
}
```

### Package Usage

```javascript
const lp = require('google-lighthouse-puppeteer');

lp.exec('/path/to/my/test.js')
.then(() => console.log('everything ok'))
.catch((err) => console.error(err));
```

#### options

Default:

```
{
debugPort:9222, //port to communicate with chrome-debug (change only if you know what you are doing)
lighthouse: {
params:'', //optional parameters to be passed to lighthouse
useGlobal:true, //should use running chrome-debug or not (yes to use default, no to launch a new one with lighthouse) (change only if you know what you are doing)
out:'/home/chrome/reports', //path to export reports to
html:true, //true to export HTML reports with JSON reports, false for json only
verbose:false, //false to hide debug, true to display some more informations
}
}
```

### API

You should create a testcase file named `whateverYouWant.js`.
Expand Down Expand Up @@ -117,7 +144,7 @@ class whateverYouWant
await page.type('#login', 'admin');
await page.type('#password', 'admin');
await page.$eval('#form input[type=submit]', x => x.click());
await page.waitForNavigation({waitUntil: 'networkidle'});
await page.waitForNavigation({waitUntil: 'networkidle2'});
resolve(browser);
});
}
Expand Down
2 changes: 1 addition & 1 deletion bin/lighthouse-puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ if (typeof(testcase.getUrls) !== 'function') {
}

const lighthousePuppeteer = require('google-lighthouse-puppeteer');
lighthousePuppeteer(testcase, JSON.parse(opts));
lighthousePuppeteer.exec(testcase, JSON.parse(opts)).catch((error) => console.error(error));
81 changes: 44 additions & 37 deletions lighthouse-puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,54 @@ const DEBUG_PORT = 9222;
const puppeteer = require('puppeteer');
const lightHouse = require('lighthouse-batch');
const defaultOptions = {
debugPort:DEBUG_PORT,
debugPort: DEBUG_PORT,
lighthouse: {
params:'',
useGlobal:true,
out:'/home/chrome/reports',
html:true,
verbose:false,
params: '',
useGlobal: true,
out: '/home/chrome/reports',
html: true,
verbose: false,
}
};
var browser;

module.exports = (modulePath, opts={}) => {
const options = Object.assign({}, defaultOptions, opts);
const testcase = typeof (modulePath) === 'object' ? modulePath : require(modulePath);
if (typeof(testcase.connect) !== 'function') {

console.log(`${modulePath}: Module incorrectly formatted. Module should have "connect" method!`);
process.exit(-3);
}
if (typeof(testcase.getUrls) !== 'function') {
console.log(`${modulePath}: Module incorrectly formatted. Module should have "getUrls" method!`);
process.exit(-4);
module.exports = {
exec(modulePath, opts = {}) {
return new Promise((resolveGlobal, reject) => {
const options = Object.assign({}, defaultOptions, opts);
const testcase = typeof (modulePath) === 'object' ? modulePath : require(modulePath);
if (typeof(testcase.connect) !== 'function') {
console.log(`${modulePath}: Module incorrectly formatted. Module should have "connect" method!`);
process.exit(-3);
}
if (typeof(testcase.getUrls) !== 'function') {
console.log(`${modulePath}: Module incorrectly formatted. Module should have "getUrls" method!`);
process.exit(-4);
}
puppeteer.launch({args: [`--remote-debugging-port=${options.debugPort}`]})
.then(testcase.connect)
.then(b => new Promise((resolve) => {
browser = b;
resolve(b);
}))
.then(b => new Promise((resolve) => {
const options = {
verbose: options.lighthouse.verbose,
sites: testcase.getUrls(),
html: options.lighthouse.html,
out: options.lighthouse.out,
useGlobal: options.lighthouse.useGlobal,
params: `--port ${debugPort} ${options.lighthouse.params}`,
};
lightHouse(options);
resolve(b);
}))
.then(b => b.close())
.then(b => resolveGlobal)
.catch((err) => {
browser.close();
reject(err);
});
});
}
puppeteer.launch({args: [`--remote-debugging-port=${options.debugPort}`]})
.then(testcase.connect)
.then(b => new Promise((resolve, reject) => {
browser = b;
resolve(b);
}))
.then(b => new Promise((resolve, reject) => {
const options = {
verbose: options.lighthouse.verbose,
sites: testcase.getUrls(),
html: options.lighthouse.html,
out: options.lighthouse.out,
useGlobal: options.lighthouse.useGlobal,
params: `--port ${debugPort} ${options.lighthouse.params}`,
};
lightHouse(options);
resolve(b);
}))
.then(b => b.close())
.catch(() => browser.close());
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "google-lighthouse-puppeteer",
"version": "0.2.3",
"version": "0.2.4",
"description": "Google Lighthouse Puppeteer is a package to generate reports on multiple urls that allows or not authentication",
"main": "lighthouse-puppeteer.js",
"scripts": {
Expand Down

0 comments on commit f7aa50c

Please sign in to comment.