Imagine stopping at the security desk every single time you walk through the office gate: show your ID, sign the book, wait for a nod. That is how a test suite lives when every test logs in again. This lesson gets a badge made once and hands every test a key badge, and then shows how a single green test can bring the whole suite down. It follows the official Authentication docs, and every command and output comes from a real run.
What you need
- Node.js. 22, 24, 26.
- Playwright 1.65.1. The lesson was recorded with this version; other versions may print slightly different output.
- The code. Subscribers see the lesson repository in the Source code box under the video, with the tags
pw07-startandpw07-end. In the repository this lesson has its own config,playwright.lesson07.config.ts, and you run it with-c. - A test account. The tests use one shared account on automationexercise.com,
pw07-vi@example.comwith the passwordpw07pass. Create it once with the site's API; the repository's lesson text has the exact command. Other people may share the account, and a logged-in session can be ended from outside, so if a run looks wrong, create your own email.
The site sometimes shows a waiting page to automated browsers, so a test can time out; run it again a little later. Times are not the point of this lesson and will differ on your machine.
Step 1: The security desk
Five tests, one per page (/, /products, /view_cart, /contact_us, /test_cases), each checking that Logged in as Vi is on the page. In tests/every-time.spec.ts the file starts with a beforeEach that goes to the login page, fills two inputs and clicks the button:
npx playwright test -c playwright.lesson07.config.ts --project=every-time
Five passed, but every test spends most of its time at the desk. A few lines, harmless looking, running again before every single test.
Step 2: What you believe is half right
Tests must be isolated, and isolated means every test starts blank, so it must log in again. The first half is right: Playwright gives each test a fresh browser context, with no cookies and no history. The second half is wrong. The docs say it plainly: tests can load an existing authenticated state. Still a fresh person at the gate, just with a badge already in the pocket.
Step 3: The badge
Log in once and tell Playwright to save whatever the browser is holding. It writes a JSON file. Is the password inside? No, just two cookies, and the one that matters is called sessionid. That is the code on the badge: the server sees it and knows you are Vi, no password needed.
Step 4: The person who makes the badge
The docs suggest a separate setup file. tests/auth.setup.ts has one test, authenticate, that does three things:
setup('authenticate', async ({ page }) => {
await login(page, user);
await expect(page.getByText('Logged in as Vi')).toBeVisible();
await page.context().storageState({ path: 'playwright/.auth/user.json' });
});
The wait in the middle matters more than you think. Save too early, before the server sends the cookie, and the badge file comes out empty.
Step 5: Hand out the badge
The config has two projects. The setup project runs the authenticate file. The other project declares two lines:
{
name: 'once',
testMatch: /once\.spec\.ts/,
dependencies: ['setup'],
use: { storageState: 'playwright/.auth/user.json' },
},
dependencies means: wait until the badge is ready. storageState points to the badge file. Each test is still a fresh arrival at the gate, but the badge is already in its pocket, and the test file has no beforeEach and not a single login line.
npx playwright test -c playwright.lesson07.config.ts --project=once
Six lines in the log: authenticate first, then the five tests, each much faster than before. The gap is exactly the time spent at the desk. Five trips to the desk become one, and with fifty tests the badge is still made once.
Step 6: Take the badge away
Remove just the storageState line and keep the tests as they are:
npx playwright test -c playwright.lesson07-nostate.config.ts
Fails on purpose. All five tests are red by design.
The first test waits 10 seconds for Logged in as Vi and does not find it. The menu only shows Signup / Login: someone outside the gate with no badge. So it really is the badge that opens the gate for the whole suite.
Step 7: Do not drop the badge
The docs have a red box that says DANGER: the file may contain cookies good enough for someone to impersonate you. So the playwright/.auth folder goes into .gitignore. Committing it is like photocopying your badge and pinning it on the public board.
Step 8: One person calls in sick
Add a test for the Logout button between two old tests, holding the shared badge like every other test:
npx playwright test -c playwright.lesson07-logout.config.ts tests/logout.spec.ts
Fails on purpose.
logout.spec.tsis red by design.
Guess which test goes red. Not the Logout test, it is green. The red one is the test right after it, which never touches Logout, with the same error as when the badge was removed: no Logged in as Vi.
Logout does not only clear a cookie in the browser. It tells the server this account is done, and the server ends every session of that account. Like telling security that employee Vi has left: every badge in Vi's name, whoever holds it, stops working. A different test goes red if you change the run order, which is why this kind of failure is easily called flaky.
| Try | Result |
|---|---|
| Swap Logout for one more login, same account | all green |
Give the Logout test its own fresh login, same account (tests/logout-fixed.spec.ts) | still red |
So the problem is not the badge the test holds, it is the account it uses.
Step 9: A badge of its own
Right under this approach the docs have a "When not to use" section: when tests change server-side state, they must use different accounts. Logout is exactly that. tests/logout-own.spec.ts creates its own account, logs in, clicks Logout, then deletes the account (tests/login-again.spec.ts logs in again after the logout):
npx playwright test -c playwright.lesson07-logout.config.ts tests/logout-own.spec.ts tests/login-again.spec.ts
All green: the badge step plus the tests.
Step 10: One account per worker
So far there was one worker. A big suite runs several workers in parallel, and then a dozen people carry badges in Vi's name: one report that Vi left and the whole building is locked out. The docs have a second approach: one account per parallel worker. tests/worker-fixtures.ts is a worker fixture ({ scope: 'worker' }) that creates an account, logs in, saves its own badge file playwright/.auth/worker-<n>.json, and deletes the account at the end.
npx playwright test -c playwright.lesson07-worker.config.ts --reporter=list,./worker-reporter.js
Two files run on two workers, and worker-reporter.js prints which worker ran which test: each worker gets a different account. The file tests/pw/a-logout.spec.ts keeps the old Logout test on the worker's shared badge, and exactly one test goes red: the one after Logout in the same worker. The other worker stays green because it holds a different code. Per-worker accounts protect workers from each other; they do not save the next test in the same worker. Combine both, a badge per worker and its own account for the Logout test, and everything is green.
Tests that fail on purpose
| File | Fails because |
|---|---|
playwright.lesson07-nostate.config.ts runs once.spec.ts | no storageState, so nobody is logged in |
logout.spec.ts | the logout ends the shared session, so the next test is logged out |
logout-fixed.spec.ts | a second session of the same account is ended as well |
pw/a-logout.spec.ts | logs out the account its worker shares with the next test |
Check your result
npx playwright test -c playwright.lesson07.config.ts --project=once→ 6 passednpx playwright test -c playwright.lesson07-logout.config.ts tests/logout.spec.ts→ 1 failed
Recap
| Approach | Good for | Limit |
|---|---|---|
beforeEach login | one test | pays for the desk every time |
Setup project + storageState | read-only tests | one account for everybody |
| Own account for the Logout test | tests that change server state | creates and deletes an account |
| One account per worker | parallel runs | does not protect the next test in the same worker |
- Tests are still isolated; they just do not stop at the security desk every time.
- One setup project makes the badge, one badge file goes into
.gitignore, two lines go into the config. - A test that logs out ends every session of its account, so give it an account of its own.
- In parallel, give each worker its own account.
Next lesson
Next, we put this whole suite on CI, running on every push.