How to run Lighthouse programmatically behind an authentication wall?

Viewed 1935

I would like to run a Lighthouse audit behind an authentication wall. For example, below I tried to run Lighthouse using PageSpeed Insights to a URL that usually resolves to my Gmail account. However, as seen below, the Lighthouse audit ran against the sign in page, not my Gmail.

enter image description here

Therefore, how can I run Lighthouse programmatically behind an authentication wall? I understand that I can log in to a page, navigate to the DevTools -> Audits page, and manually run an audit; however, I would like to figure out a way to do this programmatically (via PageSpeed Insights API call or via DevTools in a programmatic way is best, but via NodeJS is OK too).

2 Answers

You cannot do this 'out of the box' with just lighthouse.

google-lighthouse-puppeteer however allows you to use puppeteer to handle the login first and then run lighthouse.

It is a steep learning curve but once you master puppeteer it opens up lots of automated testing capabilities.

There seems to be a NPM distribution for Lightouse:

https://www.npmjs.com/package/lighthouse

Following the examples you should be able to use it with something like this:

lighthouse --output html --output-path ./report.html
Related