camera.spec.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { expect, test } from '@playwright/test'
  2. import { settle } from '../../support/hydration'
  3. /**
  4. * Photographing a part at the desk. A phone's "Camera" opens its camera app
  5. * from a file input; a desk browser ignores that and shows the file picker
  6. * (issue #555), so the page takes the photo itself with the computer's camera
  7. * or a plugged-in webcam, and the photo joins the part's gallery exactly as an
  8. * uploaded file would.
  9. *
  10. * Chromium is handed a fake camera and the permission is granted up front, so
  11. * the camera runs as it would for a person who said yes.
  12. */
  13. test.use({
  14. launchOptions: {
  15. args: ['--use-fake-device-for-media-stream', '--use-fake-ui-for-media-stream'],
  16. },
  17. permissions: ['camera'],
  18. })
  19. test('Camera on a desk computer takes the part photo with the webcam, not the file picker', async ({
  20. page,
  21. }) => {
  22. await page.goto('/inventory')
  23. await settle(page)
  24. await expect(async () => {
  25. // The toolbar's button; the empty list has a second one.
  26. await page.getByRole('button', { name: 'Add Part' }).first().click()
  27. await expect(page.getByRole('dialog', { name: 'Add New Part' })).toBeVisible({
  28. timeout: 2_000,
  29. })
  30. }).toPass({ timeout: 30_000 })
  31. const form = page.getByRole('dialog', { name: 'Add New Part' })
  32. // No file picker: the page's own camera.
  33. let picked = false
  34. page.on('filechooser', () => {
  35. picked = true
  36. })
  37. await form.getByRole('button', { name: 'Camera', exact: true }).click()
  38. const camera = page.getByTestId('camera-photo')
  39. await expect(camera).toBeVisible()
  40. expect(picked, 'a desk computer is not sent to the file picker').toBe(false)
  41. const take = camera.getByRole('button', { name: 'Take photo' })
  42. await expect(take).toBeEnabled({ timeout: 15_000 })
  43. await take.click()
  44. // Looked at before it is used.
  45. await expect(camera.getByRole('img', { name: 'The photo you just took' })).toBeVisible()
  46. await camera.getByRole('button', { name: 'Use this photo' }).click()
  47. await expect(camera).toBeHidden()
  48. await expect(page.getByText('Image uploaded')).toBeVisible({ timeout: 30_000 })
  49. await expect(form.getByRole('img', { name: 'Part 1' })).toBeVisible()
  50. })