Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

425
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda