|
| 1 | +--- |
| 2 | +title: 'cy.press() | Cypress Documentation' |
| 3 | +description: Trigger native key events in your application to simulate keyboard interactions. |
| 4 | +sidebar_label: press |
| 5 | +slug: /api/commands/press |
| 6 | +componentSpecific: false |
| 7 | +--- |
| 8 | + |
| 9 | +<ProductHeading product="app" /> |
| 10 | + |
| 11 | +# press |
| 12 | + |
| 13 | +Trigger native key events in your application to simulate keyboard interactions. |
| 14 | + |
| 15 | +A `keydown`, `press`, and `keyup` event will be dispatched directly to the browser window. |
| 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`. |
| 20 | + |
| 21 | +:::caution |
| 22 | + |
| 23 | +<strong>Supported Browsers:</strong> |
| 24 | +This command is supported in chromium browsers and Firefox versions >= v135. WebKit |
| 25 | +is not supported. This command will fail if executed in a browser that does not support |
| 26 | +it. |
| 27 | + |
| 28 | +::: |
| 29 | + |
| 30 | +## Syntax |
| 31 | + |
| 32 | +```javascript |
| 33 | +cy.press(key) |
| 34 | +cy.press(key, options) |
| 35 | +``` |
| 36 | + |
| 37 | +## Signature |
| 38 | + |
| 39 | +```typescript |
| 40 | +interface PressCommand { |
| 41 | + ( |
| 42 | + key: KeyPressSupportedKeys, |
| 43 | + options?: Partial<Cypress.Loggable> & Partial<Cypress.Timeoutable> |
| 44 | + ): void |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +## Usage |
| 49 | + |
| 50 | +<Icon name="check-circle" color="green" /> **Correct Usage** |
| 51 | + |
| 52 | +```javascript |
| 53 | +cy.get('input.first').focus() |
| 54 | +cy.press(Cypress.Keyboard.Keys.TAB) |
| 55 | +cy.get('input.second').should('have.focus') |
| 56 | +``` |
| 57 | + |
| 58 | +<Icon name="exclamation-triangle" color="red" /> **Incorrect Usage** |
| 59 | + |
| 60 | +```javascript |
| 61 | +cy.get('input.first').focus() |
| 62 | +cy.press(Cypress.Keyboard.Keys.TAB) |
| 63 | + // Errors because press yields null |
| 64 | + .should('have.focus') |
| 65 | +``` |
| 66 | + |
| 67 | +### Arguments |
| 68 | + |
| 69 | +<Icon name="angle-right" /> **key _(String)_** |
| 70 | + |
| 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. |
| 72 | + |
| 73 | +### Supported Keys |
| 74 | + |
| 75 | +| Reference | Value | |
| 76 | +| --------------------------- | ------- | |
| 77 | +| `Cypress.Keyboard.Keys.TAB` | `"Tab"` | |
| 78 | + |
| 79 | +<Icon name="angle-right" /> **options _(Object)_** |
| 80 | + |
| 81 | +Pass in an options object to change the default behavior of `.press()`. |
| 82 | + |
| 83 | +| Option | Default | Description | |
| 84 | +| --------- | ----------------------------------------------------------------- | ----------------------------------------------------------------------------------- | |
| 85 | +| `log` | `true` | Displays the command in the [Command log](/app/core-concepts/open-mode#Command-Log) | |
| 86 | +| `timeout` | [`defaultCommandTimeout`](/app/references/configuration#Timeouts) | Time to wait for `cy.press()` to resolve before timing out | |
| 87 | + |
| 88 | +<HeaderYields /> |
| 89 | + |
| 90 | +- `cy.press()` yields `null`. |
| 91 | + |
| 92 | +## Examples |
| 93 | + |
| 94 | +### Test focus order of tab |
| 95 | + |
| 96 | +```js |
| 97 | +it('moves focus to the next form element when pressing Tab', () => { |
| 98 | + cy.visit('/my-login') |
| 99 | + cy.get('input.email').type('username') |
| 100 | + cy.press(Cypress.Keyboard.Keys.TAB) |
| 101 | + cy.get('input.password').should('have.focus') |
| 102 | +}) |
| 103 | +``` |
| 104 | + |
| 105 | +### Test autocomplete of search input with tab |
| 106 | + |
| 107 | +```js |
| 108 | +it('autocompletes search input when pressing Tab', () => { |
| 109 | + cy.get('[data-cy="search"]').type('cy') |
| 110 | + cy.press(Cypress.Keyboard.Keys.TAB) |
| 111 | + cy.get('[data-cy="search"]').should('have.value', 'cypress') |
| 112 | +}) |
| 113 | +``` |
| 114 | + |
| 115 | +## Notes |
| 116 | + |
| 117 | +### Transient activation |
| 118 | + |
| 119 | +By dispatching native keyboard events to the browser, this command will cause the browser to enter [Transient activation](https://developer.mozilla.org/en-US/docs/Glossary/Transient_activation) state. |
| 120 | + |
| 121 | +If your application prevents the default behavior of the `beforeunload` event, this may cause issues when navigating away from the current page. |
| 122 | + |
| 123 | +## History |
| 124 | + |
| 125 | +| Version | Changes | |
| 126 | +| ----------------------------------- | ---------------------------- | |
| 127 | +| [14.3.0](/app/references/changelog) | Added the `.press()` command | |
| 128 | + |
| 129 | +## See also |
| 130 | + |
| 131 | +- [`Cypress.Keyboard`](/api/cypress-api/keyboard-api) |
| 132 | +- [`.focus()`](/api/commands/focus) |
| 133 | +- [`.focused()`](/api/commands/focused) |
| 134 | +- [`.type()`](/api/commands/type) |
0 commit comments