tgordon18
tgordon18

Reputation: 1849

Cypress undo input text

I'd like to write a Cypress test to verify that pressing ctrl-z (or cmd on a Mac but ignoring that for now) in an <input> text undoes the previously-typed text. For example:

cy.get('input')
  .type('abc')
  .should('have.value', 'abc')
  .type('{ctrl+z}')
  .should('have.value', '');

However, the .type('{ctrl+z}') doesn't actually seem to trigger the undo. This surprised me because I was able to trigger a custom keydown EventListener with the same .type('{ctrl+z}').

I know Cypress doesn't support keyboard shortcuts for copying/pasting, and I'm wondering if I'm running up against a limitation here.

I get that it's usually unnecessary to check something like this, which is default browser behavior and should work. But in my case, I want to make sure that my global ctrl-z listener isn't preventing the default undo behavior from firing.

Upvotes: 4

Views: 541

Answers (1)

Fody
Fody

Reputation: 31974

You can do it with cypress-real-events

cy.get("input").focus();
cy.realType("abc")
cy.get("input").should('have.value', 'abc')    // passes
cy.realPress(["Control", "z"])
cy.get("input").should('have.value', '')       // passes

Install with

npm install cypress-real-events
// or
yarn add cypress-real-events

Add an import to cypress/suport/e2e.js

import "cypress-real-events/support";

Upvotes: 2

Related Questions