Keyboard Actions
Typing, moving focus, shortcuts and held keys: everything a keyboard user does.
fill(), pressSequentially() or keyboard.type()?
fill()sets the whole value at once. It is fast and fires aninputevent, 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: 10Editing 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 macOSFocus 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();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
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.
Related exercises
- Mouse & KeyboardIntermediateMouse ActionsUse the low-level mouse: modifier clicks, positions, drawing, the wheel and a slider.
- Content & AttributesIntermediateARIA WidgetsLocate widgets by their ARIA state, and assert whole structures with aria snapshots.
- FormsIntermediateAutocompleteDrive two comboboxes: one filtered in the browser, one that asks the server.
Read the guide: Playwright practice for beginners: a four-week plan