Rayrun
← Back to Discord Forum

Centralized authentication, but skip for specific tests

anton.johanssonposted in #help-playwright
Open in Discord
anton.johansson
anton.johansson

I'm following this guide to create a centralized authentication to re-use the cookie and avoid excessive logins: https://playwright.dev/docs/auth#reuse-signed-in-state

However, I have a few specific tests where I don't want this cookie. For example, I want to test different corner cases of the authentication flow. How can I do this? Like opting out of the storage state, some how.

Thanks in advance!

PS: I'm using NodeJS.

This thread is trying to answer question "How can I opt out of the storage state for specific tests when using centralized authentication?"

6 replies

You can pass context fixture to the test and create your own page. In this case it will not fetch the storage state from the page fixture

Easiest way that works for me is adding at the top of the login test file/s: test.use({ storageState: { cookies: [], origins: [] } });

But if there are multiple tests in the same file and some of them need to use storage state and some of them don’t - it will not work like that. Right?

Put in separate test.decribe blocks and it will work

How about two projects? One for guest mode and one for authenticated mode.

You could define an env variable AUTH then :

  • On your globalSetup you store login cookies only when AUTH is true with page.context().addCookies
  • Fetch on your test the login storage state with page.context().addCookies
TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.