Automated checks for a whole page

Title, meta tags and language

await expect(page).toHaveTitle('Page Audit · QA Playground');
await expect(page.locator('meta[name="description"]')).toHaveAttribute('content', /audit/i);
await expect(page.locator('html')).toHaveAttribute('lang', 'en');

Images without alt text

Every image needs an alt attribute. An empty alt="" is correct for decorative images; a missing one is a bug:

await expect(page.locator('main img:not([alt])')).toHaveCount(1);

Broken images

A broken image is still an element on the page, so toBeVisible() will not catch it. Wait until every image has finished loading, then check its naturalWidth: it is 0 when the file could not be shown.

const images = page.locator('main img');

await expect
  .poll(() => images.evaluateAll((imgs) => imgs.every((img) => (img as HTMLImageElement).complete)))
  .toBe(true);

const broken = await images.evaluateAll((imgs) =>
  (imgs as HTMLImageElement[])
    .filter((img) => img.naturalWidth === 0)
    .map((img) => img.getAttribute('src'))
);
expect(broken).toEqual(['/images/team-photo.png']);

Broken links

Collect every same-site link and request it with the request fixture, without a browser. Use expect.soft() so one broken link does not hide the others: the test continues and reports all failures at the end.

test('links work', async ({ page, request }) => {
  await page.goto('/content/page-audit');

  const hrefs = await page
    .locator('main a[href^="/"]')
    .evaluateAll((links) => links.map((a) => a.getAttribute('href')!));

  for (const href of hrefs) {
    const response = await request.get(href);
    expect.soft(response.status(), href).toBeLessThan(400);
  }
});

Leave external links out of automated checks: other sites are slow, rate-limited and outside your control.

Unsafe new-tab links

Links that open a new tab should have rel="noopener", or the new page can control the old one:

await expect(page.locator('main a[target="_blank"]:not([rel~="noopener"])')).toHaveCount(1);

Skipped heading levels

Headings should go down one level at a time; screen-reader users navigate by them. Read the levels and look for jumps:

const levels = await page
  .getByTestId('release-notes')
  .getByRole('heading')
  .evaluateAll((headings) => headings.map((h) => Number(h.tagName[1])));

const skips = levels.filter((level, i) => i > 0 && level - levels[i - 1] > 1);
expect(skips).toEqual([5]);

Images

QA Playground logoTeam photo

Release notes

Version 2.0

New features

Data grid, aria snapshots and a fake clock.

Version 1.9

Bug fixes

Timers no longer fire after leaving a page.