microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
66.9k stars 3.67k forks source link

[Feature]: browserType.launchPersistentContext: populate context with given storage state #7634

Open badsyntax opened 3 years ago

badsyntax commented 3 years ago

Feature request

Hi there.

It would be really handy to be able to populate a persistent context with auth state, similar to browser.newContext({ storageState: 'state.json' }).

I am generating auth-state (state.json) in my globalSetup, and I'm using a persistent context to allow lighthouse to run on authenticated routes, but I currently have to manually setup the user session by manually "injecting" auth state from the saved "state.json" file. It would be really convenient if I could do this instead:

const context = await chromium.launchPersistentContext(userDataDir, {
  args: [`--remote-debugging-port=${port}`, '--lang=en-GB'],
  storageState: 'state.json'
});
paambaati commented 2 years ago

@badsyntax I've run into this same problem (also see https://github.com/microsoft/playwright/issues/11108#issuecomment-1002423196). Would you mind showing me how you're doing this?

setup the user session by manually "injecting" auth state from the saved "state.json" file

I can set the cookies fine (using context.addCookies({})), but I haven't found a straightforward API to set local storage similarly.

cliffordfajardo commented 2 years ago

@paambaati - you can create a fixture file like this. In this example below I created a fixture that will pre-populate my localstorage with a specific value I've defined. This allows me to get passed SSO authentication in my app

CleanShot 2022-03-24 at 21 27 34@2x

cliffordfajardo commented 2 years ago

@badsyntax - is it possible to use palywright to invoke lighthouse? Or are you using an additional npm package for this?

badsyntax commented 2 years ago

@cliffordfajardo i'm using https://www.npmjs.com/package/playwright-lighthouse and it's working pretty well

pavelfeldman commented 2 years ago

Closing as per above, please feel free to open a new issue if this does not cover your use case.

metodi022 commented 1 year ago

Yes, I agree that it would be very useful to have this feature.

Currently the only way is to manually load the cookies when opening the context. Does anyone know how to also load the localStorage?

rggammon commented 1 year ago

Puppeteer has a recipe for running lighthouse on authenticated pages, but seems to rely on lighthouse taking a Puppeteer page as the 4th argument.

https://github.com/GoogleChrome/lighthouse/blob/main/docs/recipes/auth/README.md

rggammon commented 1 year ago

See also https://github.com/microsoft/playwright/issues/7447

harry-nutsfi commented 2 months ago

I would love to have this feature. Waiting for it!

alionatudan commented 2 months ago

Vote for it

seanwessmith commented 2 months ago

any update on this?