Skip to content

Commit 3a09086

Browse files
committed
docs: updates cy.press() documentation to include expanded named keys, as well as utf-8 characters
1 parent 85aed76 commit 3a09086

File tree

1 file changed

+59
-4
lines changed

1 file changed

+59
-4
lines changed

docs/api/commands/press.mdx

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ Trigger native key events in your application to simulate keyboard interactions.
1414

1515
A `keydown`, `press`, and `keyup` event will be dispatched directly to the browser window.
1616

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.
2018

2119
:::caution
2220

@@ -68,13 +66,69 @@ cy.press(Cypress.Keyboard.Keys.TAB)
6866

6967
<Icon name="angle-right" /> **key _(String)_**
7068

71-
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.
7270

7371
### Supported Keys
7472

7573
| Reference | Value |
7674
| --------------------------- | ------- |
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"` |
7784
| `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).
78132

79133
<Icon name="angle-right" /> **options _(Object)_**
80134

@@ -125,6 +179,7 @@ If your application prevents the default behavior of the `beforeunload` event, t
125179
| Version | Changes |
126180
| ----------------------------------- | ---------------------------- |
127181
| [14.3.0](/app/references/changelog) | Added the `.press()` command |
182+
| [15.1.0](/app/references/changelog) | Expanded support to include named keys and single+multi-codepoint UTF-8 characters. |
128183

129184
## See also
130185

0 commit comments

Comments
 (0)