You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/commands/press.mdx
+59-4Lines changed: 59 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,9 +14,7 @@ Trigger native key events in your application to simulate keyboard interactions.
14
14
15
15
A `keydown`, `press`, and `keyup` event will be dispatched directly to the browser window.
16
16
17
-
Unlike `cy.type()`, which is best for typing character keys, `cy.press()` will dispatch real keyboard events rather than simulated ones. This command is especially useful when testing focus management and keyboard navigation patterns which are critical for [accessibility testing](/app/guides/accessibility-testing) and great keyboard UX.
18
-
19
-
Currently, the only key supported is `Tab`.
17
+
Unlike `cy.type()`, which is best for typing multiple character keys, `cy.press()` will dispatch real keyboard events rather than simulated ones. This command is especially useful when testing focus management and keyboard navigation patterns which are critical for [accessibility testing](/app/guides/accessibility-testing) and great keyboard UX.
The key to press. The supported values are available on [`Cypress.Keyboard.Keys`](/api/cypress-api/keyboard-api), and may change from time to time. It's recomended that you reference these values from `Cypress.Keyboard.Keys` rather than passing in a string.
69
+
The key to press. The supported values are available on [`Cypress.Keyboard.Keys`](/api/cypress-api/keyboard-api), and may change from time to time. It's recommended that you reference these values from `Cypress.Keyboard.Keys` rather than passing in a string.
72
70
73
71
### Supported Keys
74
72
75
73
| Reference | Value |
76
74
| --------------------------- | ------- |
75
+
|`Cypress.Keyboard.Keys.DOWN`|`"ArrowDown"`|
76
+
|`Cypress.Keyboard.Keys.LEFT`|`"ArrowLeft"`|
77
+
|`Cypress.Keyboard.Keys.RIGHT`|`"ArrowRight"`|
78
+
|`Cypress.Keyboard.Keys.UP`|`"ArrowUp"`|
79
+
|`Cypress.Keyboard.Keys.END`|`"End"`|
80
+
|`Cypress.Keyboard.Keys.HOME`|`"Home"`|
81
+
|`Cypress.Keyboard.Keys.PAGEDOWN`|`"PageDown"`|
82
+
|`Cypress.Keyboard.Keys.PAGEUP`|`"PageUp"`|
83
+
|`Cypress.Keyboard.Keys.ENTER`|`"Enter"`|
77
84
|`Cypress.Keyboard.Keys.TAB`|`"Tab"`|
85
+
|`Cypress.Keyboard.Keys.BACKSPACE`|`"Backspace"`|
86
+
|`Cypress.Keyboard.Keys.DELETE`|`"Delete"`|
87
+
|`Cypress.Keyboard.Keys.INSERT`|`"Insert"`|
88
+
|`Cypress.Keyboard.Keys.F1`|`"F1"`|
89
+
|`Cypress.Keyboard.Keys.F2`|`"F2"`|
90
+
|`Cypress.Keyboard.Keys.F3`|`"F3"`|
91
+
|`Cypress.Keyboard.Keys.F4`|`"F4"`|
92
+
|`Cypress.Keyboard.Keys.F5`|`"F5"`|
93
+
|`Cypress.Keyboard.Keys.F6`|`"F6"`|
94
+
|`Cypress.Keyboard.Keys.F7`|`"F7"`|
95
+
|`Cypress.Keyboard.Keys.F8`|`"F8"`|
96
+
|`Cypress.Keyboard.Keys.F9`|`"F9"`|
97
+
|`Cypress.Keyboard.Keys.F10`|`"F10"`|
98
+
|`Cypress.Keyboard.Keys.F11`|`"F11"`|
99
+
|`Cypress.Keyboard.Keys.F12`|`"F12"`|
100
+
101
+
:::caution
102
+
<strong>Browser-Specific Functionality:</strong>
103
+
In Firefox, [the F6 key is used to change focus to the next frame](https://support.mozilla.org/en-US/kb/keyboard-shortcuts-perform-firefox-tasks-quickly#w_current-page). Pressing F6 via `cy.press()` will dispatch the initial `keydown` appropriately, but due to the focus change, the `keyup` event will not be dispatched to the iframe containing the application under test.
104
+
105
+
To re-focus the iframe after pressing F6, use [`cy.focus()`](/api/commands/focus).
106
+
:::
107
+
108
+
## Supported Characters
109
+
110
+
Single character keys are supported, like `a`, `b`, `c`, etc. There is no need to reference `Cypress.Keyboard.Keys` record for these keys, just type them normally as a string:
111
+
112
+
```javascript
113
+
cy.get('input').focus()
114
+
cy.press('a')
115
+
```
116
+
117
+
Multi-codepoint UTF-8 characters are also supported and do not need to be entered as individual codepoints. For example, `é` can be either one single codepoint or two separate codepoints when the accent is pressed as a subsequent modifier key. You do not need to press each codepoint separately; just type the entire character as a string.
118
+
119
+
```javascript
120
+
// works
121
+
cy.get('input').focus()
122
+
cy.press('é') // \u00e9 (composed é)
123
+
cy.press('é') // \u0065\u0301 (e + combining acute accent)
124
+
125
+
// also fine, but not necessary
126
+
cy.get('input').focus()
127
+
cy.press('e') // \u0065
128
+
cy.press('́') // \u0301 (combining acute accent)
129
+
```
130
+
131
+
Strings with multiple characters are not supported. If you need to input longer strings into a text input or similar, use [`cy.type()`](/api/commands/type).
78
132
79
133
<Iconname="angle-right" /> **options _(Object)_**
80
134
@@ -125,6 +179,7 @@ If your application prevents the default behavior of the `beforeunload` event, t
0 commit comments