Search code examples
javascripttestingautomated-testscypresscypress-cucumber-preprocessor

Cypress If/Else condition in Cucumber


I am using Cucumber for testing a bank application where I can open a Checking and a Savings account based on the value in the dropdown. Sample code :

When("Savings account")
    cy.get('#type').select('SAVINGS')
When("Checking account")
    cy.get('#type').select('CHECKING')

Then("Account is created")
    cy.get('#accountType').should('have.text', 'CHECKING') or 
    cy.get('#accountType').should('have.text', 'SAVINGS')
    based on what is selected in the When statement. 

How can this be done?


Solution

  • You can use should(callback) for checking the text when criteria is more complicated.

    Using should() gives you retry and is preferred over .then().

    cy.get('#accountType')
      .should($el => {
        const text = $el.text().toUpperCase()          // make sure case matches
        expect(['CHECKING', 'SAVINGS']).to.include(text)   
      })