-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial version of lighthouse-puppeteer
- Loading branch information
0 parents
commit df6e055
Showing
6 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
How to Contribute | ||
================= | ||
|
||
This project welcomes your contribution. There are several ways to help out: | ||
|
||
* Create an [issue](https://github.com/femtopixel/docker-google-lighthouse-puppeteer/issues/) on GitHub, | ||
if you have found a bug or have an idea for a feature | ||
* Write test cases for open bug issues | ||
* Write patches for open bug/feature issues | ||
|
||
There are a few guidelines that we need contributors to follow, so that we have a | ||
chance of keeping on top of things. | ||
|
||
Issues | ||
------ | ||
|
||
* Submit an [issue](https://github.com/femtopixel/docker-google-lighthouse-puppeteer/issues/) | ||
* Make sure it does not already exist. | ||
* Clearly describe the issue including steps to reproduce, when it is a bug. | ||
* Make sure you note the version you use. | ||
|
||
Additional Resources | ||
-------------------- | ||
|
||
* [Existing issues](https://github.com/femtopixel/docker-google-lighthouse-puppeteer/issues/) | ||
* [General GitHub documentation](https://help.github.com/) | ||
* [GitHub pull request documentation](https://help.github.com/send-pull-requests/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM femtopixel/google-lighthouse | ||
|
||
MAINTAINER Jay MOULIN <[email protected]> <https://twitter.com/moulinjay> | ||
|
||
RUN mkdir -p /home/chrome/testcases && cd /home/chrome && yarn add puppeteer lighthouse-batch | ||
COPY entrypoint.sh /usr/bin/entrypoint | ||
COPY lighthouse-puppeteer.js /home/chrome/lighthouse-puppeteer.js | ||
WORKDIR /home/chrome | ||
VOLUME /home/chrome/testcases | ||
|
||
ENTRYPOINT ["entrypoint"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
The MIT License (MIT) | ||
Copyright (c) 2017 FemtoPixel | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
Google Lighthouse + Puppeteer - Docker Image | ||
============================================ | ||
|
||
[![latest release](https://img.shields.io/github/release/femtopixel/docker-google-lighthouse-puppeteer.svg "latest release")](http://github.com/femtopixel/docker-google-lighthouse-puppeteer/releases) | ||
[![Docker Pulls](https://img.shields.io/docker/pulls/femtopixel/google-lighthouse-puppeteer.svg)](https://hub.docker.com/r/femtopixel/google-lighthouse-puppeteer/) | ||
[![Bitcoin donation](https://github.com/jaymoulin/jaymoulin.github.io/raw/master/btc.png "Bitcoin donation")](https://m.freewallet.org/id/374ad82e/btc) | ||
[![Litecoin donation](https://github.com/jaymoulin/jaymoulin.github.io/raw/master/ltc.png "Litecoin donation")](https://m.freewallet.org/id/374ad82e/ltc) | ||
|
||
* [Google Chrome Headless](https://developers.google.com/web/updates/2017/04/headless-chrome) is the Google Chrome browser that can be started without graphical interface to accomplish several tasks (PDF printing, performance, automation...) | ||
* [Google Lighthouse](https://developers.google.com/web/tools/lighthouse/) analyzes web apps and web pages, collecting modern performance metrics and insights on developer best practices. | ||
* [Google Puppeteer](https://github.com/GoogleChrome/puppeteer) is a tool to automate tasks on a Chrome (headless) browser. | ||
|
||
Description | ||
----------- | ||
|
||
The purpose of this image is to produce performance report for several pages in connected mode and in an automated way to be integrated in a CI environment. | ||
|
||
It uses [lighthouse-batch](https://github.com/mikestead/lighthouse-batch) to be able to automate export of multiple | ||
|
||
Usage | ||
----- | ||
|
||
``` | ||
docker run --rm --name perf -it -v /path/to/your/report/folder:/home/chrome/reports -v /path/to/your/testcases/folder:/home/chrome/testcases --cap-add SYS_ADMIN femtopixel/google-lighthouse-puppeteer <name_of_the_test> | ||
``` | ||
|
||
with `<name_of_the_test>` the name of the test in the `testcases` folder without `.js` | ||
|
||
### Example | ||
|
||
``` | ||
docker run --rm --name perf -it -v /path/to/your/report/folder:/home/chrome/reports -v /path/to/your/testcases/folder:/home/chrome/testcases --cap-add SYS_ADMIN femtopixel/google-lighthouse-puppeteer my_test | ||
``` | ||
|
||
|
||
Usage : Improved | ||
---------------- | ||
|
||
Using the ever-awesome [Jessie Frazelle](https://twitter.com/jessfraz) SECCOMP profile for Chrome, we don't have to use the hammer that is SYS_ADMIN: | ||
|
||
``` | ||
wget https://raw.githubusercontent.com/jfrazelle/dotfiles/master/etc/docker/seccomp/chrome.json -O ~/chrome.json | ||
docker run --rm --name perf -it -v /path/to/your/report/folder:/home/chrome/reports -v /path/to/your/testcases/folder:/home/chrome/testcases --security-opt seccomp=$HOME/chrome.json femtopixel/google-lighthouse-puppeteer <name_of_the_test> | ||
``` | ||
|
||
API (NodeJS) | ||
--- | ||
|
||
You should create a testcase file named `whateverYouWant.js`. | ||
|
||
This file must `module.exports` an object which must contain two methods : `connect` and `getUrls`. | ||
|
||
### connect | ||
|
||
This method must return a `Promise` which resolves the browser (first argument received of the method connect). | ||
|
||
The purpose of this method is to connect the user to the browser. | ||
|
||
### getUrls | ||
|
||
This method must return an array of string for the url to be tested. You can put url restricted by connection since the `connect` method will grant you access. | ||
|
||
### implementation | ||
|
||
```js | ||
class whateverYouWant | ||
{ | ||
getUrls() { | ||
return [ | ||
"https://myawsome.site/admin/heavypage", | ||
"https://myawsome.site/admin/lightpage", | ||
]; | ||
} | ||
|
||
connect(browser) { | ||
return new Promise(async (resolve, reject) => { | ||
const page = await browser.newPage(); | ||
await page.goto('https://myawesome.site/admin/authentication', {waitUntil: 'load'}); | ||
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'}); | ||
resolve(browser); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = new whateverYouWant(); | ||
``` | ||
|
||
FAQ | ||
--- | ||
|
||
* Using `lighthouse` in authenticated mode : https://github.com/GoogleChrome/lighthouse/blob/master/docs/readme.md#testing-on-a-site-with-authentication | ||
* Error while writing files | ||
``` | ||
Runtime error encountered: { Error: EACCES: permission denied, open '/home/chrome/reports/myawesome_site_admin_heavypage.report.json' | ||
errno: -13, | ||
code: 'EACCES', | ||
syscall: 'open', | ||
path: '/home/chrome/reports/myawesome_site_admin_heavypage.report.json' } | ||
``` | ||
Make sure your folder has the write right for others (`chmod o+w`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
if [[ -f "testcases/$1.js" ]]; then | ||
node lighthouse-puppeteer.js $1 | ||
else | ||
exec "$@" | ||
fi | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
if (process.argv.length <= 2) { | ||
console.log(`Usage: ${__filename} testcase`); | ||
process.exit(-1); | ||
} | ||
|
||
const fs = require('fs'); | ||
const modulePath = `./testcases/${process.argv[2]}.js`; | ||
|
||
if (!fs.existsSync(modulePath)) { | ||
console.log(`File must exists : ${modulePath}`); | ||
process.exit(-2); | ||
} | ||
|
||
const DEBUG_PORT = 9222; | ||
const puppeteer = require('puppeteer'); | ||
const lightHouse = require('lighthouse-batch'); | ||
const testcase = 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); | ||
} | ||
|
||
var browser; | ||
|
||
puppeteer.launch({args: [`--remote-debugging-port=${DEBUG_PORT}`]}) | ||
.then(testcase.connect) | ||
.then(b => new Promise((resolve, reject) => { | ||
browser = b; | ||
resolve(b); | ||
})) | ||
.then(b => new Promise((resolve, reject) => { | ||
const options = { | ||
verbose: false, | ||
sites: testcase.getUrls(), | ||
html: true, | ||
out: '/home/chrome/reports', | ||
useGlobal: true, | ||
params: `--port ${DEBUG_PORT}`, | ||
}; | ||
lightHouse(options); | ||
resolve(b); | ||
})) | ||
.then(b => b.close()) | ||
.catch(() => browser.close()); |