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

297
Views
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 answers
Answer question

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 Report

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 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!