auth.setup.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { test as setup, expect } from '@playwright/test'
  2. /**
  3. * Signs in once and keeps the session on disk. Every other spec starts already
  4. * authenticated, which saves a login per test and keeps the sign-in flow tested
  5. * in exactly one place.
  6. */
  7. const AUTH_STATE = 'e2e/.auth/owner.json'
  8. const email = process.env.E2E_USER_EMAIL ?? 'demo@torqvoice.com'
  9. const password = process.env.E2E_USER_PASSWORD ?? 'demo-e2e-pass'
  10. setup('sign in as the workshop owner', async ({ page, context }) => {
  11. await page.goto('/auth/sign-in')
  12. await page.locator('#email').fill(email)
  13. await page.locator('#password').fill(password)
  14. await page.getByRole('button', { name: 'Sign In', exact: true }).click()
  15. // Landing anywhere outside /auth means the session cookie was accepted.
  16. await page.waitForURL((url) => !url.pathname.startsWith('/auth'), { timeout: 30_000 })
  17. await expect(page.locator('#password')).toHaveCount(0)
  18. // A workshop that predates a feature is greeted with an announcement card
  19. // on its first page ("Your emails have a new look"). It is a non-modal
  20. // dialog, so a spec looking for "the dialog" finds it as well as its own.
  21. // Closed here through its own button, which the app records for the whole
  22. // workshop, so no spec starts with one on screen. Announcements queue one
  23. // at a time, hence the loop.
  24. const gotIt = page.getByRole('button', { name: 'Got it', exact: true })
  25. for (let shown = 0; shown < 5; shown++) {
  26. const open = await gotIt
  27. .first()
  28. .waitFor({ state: 'visible', timeout: 3_000 })
  29. .then(() => true)
  30. .catch(() => false)
  31. if (!open) break
  32. await gotIt.first().click()
  33. await expect(gotIt).toHaveCount(0, { timeout: 10_000 })
  34. }
  35. // The app reads its language from this cookie before Accept-Language. Set
  36. // here rather than per test so the saved state carries it everywhere.
  37. const { hostname } = new URL(page.url())
  38. await context.addCookies([{ name: 'locale', value: 'en', domain: hostname, path: '/' }])
  39. await context.storageState({ path: AUTH_STATE })
  40. })