shreyas
shreyas

Reputation: 57

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

Upvotes: 0

Views: 340

Answers (2)

Michael Hines
Michael Hines

Reputation: 1148

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')

Upvotes: 1

Syeda Aeimen Bukhari
Syeda Aeimen Bukhari

Reputation: 51

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.

Upvotes: 1

Related Questions