t.pressKey Method

Presses the specified keyboard keys. Does not require a target element.

t.pressKey( keys [, options] )
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 Any of the above in a string separated by spaces, for example, 'a ctrl+b'

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>

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 `My fixture`
    .page `http://www.example.com/`;

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.

{
    speed: Number
}
Parameter Type Description Default
speed Number The speed of action emulation. Defines how fast TestCafe performs the action when running tests. A number between 1 (the maximum speed) and 0.01 (the minimum speed). If test speed is also specified in the CLI, API or in test code, the action speed setting overrides test speed. 1

Example

import { Selector } from 'testcafe';

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

fixture `My Fixture`
    .page `http://devexpress.github.io/testcafe/example/`

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