-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: documentation for the press() command #6135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2ea2abb
docs for the press() command, including a callout in the accessibilit…
cacieprins c843f74
add press to TOC
jennifer-shehane 61ca78c
Move some content around, mention accesiibility
jennifer-shehane b6adcf9
remove 'focus' note
jennifer-shehane cfb3ff9
Document Keyboard.Keys
jennifer-shehane 30724a3
update reference to command
jennifer-shehane 5136ef9
Update example to be a little more real case
jennifer-shehane 65cdc6c
Add another example of autocomplete with tab
jennifer-shehane bf6ec3d
Add link to keyboard api page
jennifer-shehane 86024bc
Fix version number for introduction
jennifer-shehane 02f0e6c
Fix broken link
jennifer-shehane 8b490d7
alphabetize see also
jennifer-shehane ee7638a
Update package.json
jennifer-shehane 1f3240e
Fix broken link
jennifer-shehane af05f3c
lint
jennifer-shehane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
--- | ||
title: 'cy.press() | Cypress Documentation' | ||
description: Trigger native key events in your application to simulate keyboard interactions. | ||
sidebar_label: press | ||
slug: /api/commands/press | ||
componentSpecific: false | ||
--- | ||
|
||
<ProductHeading product="app" /> | ||
|
||
# press | ||
|
||
Trigger native key events in your application to simulate keyboard interactions. | ||
|
||
A `keydown`, `press`, and `keyup` event will be dispatched directly to the browser window. | ||
|
||
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. | ||
|
||
Currently, the only key supported is `Tab`. | ||
|
||
:::caution | ||
|
||
<strong>Supported Browsers:</strong> | ||
This command is supported in chromium browsers and Firefox versions >= | ||
v135. WebKit is not supported. This command will fail if executed in a browser that | ||
does not support it. | ||
|
||
::: | ||
|
||
## Syntax | ||
|
||
```javascript | ||
cy.press(key) | ||
cy.press(key, options) | ||
``` | ||
|
||
## Signature | ||
|
||
```typescript | ||
interface PressCommand { | ||
( | ||
key: KeyPressSupportedKeys, | ||
options?: Partial<Cypress.Loggable> & Partial<Cypress.Timeoutable> | ||
): void | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
<Icon name="check-circle" color="green" /> **Correct Usage** | ||
|
||
```javascript | ||
cy.get('input.first').focus() | ||
cy.press(Cypress.Keyboard.Keys.TAB) | ||
cy.get('input.second').should('have.focus') | ||
``` | ||
|
||
<Icon name="exclamation-triangle" color="red" /> **Incorrect Usage** | ||
|
||
```javascript | ||
cy.get('input.first').focus() | ||
cy.press(Cypress.Keyboard.Keys.TAB) | ||
// Errors because press yields null | ||
.should('have.focus') | ||
``` | ||
|
||
### Arguments | ||
|
||
<Icon name="angle-right" /> **key _(String)_** | ||
|
||
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. | ||
|
||
### Supported Keys | ||
|
||
| Reference | Value | | ||
| --------------------------- | ------- | | ||
| `Cypress.Keyboard.Keys.TAB` | `"Tab"` | | ||
|
||
<Icon name="angle-right" /> **options _(Object)_** | ||
|
||
Pass in an options object to change the default behavior of `.press()`. | ||
|
||
| Option | Default | Description | | ||
| ---------------------------- | --------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `log` | `true` | Displays the command in the [Command log](/app/core-concepts/open-mode#Command-Log) | | ||
| `timeout` | [`defaultCommandTimeout`](/app/references/configuration#Timeouts) | Time to wait for `.click()` to resolve before [timing out](#Timeouts) | | ||
|
||
<HeaderYields /> | ||
|
||
- `cy.press()` yields `null`. | ||
|
||
## Examples | ||
|
||
### Test focus order of tab | ||
|
||
```js | ||
it('moves focus to the next form element when pressing Tab', () => { | ||
cy.visit('/my-login') | ||
cy.get('input.email').type('username') | ||
cy.press(Cypress.Keyboard.Keys.TAB) | ||
cy.get('input.password').should('have.focus') | ||
}) | ||
``` | ||
|
||
### Test autocomplete of search input with tab | ||
|
||
```js | ||
it('autocompletes search input when pressing Tab', () => { | ||
cy.get('[data-cy="search"]').type('cy') | ||
cy.press(Cypress.Keyboard.Keys.TAB) | ||
cy.get('[data-cy="search"]').should('have.value', 'cypress') | ||
}) | ||
``` | ||
|
||
## Notes | ||
|
||
### Transient activation | ||
|
||
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. | ||
|
||
If your application prevents the default behavior of the `beforeunload` event, this may cause issues when navigating away from the current page. | ||
|
||
## History | ||
|
||
| Version | Changes | | ||
| ------------------------------------------ | --------------------------- | | ||
| [14.3.0](/app/references/changelog) | Added the `.press()` command | | ||
|
||
## See also | ||
|
||
- [`Cypress.Keyboard`](/api/cypress-api/keyboard-api) | ||
- [`.focus()`](/api/commands/focus) | ||
- [`.focused()`](/api/commands/focused) | ||
- [`.type()`](/api/commands/type) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,5 +69,6 @@ | |
"last 1 firefox version", | ||
"last 1 safari version" | ||
] | ||
} | ||
}, | ||
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.