Rayrun
← Back to Discord Forum

authentication by storing state in JSON fail on logout test

I followed the doc https://playwright.dev/docs/auth for storing 'logged in' state in a JSON file. The next tests work like a charm until I reach the one that tests the logout feature. After the logout test scenario, the following tests no longer start as an already logged user. Why is that? Pls help me understand and fix it. Cheers!

This thread is trying to answer question "Why do subsequent tests no longer start as an already logged user after testing the logout feature when storing 'logged in' state in a JSON file using Playwright?"

3 replies
butchmayhew
butchmayhew

Your logout test more than likely invalidated your cookie or token you originally saved… you could have an after block that re-logs in and saves a new cookie/auth state.

This is a security feature that the application you are testing has implemented properly.

Yep, as @butchmayhew said, the cookie becomes stale. If you need to test logout, do it against a different user than the storage state is used for.

Answer

Understanding Playwright's storageState Feature

When using @playwright/test, you might notice that after running a logout test, subsequent tests no longer start as an already logged user. This is due to how Playwright manages the authentication state, specifically with the storageState feature.

How storageState Works

In Playwright, you can store the authentication state in a JSON file using storageState. You can specify storageState for each test file or group, allowing each test to have its own authentication state.

import { test } from '@playwright/test';

test.use({
  storageState: 'state.json',
});

Why Logout Affects Subsequent Tests

If a logout test modifies or clears the authentication state in the JSON file, subsequent tests that rely on this state will no longer start as an already logged user. They'll be using an outdated or empty authentication state.

Ensuring Consistent Login States

To ensure that subsequent tests start as an already logged user, you need to update the "logged in" state in the JSON file after login actions. Also, handle any modifications or clearing of this state during logout to avoid affecting subsequent tests.

import { test } from '@playwright/test';

test('login', async ({ page }) => {
  await page.goto('https://ray.run/login');
  // perform login actions
  await page.context().storageState({ path: 'state.json' });
});

test('logout', async ({ page }) => {
  await page.goto('https://ray.run/logout');
  // perform logout actions
  // handle storageState modifications
});

By properly managing storageState, you can maintain consistent login states across different scenarios. This is crucial for robust testing with Playwright. For more insights on handling flaky tests in Playwright, check out this blog post.

Related Discord Threads

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.