How to Test Modal Dialogs With a Keyboard and an Agent
A practical keyboard testing charter for modal dialogs, with focus boundaries, Escape behavior, safe agent permissions, and independent state verification.
TL;DR: A dialog that looks right and passes a click-through can still strand a keyboard user. Give an agent a disposable test account and a small, explicit keyboard charter: open the dialog, check focus and tab boundaries, close it with Escape, and verify the intended focus and data state. Treat missing evidence as blocked, not a pass. Follow up with a human accessibility assessment; automation is not an accessibility certification.
Your release test opens the Invite Teammate dialog, fills an address, and clicks Cancel. The dialog disappears. The test passes. But what if keyboard focus never entered the dialog, Shift+Tab reached a control behind it, or Escape did nothing? None of those failures requires the dialog to look broken.
For a QA engineer reviewing an agent-generated test, the question is whether the keyboard contract holds and whether there is enough evidence to investigate a failure.
Start With One Disposable Scenario
Use a staging workspace and a test account with permission to open an Invite Teammate dialog. Prepare a harmless address such as keyboard-test@example.test; do not send an invitation. Confirm that Cancel and Escape are meant to discard the draft. If the actual product offers a confirmation when discarding, write that expected behavior into the test instead of assuming immediate closure.
For this example, the agreed contract is: the opener remains on the page, the dialog has an accessible name, its tabbable controls follow a known sequence, and Escape closes it without sending. Focus returns to the opener and no invitation is created. These are fixture-specific expectations, not assumptions about every dialog.
The W3C modal dialog pattern describes moving focus inside when a modal opens, containing Tab and Shift+Tab within it, and closing with Escape. It also describes exceptions to returning focus to the opener, such as when that element no longer exists or the next step calls for a different logical destination. Agree on the expected destination before running the test.
This kind of failure can also appear in layered controls. In an open Radix issue, a reporter describes Escape sometimes doing nothing after reopening a dropdown inside a modal. That is a report about a particular setup, not evidence that every Radix dialog is affected or that this simpler fixture will reproduce it.
Give the Agent a Keyboard Charter
Ask the agent to run the following sequence once in the controlled workspace and keep the first mismatch. A browser agent may help execute and document the check, but its completion message is not the result.
Establish the start. Record the URL, build, browser, account role, and invitation-list baseline. Find the Invite Teammate opener by its role and accessible name. Open it with the keyboard, not a coordinate click.
Inspect the open state. Check that a dialog with the agreed accessible name appears and that the actual focused element is inside it. Record which element received focus. Do not require the first button by default: W3C notes that the best initial target depends on the content and risk.
Exercise both boundaries. From the known first tabbable control, press Shift+Tab and check that focus wraps to the known last control. From the last, press Tab and check that it wraps to the first. Step through the intervening controls too, watching for focus on background content or an unexpected stop. Use actual key presses rather than moving focus directly to manufacture a pass.
Dismiss and read back. Enter the harmless address without submitting, press Escape, and verify the dialog is no longer visible. Check that focus returns to the specified opener. Independently inspect the invitation list or another authorized durable readback to confirm no invitation was created. A closed dialog alone does not prove that outcome.
Reopen once. Repeat the open-and-dismiss path from the same defined starting state. If the result differs, retain both sequences and report the difference; do not erase the first failure with a successful retry.
The test should also check the dialog's semantics. The W3C pattern describes a dialog role, a name supplied by a label or labelled title, and modal state; it cautions that declaring a dialog modal is not enough if the interface does not actually behave modally. A role lookup is a useful start, not proof of focus containment or visual obscuring.
Define the Agent's Stop Conditions
Permit the agent to read the test workspace and operate the dialog, but not to submit an invitation, change permissions, bypass access controls, or use production data. Set a short run limit and allow one diagnostic rerun only after preserving the first failure. If a prerequisite or durable readback is unavailable, label the result blocked.
Use four outcomes:
Pass: every agreed check and the independent no-invitation readback succeeded.
Fail: an observed behavior contradicts the contract, such as Escape leaving this dialog open.
Blocked: the test could not establish a prerequisite or complete a required check.
Needs human review: evidence is ambiguous, or the expected focus destination and behavior have not been agreed.
For a failure, hand off the build and browser, starting account and route, exact key sequence, expected and observed focused element, dialog state, and the no-invitation readback. Include a screenshot or recording as context, but do not treat a screenshot as proof of keyboard focus or persisted state. Keep test addresses and other sensitive details out of shared artifacts where possible.
Keep the Accessibility Review Human
Playwright's accessibility guidance notes that automated checks can catch some common issues but recommends combining them with manual assessments and inclusive user testing. Its locator guidance recommends user-facing locators such as role and accessible name. Use those tools to make the charter repeatable, then have a person assess keyboard use, focus visibility, and relevant screen-reader behavior on the supported browsers and assistive technologies. A green script does not establish accessibility conformance.
For a concrete bug handoff, Samelogic describes recording browser steps, inspecting affected elements, and sharing the recording with teammates or agents. A team can add the keyboard sequence and expected-versus-observed focus to the issue alongside that recording. Review the captured content before sharing, and verify the fix with the same independent checks; the recording is investigation context, not the accessibility verdict.
Sources
Related workflows
Move from editorial context into the selector, Playwright, and bug-reproduction pages that turn exact UI evidence into action.

