Don’t rewrite your login for every Playwright Test!
AI Summary
This video discusses the concept of reusable login in testing, especially in Playwright. It starts with a simple login test that fills in email and password, clicks login, and asserts successful navigation. The video suggests abstracting login logic into its own module or using a page object model for reusability across multiple tests.
However, it points out the inefficiency of repeating the login sequence in every test since Playwright wipes out session data between tests for isolation, leading to repeated logins.
To optimize, the video explains how to use Playwright’s storage state feature to save the authenticated browser state (cookies, local storage) once after login and reuse it for all tests, avoiding repeated login steps. The storage state is saved to a JSON file (which should be gitignored to avoid sensitive data leaks) and loaded before tests run to simulate a logged-in session.
It further explains the mechanism behind login cookies and how they authenticate sessions persistently in browsers, paralleling normal browser behavior for logged-in users.
The video also suggests using this method for cookie consent settings and feature flags.
Finally, it covers a setup project approach in Playwright to run login and other setup steps once before running test files that depend on the storage state, enforcing efficient test setup and isolation.
The presenter answers some common questions about when fixtures run and using storage states behind VPNs, emphasizing the usefulness of storage states for maintaining consistent test setups across tests.