question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Puppeteer auth script not working

See original GitHub issue

Describe the bug

I have a puppeteer script that logs in to my site, so I can run lighthouse against authenticated pages.

When I run this in headed mode, I see Chrome login as expected.

However, when I check the test output, all the screenshots clearly show the login page, so it’s as if lighthouse has ignored the puppeteer script entirely.

To Reproduce Steps to reproduce the behavior:

const puppeteer = require("puppeteer");

const user = process.env.USER;
const password = process.env.PASSWORD;
const siteOrigin = process.env.SITE_ORIGIN;

module.exports = async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(`${siteOrigin}/sign_in`);

  await page.type("input#user_email", user);
  await page.type("input#user_password", password);
  await page.click('input[type="submit"]');

  await page.waitForNavigation();
  await page.close();
};
module.exports = {
  ci: {
    collect: {
      settings: {
        disableStorageReset: true,
      },
      upload: {
        target: "temporary-public-storage",
      },
      isSinglePageApplication: true,
      puppeteerScript: "./src/event.js",
      url: [
        "https://mywebsite.com/account",
      ],
    },
  },
};

Expected behavior I would expect the lighthouse test to test against the logged in page.

Environment (please complete the following information):

  • OS: Mac OS
  • Browser Chrome
  • Version 88.0.4324.96

Additional context It’s worth mentioning I’ve tried this with and without the page.close() step.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
patrickhulcecommented, Jan 28, 2021

Please see the example in the documentation. You need to use the browser argument that is passed to your script. Launching your own browser means it will be using its own profile so all storage is not kept when Lighthouse runs in the browser that was passed to you.

Did you already address this? This was the headline of my comment and the reason the script you provided will never work. The storage reset comment was just an extra aside.

1reaction
citypaulcommented, Jan 28, 2021

So for the benefit of anyone else who stumbles upon this, here’s the finished code that does now work:

const user = process.env.USER;
const password = process.env.PASSWORD;
const siteOrigin = process.env.SITE_ORIGIN;

module.exports = async (browser) => {
  const page = await browser.newPage();
  await page.goto(`${siteOrigin}/sign_in`);

  await page.type("input#user_email", user);
  await page.type("input#user_password", password);
  await page.click('input[type="submit"]');

  await page.waitForNavigation();
  await page.close();
};

As you can see, I just needed to use the provided browser instance, just as @patrickhulce suggested.

Thanks! 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to login with Puppeteer - node.js - Stack Overflow
This error occurred because you are setting email and password by executing a javascript function $eval instead of the type function.
Read more >
site login malfunction under puppeteer that does not occur ...
Run script, Chrome window opens. · Enter https://online.citi.com in address bar, home page opens showing Sign On section. · Enter 'aaa' in User...
Read more >
Using cookies to speed up Puppeteer and Playwright scripts
Reading or modifying cookies opens up useful possibilities. A practical example is skipping authentication when testing features available only after login. We ...
Read more >
Web Scraping with a Headless Browser: A Puppeteer Tutorial
In this article, Toptal Freelance JavaScript Developer Nick Chikovani shows how easy it is to perform web scraping using a headless browser.
Read more >
Puppeteer | webhint documentation
All properties of options are optional. auth : The credentials and elements to authenticate on a website. See next section for further details....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found