Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to set cookies #58

Closed
Friss opened this issue Nov 11, 2019 · 9 comments
Closed

Ability to set cookies #58

Friss opened this issue Nov 11, 2019 · 9 comments

Comments

@Friss
Copy link

Friss commented Nov 11, 2019

Having the ability to specify / pass cookies would be really helpful for testing pages that are behind auth or interact with authed endpoints.

@patrickhulce
Copy link
Collaborator

Thanks for filing @Friss! This is awaiting work on Lighthouse core (GoogleChrome/lighthouse#9170), once it's available it will be available here too :)

@patrickhulce
Copy link
Collaborator

patrickhulce commented Nov 12, 2019

Also FWIW, if you don't need any cookie merging or other functionality in GoogleChrome/lighthouse#9170, you can get this working today with a lighthouserc.json like...

{
  "ci": {
    "collect": {
      "settings": {
        "extraHeaders": "{\"Cookie\": \"session_id=whatever-session-id\"}"
      }
    }
  }
}

@patrickhulce
Copy link
Collaborator

Also https://github.com/GoogleChrome/lighthouse/blob/master/docs/authenticated-pages.md is a great resource for running Lighthouse on authenticated pages :)

@Friss
Copy link
Author

Friss commented Nov 12, 2019

Thanks for the recommendations @patrickhulce. I was unable to make the extraHeaders way work. I couldn't really debug why since the page would be redirected and the browser would close before I had a chance to look any further. So the upstream PR may help.

I was able to create a puppeteer script that worked for our auth flow (via page.setCookie's). And then I passed a port option and the underlying lighthouse cli was able to connect to it.

Here's a gist of what I got to work for me. (I dynamically create the config file from my testing with extraHeader so that part isn't exactly necessary anymore)
https://gist.github.com/Friss/6c20241725e36f2abc8d8c6d7cb5ad16

Couple things of note:

  1. I had to pass the config as --rcFile instead of --config that the docs mention.
  2. extraHeaders needs to be a JSON string or a file path. extraHeaders code

@patrickhulce
Copy link
Collaborator

patrickhulce commented Nov 12, 2019

Thanks so much for the troubleshooting and extra info @Friss! :)

I had to pass the config as --rcFile instead of --config that the docs mention.

This was a late breaking change in the API, 0.3.0-alpha.0 was rcFile but 0.3.0 (published late today) and newer follow the docs, sorry about that!

extraHeaders needs to be a JSON string or a file path. extraHeaders code

Ah, drat this is indeed a bug with the CLI form of Lighthouse. Will file an issue upstream 👍

@Draichi
Copy link

Draichi commented Jan 23, 2020

Not working for me

I've tried both ways:

"settings": {
        "extraHeaders": "{\"Cookie\": \"PHPSESSID=1234\"}"
      }
"settings": {
        "extraHeaders": {"Cookie": "session_id=whatever-session-id"}
      }

Not working

@patrickhulce
Copy link
Collaborator

patrickhulce commented Jan 23, 2020

Not working

Can you provide more detail on what you mean by "Not working"? Do you get an error message? Does it seem like it's totally ignored? Only the first version of what you posted should have an effect (assuming you're nesting this under ci.collect) and that latter should throw a fatal error until the PR adding support to Lighthouse is published publicly.

@patrickhulce
Copy link
Collaborator

Also worth noting for this thread that puppeteer scripts came out after this issue was filed and are a far superior and flexible method of handling auth at this point.

@patrickhulce
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants