Skip to content

Instantly share code, notes, and snippets.

@Friss
Created November 12, 2019 06:54
Show Gist options
  • Save Friss/6c20241725e36f2abc8d8c6d7cb5ad16 to your computer and use it in GitHub Desktop.
Save Friss/6c20241725e36f2abc8d8c6d7cb5ad16 to your computer and use it in GitHub Desktop.
Example way to run lighthouse cli with auth
const fs = require("fs").promises;
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const puppeteer = require("puppeteer");
const PORT = 8041;
(async () => {
// Direct Puppeteer to open Chrome with a specific debugging port.
const cookieString = 'testCookie'
const browser = await puppeteer.launch({
args: [`--remote-debugging-port=${PORT}`],
// Optional, if you want to see the tests in action.
headless: false,
slowMo: 50
});
const page = await browser.newPage();
await page.setCookie({
name: "session",
value: cookieString,
domain: ".example.com",
path: "/",
expires: Date.now() + 1000 * 60 * 60,
httpOnly: true,
secure: true
});
const config = {
ci: {
collect: {
headful: true,
numberOfRuns: 1,
url:
"https://example.com",
settings: {
port: PORT
}
}
}
};
await fs.writeFile("./lighthouserc.json", JSON.stringify(config, null, 2));
const { stdout, stderr } = await exec(
"./node_modules/.bin/lhci autorun --rcFile=./lighthouserc.json"
);
console.log(stdout);
console.log(stderr);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment