cuckcoding.pro
Menu

Test a form by keyboard with Playwright

The reservation button works with the mouse. A visitor who puts the mouse away can type their email, then spend the rest of the afternoon trying to reach the button. Give your coding assistant a reproducible keyboard journey before asking it to improve the form.

Before you start
Node.js 22 or later, npm and a terminal in an empty directory. The first installation needs internet access.
What to verify
Catch a deliberate keyboard regression and make the original test pass again with a one-attribute fix.

Install one browser for the lab

Use an empty directory for the exercise. Install Playwright Test and its Chromium browser there. You will load the HTML straight from disk inside the test, so there is no web server to start, no framework and no other tab competing for attention.

Terminal
npm init -y
npm install --save-dev @playwright/test
npx playwright install chromium

Run the later commands in this directory as well. On Linux, if required system libraries are missing, follow the browser installer’s instructions.

Make a real, small form

Save this file beside package.json. The label names the input, the button submits the form, and a status message changes only after submission. That last detail gives the test an outcome to check. A highlighted button alone cannot tell you the reservation was saved.

form.html
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Reserve</title>
<form>
  <label for="email">Email address</label>
  <input id="email" type="email" required>
  <button>Reserve</button>
</form>
<p role="status"></p>
<script>
  document.querySelector('form').addEventListener('submit', event => {
    event.preventDefault();
    document.querySelector('[role="status"]').textContent = 'Reservation saved';
  });
</script>
</html>

Open the file and try the route by hand. Type an actual email-shaped value so that native validation lets the submission through.

Follow the focus in both directions

Write the test beside the HTML. Tab sends actual keyboard input. The focus assertions inspect the control that received it. Shift+Tab checks the return journey, which easily gets lost when a patch adds a custom control. Finally, Enter should trigger the same visible confirmation a mouse user would see.

chair.spec.mjs
import { test, expect } from '@playwright/test';
import { readFile } from 'node:fs/promises';

test('reserves a chair using only the keyboard', async ({ page }) => {
  const html = await readFile('form.html', 'utf8');
  await page.setContent(html);
  const email = page.getByRole('textbox', { name: 'Email address' });
  const reserve = page.getByRole('button', { name: 'Reserve' });

  await page.keyboard.press('Tab');
  await expect(email).toBeFocused();
  await page.keyboard.type('reader@example.org');
  await page.keyboard.press('Tab');
  await expect(reserve).toBeFocused();
  await page.keyboard.press('Shift+Tab');
  await expect(email).toBeFocused();
  await page.keyboard.press('Tab');
  await page.keyboard.press('Enter');
  await expect(page.getByRole('status')).toHaveText('Reservation saved');
});

Run npx playwright test chair.spec.mjs. You should see one passing test.

Give the test something to catch

Temporarily add tabindex="-1" to the button in form.html. Run the same test again. The forward focus assertion on the button should fail, even though clicking still works. Give the failure and just that change to your assistant. Ask for the smallest repair, then remove the attribute and run the unchanged test again.

Use --headed to watch Chromium if focus moves somewhere unexpected. The green result covers this path in Chromium. A screen reader announcement needs a separate check.

Try this prompt with your agent

Prompt
Review this keyboard-only reservation test. Identify the first step that stops matching a real user journey. Keep Tab, Shift+Tab and Enter. Propose one HTML change and explain which assertion proves it.

A trap worth catching

Calling locator.focus() moves focus directly. A test written that way can pass while the real visitor still cannot reach the control.

Official documentation