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#
cd examples/js-scenario
npx brokit check --verbose # --verbose lists every step, as below$ 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 sThe 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 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:
// 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');
};- The named exports are the scenario's settings, the same keys as in a JSON scenario:
url,viewport,ready, and alsotimeout,toleranceandnote. - The default export is the function brokit runs once the page is ready. It gets:
page, with a method for every step (click,tap,key,type,emulate,eval, ...) andrect(selector), which gives an element's{ x, y, width, height };expect, whoseequal,near,matches,includes,atLeast,atMostandokeach record a check with its label, and return whether it held;step(...), which runs a JSON step and records it, as{ key }here;screenshot(name, options), a screenshot step:{ baseline: true }compares it with its baseline.
page.click(...)acts but isn't listed in the report;step({ click: ... })is. Usestepfor actions you want to see in the output.- A failed expectation doesn't stop the function: the loop goes on, so one run shows every tab that's wrong. A failed action (a click on something that isn't there) does stop it, and the report points at the line.
- The last part measures the page: on a 360-pixel phone, the three tabs must fit in the tab bar, on one line.
Note: A JS scenario is code.
brokit checkruns it with Node and your permissions, as a test runner runs test files. On a checkout you don't trust, runbrokit check --no-js.
Learn more#
- Scenarios: JS scenarios, and what their function gets.
- Steps: every step, as a JSON step or a
pagemethod. - Checks and baselines:
brokit check, baselines and--no-js. - Library: the same
pageoutside a check.