|
| 1 | +/** |
| 2 | + * Auth fixtures for Playwright |
| 3 | + * |
| 4 | + * Usage: |
| 5 | + * import { test, expect } from '../auth' |
| 6 | + * test('admin can see users page', async ({ adminPage }) => { ... }) |
| 7 | + * test('customer sees own issues', async ({ customerPage }) => { ... }) |
| 8 | + */ |
| 9 | +import { test as base, type Page, type BrowserContext } from '@playwright/test' |
| 10 | +import path from 'path' |
| 11 | + |
| 12 | +type AuthFixtures = { |
| 13 | + adminPage: Page |
| 14 | + agentPage: Page |
| 15 | + agent2Page: Page |
| 16 | + customerPage: Page |
| 17 | + adminContext: BrowserContext |
| 18 | + agentContext: BrowserContext |
| 19 | + agent2Context: BrowserContext |
| 20 | + customerContext: BrowserContext |
| 21 | +} |
| 22 | + |
| 23 | +function authFile(role: string) { |
| 24 | + return path.join(__dirname, '.auth', `${role}.json`) |
| 25 | +} |
| 26 | + |
| 27 | +export const test = base.extend<AuthFixtures>({ |
| 28 | + adminContext: async ({ browser }, use) => { |
| 29 | + const context = await browser.newContext({ |
| 30 | + storageState: authFile('admin'), |
| 31 | + }) |
| 32 | + await use(context) |
| 33 | + await context.close() |
| 34 | + }, |
| 35 | + adminPage: async ({ adminContext }, use) => { |
| 36 | + const page = await adminContext.newPage() |
| 37 | + await use(page) |
| 38 | + await page.close() |
| 39 | + }, |
| 40 | + |
| 41 | + agentContext: async ({ browser }, use) => { |
| 42 | + const context = await browser.newContext({ |
| 43 | + storageState: authFile('agent'), |
| 44 | + }) |
| 45 | + await use(context) |
| 46 | + await context.close() |
| 47 | + }, |
| 48 | + agentPage: async ({ agentContext }, use) => { |
| 49 | + const page = await agentContext.newPage() |
| 50 | + await use(page) |
| 51 | + await page.close() |
| 52 | + }, |
| 53 | + |
| 54 | + agent2Context: async ({ browser }, use) => { |
| 55 | + const context = await browser.newContext({ |
| 56 | + storageState: authFile('agent2'), |
| 57 | + }) |
| 58 | + await use(context) |
| 59 | + await context.close() |
| 60 | + }, |
| 61 | + agent2Page: async ({ agent2Context }, use) => { |
| 62 | + const page = await agent2Context.newPage() |
| 63 | + await use(page) |
| 64 | + await page.close() |
| 65 | + }, |
| 66 | + |
| 67 | + customerContext: async ({ browser }, use) => { |
| 68 | + const context = await browser.newContext({ |
| 69 | + storageState: authFile('customer'), |
| 70 | + }) |
| 71 | + await use(context) |
| 72 | + await context.close() |
| 73 | + }, |
| 74 | + customerPage: async ({ customerContext }, use) => { |
| 75 | + const page = await customerContext.newPage() |
| 76 | + await use(page) |
| 77 | + await page.close() |
| 78 | + }, |
| 79 | +}) |
| 80 | + |
| 81 | +export { expect } from '@playwright/test' |
0 commit comments