A Bit Of Everything
A Bit Of Everything

Reputation: 1

Conditional Testing with Cypress e2e

I have a test case of my site with cypress. however the site, which is been built in vue.js has lets say 2 journeys. in one of them there is an button which I want to click. So, what I want to do is the following:

if ('button exists') {
  cy.get(#"button-class").click()    //basically click the button
}

If the button isn't there, which can be true for the second journey then carry on, as no button will be there.

Whatever I have tried so far failed. All I want is a simple if statement in cypress.

Upvotes: 0

Views: 239

Answers (2)

TesterDick
TesterDick

Reputation: 10545

This became a lot easier with the release of cypress-if package

cy.get(#"button-class")
  .if()
  .click()

Update for Cypress v12

Recent internal changes in Cypress have made this package stop working.

Please see this issue Can we please overwrite query commands #25078 and register your "vote" if you like the package and wish it were fixed.

Upvotes: 4

PeaceAndQuiet
PeaceAndQuiet

Reputation: 1790

You can get the parent element of the button, which should be displayed everytime and query for your button inside its then() callback

cy.get('css of always displayed parent').then($parent => {
    if ($parent.find('#button-class').length) {
        cy.wrap($parent).find('#button-class').click();
    }
});

Upvotes: 0

Related Questions