Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

427
Views
If/else condition in Cypress not working as expected

I have this code

Cypress.Commands.add('VerifyLoginState', () => {
    if(cy.contains('Login')) {
        cy.get('.form-control').eq(0).type('firstfield')
        cy.get('.form-control').eq(1).type('secondfield')
        cy.get('.btn-login').click()
        cy.wait(2500)
        cy.contains('Upcoming Appointments').should('be.visible')
    }
    else
    {
        cy.contains('Appointment summary').should('be.visible')
    }
})

How should I write the code so that it can pass to the condition of else, when I am authenticated in the browser and the condition of if is not valid?

In other words, I want to check if an element is present on the page, and even if it is not present, the code should not give an error and move on

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Cypress yields the result of cy functions, and does not return them. So your if/else will not work as it would in traditional JavaScript. Check out this article from Cypress about conditional testing. Something like the following should help you out:

// Get the body of the DOM
cy.get('body').then(($body) => {
  // Check if the body contains the `Login` element
  if ($body.contains('Login').length) {
        cy.get('.form-control').eq(0).type('firstfield')
        cy.get('.form-control').eq(1).type('secondfield')
        cy.get('.btn-login').click()
        cy.wait(2500)
        cy.contains('Upcoming Appointments').should('be.visible')
    } else {
        cy.contains('Appointment summary').should('be.visible')
    }
about 4 years ago · Juan Pablo Isaza Report

0

Another option is to use within

ref: https://docs.cypress.io/api/commands/within#Syntax

cy.contains('Login')
    .within(() => {
        cy.get('.form-control').eq(0).type('firstfield')
        cy.get('.form-control').eq(1).type('secondfield')
        cy.get('.btn-login').click()
        cy.wait(2500)
        cy.contains('Upcoming Appointments').should('be.visible')
      })
about 4 years ago · Juan Pablo Isaza Report

0

You may find it easier to test for either/or text using jQuery :contains and multiple selectors separated by a comma

cy.get('body')
  .children(':contains(Login), :contains(Appointment summary)')  // only one is on the page
  .invoke('text')
  .then(labelText => {
    if (labelText.trim() === 'Login') {
      cy.get('.form-control').eq(0).type('firstfield')
      ... etc
    } else {
      cy.contains('Appointment summary').should('be.visible')
    }
  })
 
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!