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

298
Vistas
Cypress (javascript) 'else' block executes even when the first 'if' statement condition is true

New to Cypress and JavaScript, Please help

I was automating the test case to check the enabled/disabled state of the button on the condition of a few elements to have 'src' html attribute with a value '/Client/images/iconNo.png' (button will be disabled) and /Client/images/iconYes.png (button will be enabled).

I used if statement to check the above condition by wrapping the objects to get the value and using '.then()' method, below is the javascript code

cy.get('.eligibilitySuccessfulGrant > :nth-child(1) > .cursor')
  .then(($checkListIcon) =>{

    if(cy.wrap($checkListIcon).should('have.attr', 'src', '/Client/images/iconNo.png')){

        verifyQIAPTab.clickRequestOrientation().should('be.disabled')
    }
    else (cy.wrap($checkListIcon).should('have.attr', 'src', '/Client/images/iconYes.png'))
    {
         verifyQIAPTab.clickRequestOrientation().should('be.enabled').click()
}
})

Problem: if the first 'if' statement is true then it should pass the test case but the cypress is throwing an error that the else condition is not satisfied.

this is the cypress error: below is the cypress error:

where the cypress is checking that .should('be.enabled').click() condition. How can I resolve this?

OR

Is there any other way in cypress to check my above test case?

Please help and suggest Thanks a lot in advance

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Cypress commands are Asynchronous. That means you're better off using two separate if conditions. Moreover, you are wrapping the same element thrice. Using javascript assertions in combination with cypress commands would be a lot better.

This hasAttribute method for example can help you remove the assertion from the if condition.

Please share inner HTML for the buttons so I can help you further.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Cannot use a Cypress command to perform an if(), commands always return an object not true/false.

Use jQuery in place of command

if ($checkListIcon.attr('src') === '/Client/images/iconNo.png')
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