Rayrun
← Back to videos

Playwright Tutorial: Re-use state & Re-use Authentication

Playwright provides a way to reuse the signed-in state in the tests. That way you can log in only once and then skip the log in step for all of the tests.

In this video, Jared from commit quality demonstrates how to log in once before all tests and use that state using Playwright's storage state capabilities. This is beneficial for test automation since it can significantly reduce the amount of time and flakiness associated with logging in before each test.

To get started, Jared creates a basic test that logs into the demo Blaze website and checks if the log out button is visible. He then duplicates the test, removing the login functionality, and demonstrates that it will fail as it hasn't logged in. However, using Playwright's storage state capabilities, we don't have to log in before each test.

Jared goes on to create a global setup file which is a function that will run once before all tests. Inside this function, he sets up the Playwright browser, context, and page, and then logs in. After logging in, he saves the state of the web page as a JSON file called login_auth.json.

Next, Jared updates the Playwright config to point to the global setup file and to use the stored state from the login_auth.json file. When running the tests, the state from the JSON file is used, and all tests pass without having to log in before each individual test.

Jared also demonstrates how to use the code generator to autogenerate the auth.json file and how to switch between different state files for various use cases or environments.

In conclusion, using Playwright's storage state capabilities allows for more efficient and stable test automation by logging in once and reusing that state across all tests. This can save time and reduce the risk of flakiness in test automation.

Key Takeaways:

  1. Playwright's storage state capabilities allow you to log in once and reuse the state across all tests, reducing time and flakiness.
  2. Create a global setup file to store and manage the authentication state.
  3. Configure the Playwright config to point to the global setup file and use the stored state from the JSON file.
  4. Use the code generator to autogenerate the auth.json file if desired.
  5. Manage different auth states and environments by switching between state files.

Related Videos

#16 - ARIA and how Playwright is making use of it to identify UI controls?
#16 - ARIA and how Playwright is making use of it to identify UI controls?
How to Code a Test Automation on Youtube | Playwright JS
How to Code a Test Automation on Youtube | Playwright JS
Playwright web table concept | Playwright Typescript tutorial - Part 92
Playwright web table concept | Playwright Typescript tutorial - Part 92
Playwright - Set timeout, parallelisation and retries directly in your test files
Playwright - Set timeout, parallelisation and retries directly in your test files
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.