Ash
Ash

Reputation: 896

How to click and hold in cypress?

I have a username/password input with a 'show password' button which shows the password text when clicked; but only if you hold it. As soon as you release the click, it will hide the password again.

The wrong way of testing it is:

  it('show password word', () => {
    cy.get('[data-cy="login-password-input"]').should('have.type', 'password')
    cy.get('[data-cy="show-password-btn"]').click()
    cy.get('[data-cy="login-password-input"]').should('have.type', 'text')
  })

Since this reverts back to type=password before I have a chance to check it. Is it possible to keep the click held down whilst performing the last verification check?

Upvotes: 3

Views: 6099

Answers (3)

Fseee
Fseee

Reputation: 2631

cy.get('[data-cy="show-password-btn"]').trigger('mousedown', {
  button: 0
})

Upvotes: 2

Hung
Hung

Reputation: 519

It works:

cy.get("@element").trigger("mousedown");

https://developer.mozilla.org/en-US/docs/Web/API/Element/mousedown_event

Upvotes: 0

QA Prof
QA Prof

Reputation: 63

{release:false}

can be added.
See https://docs.cypress.io/api/commands/type.html#Key-Combinations for reference

Upvotes: 0

Related Questions