My last article was about a WCAG 2.2 audit of OpenMRS O3 3.7.1, an open source electronic health record. axe-core found ten problems, and a manual pass with the keyboard, zoom and screen readers found more. Near the end I wrote that some of the manual findings could become plain Playwright tests, and that I had not written them yet.
Now I have. Five manual findings are encoded as Playwright tests that run against a local OpenMRS 3.7.1:
- M1: focus order on a paginated table;
- M3: an unannounced status message;
- M6: focus hidden behind a toolbar at 200% zoom;
- M7: reflow at 320 pixels;
- M8: page titles.
All of them fail today, on purpose, and the suite is still green. The day OpenMRS fixes one of these bugs, the matching test turns red and tells me so.
Writing them also showed me two places where my own audit report was wrong. I wrote the spec for the suite and reviewed every result; an AI coding agent wrote most of the code against that spec, and a separate review and QA pass checked it before it was merged. The code is in accessibility-audits, next to the audit it comes from.
Why the tests fail on purpose
A regression test for a bug that still exists has two jobs. It must stay out of the way while the bug is known, and it must speak up when the bug changes. If I skip the test, it never speaks up. If I leave it red, people learn to ignore a red suite. Playwright has test.fail() for this: it marks a test as expected to fail, reports it as passing while it fails, and reports "Expected to fail, but passed" when it starts passing.
The catch is that test.fail() on its own accepts any failure. If a selector breaks after an OpenMRS upgrade, the test fails for the wrong reason and still counts as expected. So I call it in the middle of the test, after the preconditions, never at the top:
export function expectFindingToFail(id: FindingId): void { test.fail( !showKnownFailures, `${id} (WCAG ${findings[id].sc}) is still present in OpenMRS O3 3.7.1. If this test passes, the bug is fixed: remove this call.`, ); } Every finding test has the same three parts:
- Preconditions prove the test is looking at the right thing. If one fails, the test is red, as it should be.
-
expectFindingToFail('M1')marks the rest as the known bug. - One assertion reproduces the finding.
Here is the whole M1 test. The vitals table on the patient chart is paginated. In the audit, changing the page with the keyboard dropped focus from the page selector to <body>.
test( 'M1 · 2.4.3 Focus Order (A) · focus stays on the page selector after changing the page', findingDetails('M1'), async ({ page }) => { await page.goto(chartPath(readFixture().patientUuid, 'Patient Summary')); const widget = vitalsWidget(page); await expect(widget).toBeVisible(); const select = pageSelect(widget); // 12 vitals encounters at 5 per page: 3 pages. await expect(select.locator('option')).toHaveCount(3); await expect(select).toHaveValue('1'); await select.focus(); await expect(select).toBeFocused(); // Change the page with the keyboard, as the finding describes. await page.keyboard.press('ArrowDown'); await expect(select).toHaveValue('2'); expectFindingToFail('M1'); await expect .poll(() => focusDescription(page), { timeout: 5_000, message: '[M1] keyboard focus was lost after changing the page. Actual focus', }) .toBe('page select'); }, ); Before the mark, the test proves four things: the widget rendered, there are three pages, the selector had focus, and the page really changed. After the mark there is a single question: where is the focus now? On 3.7.1 the answer is BODY.
Before I wrote any of this, I checked one behavior with a throwaway spike on Playwright 1.63.0, and it changed how I wrote every test. A failed precondition before the mark is red, as I expected. But an assertion that times out after the mark also counts as an expected failure. Only the 120-second test timeout stays red. So if the final assertion waits on an element that no longer exists, a broken selector looks exactly like the known bug. The rule I took from that is that everything the final assertion reads must already be proven by a precondition.
For debugging there is a switch. With SHOW_KNOWN_FAILURES=1 the mark is off, and the eight finding tests fail with their real values:
[M1] keyboard focus was lost after changing the page. Actual focus Expected: "page select" Received: "BODY (not the page select)" [M6] 1 focus stop(s) entirely behind a fixed bar at 200% zoom (viewport 640x400). Actual: #37 <button> "Options" top 352 bottom 400 [M7] main content scrolls sideways on P5 patient registration (<div> overflow-x auto). Actual: scrollWidth 531, clientWidth 320 That switch caught a bug in my own test, too. The first version of one M7 test passed in that mode when it should have failed. The anchor element I measured was an outer container, 320 pixels wide in a 320-pixel viewport. The region that actually scrolls sits inside it. Under the normal run, that test would have reported "Expected to fail, but passed", and I would have believed that OpenMRS had fixed something.
Proving the tests can turn red
A test that fails on purpose is only useful if it can stop failing. So I sabotaged the tests and injected fixes, without changing any test code.
Breaking a precondition turned the test red in M1, M3 and M8, and it was not counted as an expected failure.
Injecting the fix. I made temporary copies of three specs with one extra context.addInitScript that applies the fix the audit recommends:
- M8: a distinct
document.titleper route. - M6:
scroll-padding-bottomset to the height of the fixed toolbar. - M3: a
role="status"region that exists before the user types.
All three reported "Expected to fail, but passed", so the suite will notice when OpenMRS ships those fixes. I did not run this check for M1 or for the M7 assertions.
The rest of the numbers:
-
npm testruns 24 tests (setup, 13 unit tests for the geometry helpers, and 10 end-to-end tests) in about a minute. Eight of them are expected failures. -
--repeat-each=3gave 31 passed and no flaky tests. - The setup creates its own patient, with an active visit and 12 vitals records, through the REST API. A second run finds that patient and creates nothing.
Encoding each finding
M8, page titles, was the easy one. Seven screens, signed out and signed in, each proven rendered before its title is read. All seven are OpenMRS.
M3, status messages. In the patient search, the "N search results" counter is a plain <p>. A screen reader only announces changes inside a live region it already knows about, so a region created together with the results does not count. Before typing, the test marks every live region already on the page:
await page.locator(LIVE).evaluateAll((els) => { els.forEach((el) => el.setAttribute('data-m3-preexisting', '')); }); After typing, it walks up from the counter with closest() and checks for that marker. On 3.7.1 the only live region before typing is an unrelated <tbody aria-live="polite">, and the counter is in none at all.
This test checks the DOM; it does not run a screen reader. It also accepts any live region above the counter: during the fix check, role="status" on <body> was enough to make it pass.
M6, focus not obscured at 200% zoom. Playwright cannot drive browser zoom, so I emulated 200% of a 1280×800 window as a 640×400 viewport with deviceScaleFactor: 2. That is what the page sees at that zoom, and it switches OpenMRS to the tablet layout with a fixed toolbar at the bottom. The test tabs through the vitals card and measures each focused element against the fixed bars. The classification is a pure function, written test-first:
export function obscuredState( target: Rect, viewport: { width: number; height: number }, bars: Rect[], tolerance = 0.5, ): ObscuredState { const visible = intersect(target, { top: 0, right: viewport.width, bottom: viewport.height, left: 0, }); if (!visible) return 'full'; if (bars.some((bar) => contains(bar, visible, tolerance))) return 'full'; if (bars.some((bar) => intersect(bar, visible) !== null)) return 'partial'; return 'none'; } WCAG 2.4.11 at level AA only fails when the focused element is entirely hidden, so only full fails the test. Partly covered stops go into an annotation.
Timing was the hard part. OpenMRS scrolls focus into view with smooth scrolling. When I measured two animation frames after each Tab, the positions changed from run to run. For "Table view", two frames after Tab, it was at top 400 with scrollY 0; once the scroll settled, it was at top 180 with scrollY 220. Now the test waits until scrollY holds still for six frames before it measures anything.
M7, reflow at 320 pixels, is where checking the page as a whole gives the wrong answer. The obvious check is document.documentElement.scrollWidth <= clientWidth. I kept it as a test that passes on purpose, on both screens, because it shows why it is not enough. The clipping happens inside the page, where a page-level check cannot see it.
So M7 has two tests per screen:
- Controls cut off. The first collects every interactive or text element and compares its box with the viewport and with the clipping boxes of its ancestors. On registration, 4 of 10 elements are cut off, all of them header buttons beyond the 320-pixel edge. On the chart, 7 of 19 are cut off: the same header buttons, "Search patient" and "Patient lists" in the bottom toolbar.
- Content scrolling sideways. The second measures the region that holds the main content.
The second one is where my audit was wrong.
What the tests corrected
M7: the form can be reached. My report said the registration form is clipped "with no way to reach it" and cannot be completed at 320 pixels. The last article repeated that: no horizontal scrollbar, so registering a patient is impossible. The test disagrees.
The form sits inside a div with overflow-x: auto. In that region, scrollWidth is 531 against a clientWidth of 320 on registration. On the chart it was 755 in one run and 828 in a later one, against the same 320. The content is there, 211 pixels to the right of the registration form.
During the manual pass I saw no scrollbar and concluded there was no way to scroll. A follow-up check showed why there was no scrollbar: the container has scrollbar-width: none. It scrolls sideways and down (a scrollHeight of 2148 in a 208-pixel window); it just does not show it. Shift+wheel moved it the full 211 pixels. Tabbing through the form scrolled each cut-off field into view, except one "Edit" button that stayed partly past the edge. My observation was right and my conclusion was wrong.
It is still a 1.4.10 failure, just a different one. Reflow allows two-dimensional scrolling only for content that needs a two-dimensional layout, such as data tables, maps and diagrams. A registration form is linear content. Filling it in at 320 pixels means scrolling that one region down and sideways, field by field. So the finding stays, with a different description, and I have corrected it in the report.
M6: a different control. The report names "Table view" and "Add" as the controls that end up behind the toolbar. In the test run, both are fully visible once the smooth scroll settles. The control that ends up completely hidden is an "Options" button on a table row (top 352, bottom 400, under a toolbar that starts at about 335). The row button just before it is partly covered.
I trust neither result blindly here. The manual pass used real browser zoom in Firefox and Chrome. The test emulates 200% zoom in Chromium. Smooth scrolling and the point where the browser decides an element is "in view" can differ between them. The finding stands, because a focused control does end up entirely behind the toolbar. But which control it is depends on how you zoom, and I now say so instead of naming one as fact.
Both corrections came from the same habit: making the test print the measured value instead of just pass or fail. The screenshot from the audit looked conclusive. The measured scrollWidth was 531.
Limits
- Chromium only, English locale, OpenMRS 3.7.1. Another version may need different locators.
- 200% zoom is emulated, not real browser zoom.
- No screen reader runs. M3 checks the DOM conditions for an announcement, not the announcement itself.
- The M7 probe cannot see text cut by
text-overflowinside an element's own box, or content hidden by a transform. - The first start of the OpenMRS Docker stack took about 90 minutes on my machine before the tests could run at all.
The audit found these problems by hand, with a keyboard and a zoomed browser. The tests do not replace that pass. They remember what it found, and they told me where I had written it down wrong.