Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cypress conditional statement depending on whether the input field is disabled or not

I have some input fields on a page. They can be disabled or not. So I need to type the text in this field just in case when it’s not disabled. I try to do this way:

      fillPickUpTown(pickUpTown: string) {
        cy.get(`[data-testid="pick-up-town"]`).should('not.be.disabled').then(() => {
         cy.get(`[data-testid="pick-up-town"]`).type(pickUpTown);
        });
      }

But I have failed test with error "Timed out retrying after 10000ms: expected ” not to be ‘disabled’".
How can I type to this field just when it’s not disabled and do nothing when it is?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use JQuery :enabled to check and implement If-Else.

fillPickUpTown(pickUpTown: string) {
  cy.get(`[data-testid="pick-up-town"]`).then(($ele) => {
    if ($ele.is(":enabled")) {
      cy.get(`[data-testid="pick-up-town"]`).type(pickUpTown)
    }
  })
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading