File size: 2,429 Bytes
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Visual regression (@visual): the main screens compared with stored screenshots (Playwright's own toHaveScreenshot).
// Run on their own (`npm run test:visual`): each run starts a fresh server and database, so the screens are the same every
// time. Times, run ids, the version line and the 3D canvases are masked. Update the baselines on purpose with
// `npm run test:visual:update` and review the images in the diff before committing them.
import { expect, test, type Page } from '@playwright/test';
import { E2E_PASSWORD, E2E_USER } from './playwright.config';

const SCREENS = ['/', '/runs', '/assistant', '/admin'];
const WIDTHS = [1280, 375];
const WHEN = /\b\d{1,2} [A-Z][a-z]{2}( \d{4})?,? \d{1,2}:\d{2}\b|\b\d{1,2}:\d{2}\b/;

const masks = (page: Page) => [
  page.locator('canvas'),
  page.locator('[data-testid="run-id"]'),
  page.locator('time'),
  page.getByText(WHEN),
  page.getByText(/^Version /),
];

for (const width of WIDTHS) {
  test(`@visual sign-in at ${width}px`, async ({ browser }) => {
    const ctx = await browser.newContext({ viewport: { width, height: 900 }, colorScheme: 'light', reducedMotion: 'reduce' });
    const page = await ctx.newPage();
    await page.goto('/login');
    await page.getByLabel('Username').waitFor();
    await expect(page).toHaveScreenshot(`login-${width}.png`, { fullPage: true, mask: masks(page), maxDiffPixelRatio: 0.01, animations: 'disabled' });
    await ctx.close();
  });

  test(`@visual main screens at ${width}px`, async ({ browser }) => {
    const ctx = await browser.newContext({ viewport: { width, height: width < 800 ? 900 : 1000 }, colorScheme: 'light', reducedMotion: 'reduce' });
    const page = await ctx.newPage();
    await page.goto('/login');
    await page.getByLabel('Username').fill(E2E_USER);
    await page.getByLabel('Password').fill(E2E_PASSWORD);
    await page.getByRole('button', { name: 'Sign in' }).click();
    await page.waitForURL((u) => !u.pathname.startsWith('/login'));
    for (const route of SCREENS) {
      await page.goto(route);
      await page.waitForLoadState('networkidle').catch(() => undefined);
      const name = `${route === '/' ? 'home' : route.slice(1).replace(/\//g, '-')}-${width}.png`;
      await expect(page).toHaveScreenshot(name, { fullPage: true, mask: masks(page), maxDiffPixelRatio: 0.01, animations: 'disabled' });
    }
    await ctx.close();
  });
}