attachments.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. import { expect, type Page } from '@playwright/test'
  2. /**
  3. * Putting a file on a work order, through the tab that takes it.
  4. *
  5. * Shared because two specs need it for different reasons: one asks what an
  6. * attachment does to the printed invoice, the other needs a file that
  7. * genuinely belongs to one workshop before it can check another cannot fetch
  8. * it. Neither may depend on the other having run.
  9. */
  10. export async function attach(
  11. page: Page,
  12. tab: 'Images' | 'Documents',
  13. file: { name: string; mimeType: string; buffer: Buffer }
  14. ): Promise<void> {
  15. // The tab counts what it holds — "Documents (1)" once there is one — so it
  16. // is found by what it starts with rather than by its whole name.
  17. await expect(async () => {
  18. await page.getByRole('button', { name: new RegExp(`^${tab}`) }).click()
  19. await expect(page.locator('input[type="file"]').first()).toBeAttached({ timeout: 2_000 })
  20. }).toPass({ timeout: 30_000 })
  21. const accept = tab === 'Images' ? '.jpg,.jpeg,.png,.webp' : '.pdf,.csv,.txt'
  22. await page.locator(`input[type="file"][accept="${accept}"]`).setInputFiles(file)
  23. // A document is listed by name; a photograph is a thumbnail that carries
  24. // its name only as the alt text.
  25. const arrived =
  26. tab === 'Images'
  27. ? page.getByRole('img', { name: file.name })
  28. : page.getByText(file.name).first()
  29. await expect(arrived, `${file.name} reached the job`).toBeVisible({ timeout: 30_000 })
  30. }