Tabs in a JS scenario

A settings page with three tabs, checked by a JS scenario: a *.check.mjs file whose function loops over the tabs, checks each one, and takes a screenshot of each. JSON steps run one after another; a JS scenario adds loops, variables and measurements.

Run it#

shell
cd examples/js-scenario
npx brokit check --verbose      # --verbose lists every step, as below
terminal
$ brokit check --verbose
brokit check: 1 scenario, Chrome 158.0.8127.36, software drawing
✓ checks/settings  20 steps, 1.4 s, 3 baselines written
    ✓ General: its tab is selected
    ✓ General: only its panel shows
    ✓ screenshot tab-general → brokit-out/checks/settings/tab-general.png (1040×763) [baseline: new]  387 ms
    ✓ Notifications: its tab is selected
    ✓ Notifications: only its panel shows
    ✓ screenshot tab-notifications → brokit-out/checks/settings/tab-notifications.png (1040×655) [baseline: new]  185 ms
    ✓ Privacy: its tab is selected
    ✓ Privacy: only its panel shows
    ✓ screenshot tab-privacy → brokit-out/checks/settings/tab-privacy.png (1040×660) [baseline: new]  170 ms
    ✓ key ArrowRight
    ✓ ArrowRight selects General
    ✓ key ArrowLeft
    ✓ ArrowLeft selects Privacy
    ✓ key ArrowLeft
    ✓ ArrowLeft selects Notifications
    ✓ key Home
    ✓ Home selects General
    ✓ the email digest is still on
    ✓ on a phone, the last tab fits in the tab bar
    ✓ on a phone, the tabs are on one line
1 scenario passed in 1.4 s

The first run writes three baselines in checks/baselines/settings/, one per tab; the runs after it compare against them. In a real project you commit the baselines. They're not committed here, since they depend on the fonts of the machine that made them.

The General tab The Notifications tab: three switches, Mentions on The Privacy tab

The baselines: one screenshot per tab, from the loop.

The page#

index.html has a tab list and three panels; tabs.js shows the panel of the tab you click, and moves between tabs with the arrow keys, Home and End, as the ARIA tabs pattern does.

The check#

The check, checks/settings.check.mjs:

checks/settings.check.mjs
// The settings page's tabs, checked in a loop: each tab, then the arrow keys, then a
// setting that must survive a change of tab, then the tab bar on a phone.

export const url = '/index.html';
export const viewport = { size: '720x560', dpr: 2 };
export const ready = "document.readyState === 'complete'";

const tabs = ['General', 'Notifications', 'Privacy'];
const selected = "document.querySelector('[role=tab][aria-selected=true]').textContent";
const shown = "[...document.querySelectorAll('[role=tabpanel]')].filter((p) => !p.hidden).map((p) => p.id)";

export default async ({ page, expect, step, screenshot }) => {
  for (const tab of tabs) {
    const id = tab.toLowerCase();
    await page.click({ selector: `#tab-${id}` });
    expect.equal(await page.eval(selected), tab, `${tab}: its tab is selected`);
    expect.equal(await page.eval(shown), [`panel-${id}`], `${tab}: only its panel shows`);
    await screenshot(`tab-${id}`, { selector: '.settings', baseline: true });
  }

  // Focus is on the last tab now. ArrowRight goes round to the first, ArrowLeft back.
  for (const [key, tab] of [['ArrowRight', 'General'], ['ArrowLeft', 'Privacy'], ['ArrowLeft', 'Notifications'], ['Home', 'General']]) {
    await step({ key });
    expect.equal(await page.eval(selected), tab, `${key} selects ${tab}`);
  }

  // A switch turned on stays on after a visit to another tab.
  await page.click({ selector: '#tab-notifications' });
  await page.click({ selector: '#digest' });
  await page.click({ selector: '#tab-privacy' });
  await page.click({ selector: '#tab-notifications' });
  expect.ok(await page.eval("document.querySelector('#digest').checked"), 'the email digest is still on');

  // On a phone, the three tabs fit side by side.
  await page.emulate({ size: '360x740', mobile: true });
  const bar = await page.rect('[role=tablist]');
  const last = await page.rect('#tab-privacy');
  expect.atMost(last.x + last.width, bar.x + bar.width, 'on a phone, the last tab fits in the tab bar');
  expect.near(last.y, (await page.rect('#tab-general')).y, 0.5, 'on a phone, the tabs are on one line');
};

Note: A JS scenario is code. brokit check runs it with Node and your permissions, as a test runner runs test files. On a checkout you don't trust, run brokit check --no-js.

Learn more#

Edit this page on GitHub