Testing with the keyboard

fill(), pressSequentially() or keyboard.type()?

  • fill() sets the whole value at once. It is fast and fires an input event, but no key events.
  • pressSequentially() presses one key per character on an element. Use it when the app reacts to keys: masks, autocomplete, keystroke counters.
  • page.keyboard.type() does the same into whatever has focus right now.
await input.fill('Playwright');                // Keystrokes: 0
await input.pressSequentially('Playwright');   // Keystrokes: 10

Editing keys work like a user's

await input.press('Home');
await input.press('Shift+ArrowRight');         // select one character
await input.press('ControlOrMeta+A');          // Ctrl on Windows/Linux, Cmd on macOS

Focus and Tab order

Press Tab on the page and assert where focus lands with toBeFocused(). Inside a radio group, Tab enters the group once and the arrow keys move between options.

await page.getByLabel('Full name').focus();
await page.keyboard.press('Tab');
await expect(page.getByLabel('Email')).toBeFocused();
A control that the mouse can click but Tab never reaches is a real accessibility bug. Tab order tests catch it.

Shortcuts belong to the page

A shortcut is not typed into an element, so press it on page.keyboard. Check what has focus first: most apps ignore shortcuts while you type in a field.

await page.keyboard.press('ControlOrMeta+K');
await expect(page.getByRole('dialog', { name: 'Command palette' })).toBeVisible();
page.keyboard does not wait for anything: there is no element to wait for. Right after page.goto(), the app may not be listening yet, and the key press is lost. Assert something on the page first, such as its heading.

Holding a key

keyboard.down() holds a key until keyboard.up(). Anything you do in between happens with the key held, including mouse clicks.

await page.keyboard.down('Shift');
await page.getByRole('button', { name: 'Delete' }).click();
await page.keyboard.up('Shift');

Text editing

Current value: Empty

Last key pressed: None

Keystrokes: 0

Focus and Tab order

Delivery speed
Clear form

Shortcuts

Press / to jump to search, or Ctrl/⌘ + K to open the command palette.

Holding a key

Shift is up. Hold Shift while deleting to skip the trash.

draft.txt