The docs (https://playwright.dev/docs/auth) mention "acquiring a user" and their example shows actually logging in via github (or your preferred oauth provider) to create the fixture.
The problem is that I have 2FA setup, and in any case, would like to create the fixtures without any login. Especially since I need to test multiple users: one user can't access resources of another user. And creating multiple accounts would be against Github's terms of service.
I have tried modifying the auth file playwright/.auth/user.json
to extend the expiration date indefinitely but haven't been successful.
This thread is trying to answer question "How can I create mock authenticated users without actually authenticating via oauth?"
If you have 2FA setup and need to auth to access the system under test then using the time based one time passwords (TOTP) option is probably the way forward https://www.checklyhq.com/blog/how-to-bypass-totp-based-2fa-login-flows-with-playwright/
Then just resuse your auth state over and over as you would in any other situation.
Follow the example here: https://playwright.dev/docs/test-fixtures#with-fixtures
And extend the test class (from PW Test) , setting the storageState
to what ever it is you need for the test. If doing this per user.
Pretty good example in the docs for this, where storage states are setup in the global setup and you just re-use them in each test:
https://playwright.dev/docs/auth#testing-multiple-roles-with-pom-fixtures
export * from '@playwright/test';
export const test = base.extend<MyFixtures>({
adminPage: async ({ browser }, use) => {
const context = await browser.newContext({ storageState: 'playwright/.auth/admin.json' });
const adminPage = new AdminPage(await context.newPage());
await use(adminPage);
await context.close();
},
userPage: async ({ browser }, use) => {
const context = await browser.newContext({ storageState: 'playwright/.auth/user.json' });
const userPage = new UserPage(await context.newPage());
await use(userPage);
await context.close();
},
});
No if you were testing based on two different roles, you'd need to use two different sets of credentials. If you wanted two separate users, the same would be true.
In our app that uses next-auth, we just mock response from '/api/auth/session' to look like a response of authorized user. We still need to retrieve some tokens for that mock, we do it in a global setup script. Having that, we do not touch 2FA for testing although it is enabled for real users.
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 [email protected].