Generate Keyboard Navigation Test Cases with AI
Returns numbered keyboard navigation test cases covering Tab order, focus visibility, focus trap in modals/menus, Escape key behavior, skip links, focus return after modal close, and roving tabindex for composite widgets: with expected behavior per case.
When to use it
- Manual accessibility testing for a new component or page.
- Auditing keyboard accessibility before a release.
- Onboarding QA engineers to keyboard testing fundamentals.
- Validating focus management after a routing or modal refactor.
The prompt
<role>You are an accessibility-focused QA engineer. You know keyboard accessibility is the foundation of accessibility: screen reader testing assumes keyboard works. You test focus, not clicks.</role> <context>Keyboard patterns to verify:- **Tab order**: sequential focus follows visual / reading order- **Focus visibility**: every focused element has a visible indicator- **Focus trap**: modals and menus trap focus while open- **Focus return**: closing a modal returns focus to the trigger- **Skip links**: first focusable item lets users skip nav- **Escape behavior**: Esc dismisses modals, popovers, menus- **Roving tabindex**: composite widgets (tabs, menubars) use single tab stop- **Arrow keys**: within composite widgets, arrows move within- **Enter / Space**: activates buttons, links, custom controls</context> <task>For the page or component below, generate numbered keyboard test cases covering:1. Tab forward through the entire page / component2. Tab order matches visual layout3. Focus visibility on each interactive element4. Modal / dialog focus trap (if applicable)5. Modal close returns focus to trigger6. Skip link (if applicable to page-level test)7. Escape closes overlays8. Roving tabindex for composite widgets (tabs, menus, lists)9. Enter / Space activation10. Shift+Tab reverse navigation</task> <input>Page or component description: {target}Composite widgets present: {widgets}Modals / overlays present: {overlays}</input> <constraints>- Number each case sequentially (1, 2, 3...).- Each case states the key sequence AND the expected focus location after.- Focus visibility is tested separately per element: don't lump it as "all elements are visible".- Composite widgets MUST have roving tabindex tests (Tab gets you in; arrows navigate within).- Include focus return test for every modal-like component.- Skip link test required for page-level audits.</constraints> <output_format>Markdown document with a numbered list. Each case: ID, key sequence, expected focus location, expected behavior. Followed by a 2-3 bullet "Common failures" list specific to this component.</output_format> <approach>Before writing, identify whether this is a page-level test (need skip link) or component-level test (no skip link).</approach>## RoleA11y-focused QA. Keyboard accessibility = foundation. Test focus, not clicks. ## ContextPatterns:- Tab order (sequential, follows visual/reading order)- Focus visibility (every focused element has indicator)- Focus trap (modals trap focus)- Focus return (close modal → focus returns to trigger)- Skip links (first focusable lets skip nav)- Esc dismisses overlays- Roving tabindex (composite widgets = single tab stop)- Arrows navigate within composite widgets- Enter / Space activate ## TaskGenerate numbered keyboard tests covering:1. Tab forward through entire page/component2. Tab order matches visual layout3. Focus visibility on each element4. Modal focus trap (if applicable)5. Modal close → focus return6. Skip link (page-level test only)7. Esc closes overlays8. Roving tabindex for composite widgets9. Enter / Space activation10. Shift+Tab reverse ## Input- Target (page or component): {target}- Composite widgets: {widgets}- Overlays: {overlays} ## Constraints- Sequential numbering.- Each case: key sequence + expected focus location + expected behavior.- Focus visibility per element.- Composite widgets MUST have roving tabindex tests.- Focus return for every modal.- Skip link for page-level. ## Output formatMarkdown numbered list (ID | key sequence | expected focus | behavior). End with 2-3 Common failures bullets. ## ApproachIdentify page-level vs component-level test.Example
Input
Page or component description: Dashboard page with: top nav (logo + 4 nav links + user menu), sidebar nav (10 items), main content (data table with sortable columns + row-action buttons), bottom-right floating "create" button. Page has a "Skip to main content" link first. Composite widgets: Tab list in main content (4 tabs above table); user menu (dropdown). Modals / overlays: Confirmation modal on row delete; user-menu dropdown.
Output
# Keyboard Navigation Test Cases, Dashboard Page
1. **KN-01: Skip link is first focusable**
- Key: Tab from page load
- Expected: Focus moves to "Skip to main content" link; link is visible.
- Behavior: Pressing Enter on the link moves focus to the main content region (`<main>` or [role=main]).
2. **KN-02: Tab order, header zone**
- Key: Tab repeatedly from start
- Expected: Focus moves: Skip link → Logo → Nav link 1 → Nav link 2 → Nav link 3 → Nav link 4 → User menu trigger
- Behavior: Order matches visual left-to-right.
3. **KN-03: Tab order, sidebar zone**
- Key: Continue Tab from header
- Expected: Focus moves to sidebar item 1, then 2, ..., 10 in visual order.
- Behavior: No items skipped; no items reachable only via mouse.
4. **KN-04: Tab order, main content (tab list uses roving tabindex)**
- Key: Continue Tab from sidebar
- Expected: Focus moves to the currently-selected tab in the tab list (single tab stop for the entire tab list).
- Behavior: Tab list participates in page tab order ONCE; arrow keys navigate within (KN-05).
5. **KN-05: Roving tabindex, arrows within tab list**
- Key: With focus on a tab, press Right arrow
- Expected: Focus moves to next tab; tab activates (if automatic activation pattern).
- Behavior: Left arrow moves to previous tab; Home jumps to first; End jumps to last.
6. **KN-06: Tab order, main table**
- Key: Tab from active tab
- Expected: Focus moves to first sortable column header → second → ... → first row's action buttons → next row's action buttons.
- Behavior: Skip non-interactive cells (data cells without buttons).
7. **KN-07: Tab order, floating create button**
- Key: Continue Tab from last row in table
- Expected: Focus moves to floating "Create" button.
- Behavior: Floating button is in the natural tab order (not skipped because it's positioned absolute).
8. **KN-08: Focus visibility, every element**
- Key: Tab from start to end of page
- Expected: At every Tab press, a visible focus indicator appears on the focused element (outline or boxshadow).
- Behavior: No element receives focus without a visible indicator.
9. **KN-09: Reverse navigation**
- Key: Press Shift+Tab from any focused element
- Expected: Focus moves to the previous element in reverse of KN-02 through KN-07.
- Behavior: Symmetric with forward tab.
10. **KN-10: Activation, Enter on link**
- Key: Focus on nav link 1, press Enter
- Expected: Navigation occurs (URL changes).
- Behavior: Link behaves as if clicked.
11. **KN-11: Activation, Space on button**
- Key: Focus on "Create" button, press Space
- Expected: Create modal opens.
- Behavior: Space activates buttons (not links).
12. **KN-12: Activation, Enter on button**
- Key: Focus on "Create" button, press Enter
- Expected: Same as KN-11, modal opens.
- Behavior: Enter and Space both activate buttons.
13. **KN-13: User menu dropdown, opening with Enter**
- Key: Focus on user menu trigger, press Enter
- Expected: Dropdown opens; focus moves to the first menu item.
- Behavior: Focus shifts INTO the menu, not staying on the trigger.
14. **KN-14: User menu, arrow keys within**
- Key: With dropdown open, press Down arrow
- Expected: Focus moves to next menu item; wraps to top after last.
- Behavior: Up arrow moves backward; Home / End jump to first / last.
15. **KN-15: User menu, Esc closes**
- Key: With dropdown open, press Esc
- Expected: Dropdown closes; focus returns to the user menu trigger.
- Behavior: Focus return is critical, without it, user is lost in the DOM.
16. **KN-16: Confirmation modal, focus trap**
- Key: Open confirmation modal (e.g., delete a row); press Tab repeatedly
- Expected: Focus cycles through modal elements (Cancel button → Confirm button → close X → back to Cancel).
- Behavior: Focus does NOT escape the modal to elements behind it.
17. **KN-17: Confirmation modal, Esc dismisses**
- Key: With modal open, press Esc
- Expected: Modal closes WITHOUT executing the action.
- Behavior: Esc = cancel intent (NOT submit).
18. **KN-18: Confirmation modal, focus returns to trigger on close**
- Key: Close modal (Esc or Cancel)
- Expected: Focus returns to the row-action button that opened the modal.
- Behavior: Focus is NEVER lost (returning to the start of the page is NOT acceptable).
19. **KN-19: Tab list, return to where you left off**
- Key: Tab past the tab list, then Shift+Tab back
- Expected: Focus returns to the SAME tab that was focused before (not the first tab).
- Behavior: Tab list state is preserved.
## Common failures
- Floating "Create" button gets skipped in tab order (CSS position: fixed often breaks focus).
- Modal focus trap leaks: Tab past last modal element moves focus to elements behind the modal.
- Focus return on modal close fails; focus drops to start of page or to <body>.
- User menu dropdown opens but focus doesn't move into the menu.
- Focus indicator is removed via `outline: none` without replacement (developers commonly remove for aesthetics).
- Roving tabindex is missing: tabs become 4 separate tab stops instead of 1.Common pitfalls
- Focus visibility tested generically ('all elements visible') instead of per element: fails to catch the one element with `outline: none`.
- Roving tabindex tests omitted for tab lists / menus: they become 4 tab stops instead of 1.
- Focus return tests skipped: modal closes, focus drops to body, user is lost.
- Floating elements (position: fixed) skipped from tab order assertions even though they're commonly broken.
Tips
- Test by unplugging your mouse for 30 minutes. If you can't use the page, your users with disabilities can't either.
- Pair with `screen-reader-scenarios`: keyboard tests verify focus mechanics; screen reader verifies announcements.
- Add Playwright keyboard tests for the most error-prone patterns (focus trap, focus return).
- Verify focus indicators in BOTH light and dark mode: focus styles often break across themes.
FAQ
When should I use roving tabindex?
For composite widgets where multiple items share a single tab stop: tab lists, menubars, listboxes, treegrids. ARIA Authoring Practices Guide (APG) defines these. For simple lists of links, use natural tab order, not roving.
What's the difference between focus-visible and focus?
`focus-visible` applies the focus style only for keyboard focus (not click focus). Use it to avoid focus rings on click while keeping them for keyboard users. Don't use `outline: none` without focus-visible replacement.
How do touch-only devices interact with this?
Touch devices don't fire focus events on tap. Keyboard testing remains essential for accessibility because users may attach external keyboards or use switch devices. Don't assume mobile users don't tab.
What about the Tab key trapping in iframes?
Tab through an iframe normally moves into and out of it. If your modal contains an iframe, the focus trap must handle iframe boundaries. This is one of the trickiest patterns to get right.