t.pressKey Method

Presses the specified keyboard keys. Does not require a target element and can be chained with other TestController methods.

t.pressKey(keys [, options]) → this | Promise<any>
Parameter Type Description
keys String The sequence of keys and key combinations to be pressed.
options (optional) Object A set of options that provide additional parameters for the action. See Options.

The following table shows how to specify keys of different types, key sequences, and combinations:

Key Type Example
Alphanumeric keys 'a', 'A', '1'
Modifier keys 'shift', 'alt' (⌥ key on macOS), 'ctrl', 'meta' (meta key on Linux and ⌘ key on macOS)
Navigation and action keys 'backspace', 'tab', 'enter'
Key combinations 'shift+a', 'ctrl+d'
Sequential key presses Use spaces to separate keys and key combinations. For example, 'space tab x ctrl+a'

The following navigation and action keys are supported:

  • 'backspace'
  • 'tab'
  • 'enter'
  • 'capslock'
  • 'esc'
  • 'space'
  • 'pageup'
  • 'pagedown'
  • 'end'
  • 'home'
  • 'left'
  • 'right'
  • 'up'
  • 'down'
  • 'ins'
  • 'delete'

Browser Processing Emulation

When a user presses a key or a key combination, the browser dispatches an event that triggers handlers in page code. Most browsers also respond to common keystrokes with their integrated actions (for instance, they select text when you press Cmd/Ctrl+A or switch focus when you press Shift+Tab).

The t.pressKey action only triggers page handlers for most keystrokes. Browser processing is emulated with a limited set of elements:

Shortcut Elements
'ctrl+c', 'ctrl+v' None. Clipboard operations are not emulated.
'ctrl++', 'ctrl+-' None. Zoom is not performed.
'ctrl+a' text field-based inputs, <textarea>, contentEditable
'backspace' text field-based inputs, <textarea>, contentEditable
'delete' text field-based inputs, <textarea>, contentEditable
'left' text field-based inputs, radio button inputs, <textarea>, <select>, contentEditable
'right' text field-based inputs, radio button inputs, <textarea>, <select>, contentEditable
'up' text field-based inputs, radio button inputs, <textarea>, <select>
'down' text field-based inputs, radio button inputs, <textarea>, <select>
'shift+left' text field-based inputs, <textarea>
'shift+right' text field-based inputs, <textarea>
'shift+up' text field-based inputs, <textarea>
'shift+down' text field-based inputs, <textarea>
'home' text field-based inputs, <textarea>
'end' text field-based inputs, <textarea>
'shift+home' text field-based inputs, <textarea>
'shift+end' text field-based inputs, <textarea>
'enter' text field-based inputs, <textarea>, <select>, <a>
'tab' focusable elements
'shift+tab' focusable elements
'esc' <select>

Note

The 'backspace', 'delete', 'left' and 'right' key presses in contentEditable elements are processed only when text is selected.

Text Field-Based Inputs

TestCafe supports selection and navigation with keystrokes in the following input types:

  • email
  • number
  • password
  • search
  • tel
  • text
  • url

Example

The following example shows how to use the t.pressKey action:

import { Selector } from 'testcafe';

const nameInput = Selector('#developer-name');

fixture`TestController.pressKey`
    .page`https://devexpress.github.io/testcafe/example/`;

test('Key Presses', async t => {
    await t
        .typeText(nameInput, 'Peter Parker')
        .pressKey('home right . delete delete delete delete')
        .expect(nameInput.value).eql('P. Parker');
});

Options

Basic action options provide additional parameters for the t.pressKey, t.selectText, t.selectTextAreaContent and t.selectEditableContent actions.

export interface BasicOption {
    speed: number;
}
Parameter Type Description Default
speed Number The speed of action emulation. Number between 1 (the maximum speed) and 0.01 (the minimum speed). Overrides all other speed settings (CLI, Runner, and test code) for the duration of the action. 1
confidential Boolean Replace the action payload with asterisks in TestCafe Dashboard reports. true when the action target is a password type input; false otherwise.

Example

import { Selector } from 'testcafe';

const nameInput = Selector('#developer-name');

fixture`Action speed`
    .page`https://devexpress.github.io/testcafe/example/`;

test('Type name', async t => {
    await t
        .typeText(nameInput, 'Peter')
        .typeText(nameInput, ' Parker', { speed: 0.1 });
});