Cypress Keyboard API

Profile picture for user arilio666

The Keyboard API allows us to set the default values on how the type() command is executed.

Syntax

Cypress.Keyboard.defaults(options)

Arguments - Options

  • keystrokeDelay: The default time in milliseconds is 10, and we can increase this value so that the delay can be observed while the type command executed. Value 0 removes the delay, and note that, it must be a non-negative number.

Set the Keystroke Delay in Index.js

A great place to put the keystroke configuration is in our cypress/support/index.js file since it is loaded before any test files get executed. Plus, it is pretty convenient and neat rather than using the syntax every time in the test code.

Cypress.Keyboard.defaults({
  keystrokeDelay: 20,
})

Remove 20 and set it to zero if you want to remove the keystroke delay.

Cypress.Keyboard.defaults({
  keystrokeDelay: 0,
})

Set the Keystroke Delay in Index.JS

That's it. We are done now. Every time the type command is executed, the keystroke in the input field is according to the delay time in milliseconds we provided in the index.js file.

Set Keystroke Delay in Describe and It

The other way is to use the keystroke delay in every test Configuration.

  • If you will specify Key stroke delay inside it, keystroke delay will be applied to all typing in this test
  • and by specifying key stroke delay in describe , you can add or removes keystroke delay in all tests in the describe suite.
describe('The Key combinations',() => {
    it('visit the site, perform various clicks', { keystrokeDelay: 0 },()=>{
        cy.visit('https://www.programsbuzz.com/')
       cy.get('.gv-icon-52').click()
       cy.get('#edit-keys').type('Cypress{selectAll}{backspace}selenium')
    })
})