DEV Community

Tester bee
Tester bee

Posted on

Getting started with playwright auth feature

I was using the playwright session storage for one test. But the playwright doesnot inject the saved auth for the test. I tried many suggestions of claude and chat gpt but nothing works. Help me with the fix please. i attach the config and the test which uses the auth for your reference.

test('verifying Logout', async ({ page, context }) => {

  // ✅ Manually add the cookie to make sure it's injected correctly
  await context.addCookies([{
    name: 'rack.session',
    value: 'BAh7CkkiD3Nlc3Npb25faWQGOgZFVEkiRTgwODZmMDBjMjlhOWViMzdhMjYw%0ANGVlZWY4Nzk2YThlMDI1Zjg0OTA5MGNlOGFlNzQ3ODI4MzEwYTlmZjM4NTcG%0AOwBGSSIJY3NyZgY7AEZJIiU5NDA4MjgyY2M4NzBkNjM4Y2M2M2EwZWE3Nzgw%0AZTA3MgY7AEZJIg10cmFja2luZwY7AEZ7B0kiFEhUVFBfVVNFUl9BR0VOVAY7%0AAFRJIi1hZGUzNDE0YTNmZDQ4Njc4YmJkOWYwMTE0ODAyNTI4MjY2OTJiYWYz%0ABjsARkkiGUhUVFBfQUNDRVBUX0xBTkdVQUdFBjsAVEkiLTEwODBjOGZkNjQy%0ANzJiZDg1OTZmZjI4MjMwNzVjMDUzOWNjNDNkOGEGOwBGSSIKZmxhc2gGOwBG%0AewBJIg11c2VybmFtZQY7AEZJIg10b21zbWl0aAY7AFQ%3D%0A--071decbd9dd54bf2b90cf3a62edbc609d51ccea0',
    domain: 'the-internet.herokuapp.com',
    path: '/',
    httpOnly: true,
    secure: false,
    sameSite: 'Lax'
  }]);
 await page.waitForTimeout(2000)
  await page.goto('/secure');
  await expect(page).toHaveURL(/.*\/secure/);

  const dashboardPage = new Dashboard(page);
  await dashboardPage.Logout();

  await expect(page).toHaveURL(/.*\/login/);
});

Enter fullscreen mode Exit fullscreen mode

// @ts-check
import { defineConfig, devices } from '@playwright/test';
import dotenv from 'dotenv';

dotenv.config();

export default defineConfig({
testDir: './tests',

fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 1 : undefined,

reporter: 'html',

use: {
baseURL: process.env.BASE_URL,
headless: true,
trace: 'on-first-retry',
},

projects: [
{
name: 'setup',
testMatch: /.*auth.setup.spec.js/,
use: { storageState: undefined },
},
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
storageState: 'auth.json', // ✅ add here
},
dependencies: ['setup'],
},
{
name: 'firefox',
use: {
...devices['Desktop Firefox'],
storageState: 'auth.json', // ✅ add here
},
dependencies: ['setup'],
},
{
name: 'webkit',
use: {
...devices['Desktop Safari'],
storageState: 'auth.json', // ✅ add here
},
dependencies: ['setup'],
},
],
});


Enter fullscreen mode Exit fullscreen mode

Top comments (0)