Home > Back-end >  Cypress If/Else condition in Cucumber
Cypress If/Else condition in Cucumber

Time:08-31

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?

CodePudding user response:

You can use the to.be.oneOf assertion for this.

cy.get('#accountType')
  .invoke('text')
  .then((text) => {
    expect(text).to.be.oneOf(['CHECKING', 'SAVINGS'])
  })

CodePudding user response:

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)   
  })
  • Related