playwright: [BUG] I am trying to implement 'Multiple signed in roles' in my test suite but getting 'Error: ENOENT: no such file or directory, open 'playwright/.auth/teacher.json' error.
Problem Statement:
I have two users in my test cases and want to run e2e test cases without writing login steps in each tests using different user accounts. So I followed ‘https://playwright.dev/docs/auth#multiple-signed-in-roles’ documentation but error mentioned below.
Can anyone please help me with this?
Source code
Setup File:
auth.setup.ts:
import { test as setup} from '@playwright/test';
const teacherFile = 'playwright/.auth/teacher.json';
setup('authenticate as admin', async ({ page }) => {
// Perform authentication steps. Replace these actions with your own.
await page.goto('https://github.com/login');
await page.getByLabel('Username or email address').fill('admin');
await page.getByLabel('Password').fill('password');
await page.getByRole('button', { name: 'Sign in' }).click();
// End of authentication steps.
// End of authentication steps.
await page.context().storageState({ path: teacherFile });
});
const studentFile = 'playwright/.auth/student.json';
setup('authenticate as user', async ({ page }) => {
// Perform authentication steps. Replace these actions with your own.
await page.goto('https://github.com/login');
await page.getByLabel('Username or email address').fill('user');
await page.getByLabel('Password').fill('password');
await page.getByRole('button', { name: 'Sign in' }).click();
// End of authentication steps.
await page.context().storageState({ path: studentFile });
});
Test Case:
test.spec.ts:
import { test } from '@playwright/test';
test.use ({storageState:'playwright/.auth/teacher.json'})
test("expect to login into teacher account", async ({ page }) => {
await page.locator("#setting").click();
await page.locator("#curriculum").click();
});
test.describe(() => {
test.use({ storageState: 'playwright/.auth/student.json' });
test('student test', async ({ page }) => {
await page.locator(".sc-AxheI sc-pCOPB qSFrr").click();
});
});
Error:
Running 2 tests using 2 workers
1) [chromium] › createapi.spec.js:13:5 › student test ============================================
Error: ENOENT: no such file or directory, open 'playwright/.auth/student.json'
2) [chromium] › createapi.spec.js:5:1 › expect to login into teacher account ================
Error: ENOENT: no such file or directory, open 'playwright/.auth/teacher.json'
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 18 (6 by maintainers)
@mxschitt The issue in my repo was with the ‘timeout’, When i added 1 sec delay in ‘setup.auth.ts’ file before storing ‘storageState’ in the dedicated ‘.json’ file, then ‘auth.json’ file was having all the required set of cookies into the same and hence it used all the required cookies in the browser context.
I will implement ‘npx playwright test --trace=on’ to check I am not fully logged in.
Thanks Max for the constant support, quick replies and complete help to get this test done, really appreciate this. Major learning for me.
FYI, Closing this ticket.