Home > Net >  Conditional Testing with Cypress e2e
Conditional Testing with Cypress e2e

Time:10-08

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 excta button which i want to click. So, what i want to do is the foolowing:

`if ('button exists') { cy.get(#"button-class").click()

//basically click the button

}`

if the button isnt there, which can be true for the second jurney then carry on, as no button will be there.

whatever i have tried so far failed. All i want is asimple if statement in scypress.

CodePudding user response:

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();
    }
});

CodePudding user response:

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

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