@walmyr-filho/cy-press
v1.0.5
Published
A silly Cypress `.press()` command that simulates pressing a keyboard key.
Maintainers
Readme
@walmyr-filho/cy-press
A silly Cypress .press() command that simulates pressing a keyboard key.
Installation
Run npm i @walmyr-filho/cy-press -D to install it as a dev dependency.
Note:
@walmyr-filho/cy-pressis incompatible withcypressv14.3.0 or greater since cypress v14.3.0 added thecy.presscommand, which conflicts with this one, although thecy.presscommand must be chained to glogalcyobject, and this.pressmust be chained to a previous subject.
Usage
Import (or require) the @walmyr-filho/cy-press lib inside the cypress/support/e2e.js file (or cypress/support/index.js on Cypress version < 10.)
// cypress/support/e2e.js
import '@walmyr-filho/cy-press'
// or
// require('@walmyr-filho/cy-press')Then, inside the test file, chain the .press() command to a typeable field (for example), and pass to it a valid key (e.g., 'enter' or 'backspace')
✅ Correct usage
// cypress/e2e/spec.cy.js
it('types and presses enter', () => {
cy.visit('https://example.com/searchForm')
cy.get('input[type="text"]')
.type('cypress.io')
.press('enter')
// Assertion here
})🛑 Incorrect usage
// cypress/e2e/spec.cy.js
it('tries to press without a subject element', () => {
cy.visit('https://example.com/searchForm')
cy.press('enter') // This won't work and will result in an error
})Examples
cy-press-lib-exampleis an example project that uses the@walmyr-filho/cy-presslibrary.Check the
cypress/support/e2e.jsfile for the import of it, and thecypress/e2e/press.cy.jsfile for its usage.hackernews-react-app is another example of its usage. Checkout the
cypress/e2e/spec.cy.jsfile.
Available keys
The available keys are described on the official Cypress documentation + the following combination of keys: ctrl+a, CTRL+A, cmd+a, and CMD+A.
Made with ❤️ by Walmyr Filho.
