Cypress:Unable to work with else if condition and won’t execute any step but timer is running [closed]

Unable to work with below else if condition and always stuck with if

cy.get("#result").iframeOnload().find(".text-error").invoke('text').as('iframeText');
            cy.get('@iframeText').then((iframeText) => {
                if(iframeText === expectedFailText){
                    ResultPage
                    .getResultErrorMessage()
                    .should("have.text", expectedFailText);
                }
                else if(expectedPassText){
                    ResultPage
                    .getResultPassedMessage()
                    .should("have.text", expectedPassText);
                }
            });

  • Well what is the actual value of iframeText and of expectedFailText?

    – 

  • @stackoverflow.com/users/182668/pointy the values of iframetext is getting from the element i.e You have not achieved the pass rate and expectedFailText is You have not achieved the pass rate both are same only

    – 




  • what is inside expectedPassText? if this is a falsey value, the else will never be executed. Maybe you meant else if(iframeText === expectedPassText){?

    – 




  • @Kaddath the expectedPassText is “You passed”

    – 

  • @JagannathaMv well if both values are the same, why would you expect the if part not to run?

    – 

It’s a great example of how not to write a test!

You can only get one outcome, so are you planning to run it repeatedly hoping to cover both if() and else() with random results?

I suggest you start by splitting into two tests and set up precursor conditions to give you passText and failText respectively.

On a technical note, you cannot use an alias with an <iframe>. That’s because the iframe has contentWindow and the alias will attempt to requery it, and fail.

Whatever is in .iframeOnload(), get rid of it and use the cypress-iframe plugin. Use it to first establish that the iframe has loaded, then run a normal test inside the <iframe> scope.

A rough example

it('tests the pass text', () => {
  // precursor condition - setup for pass
  cy.enter('#my-iframe').then(getBody => {
    getBody().find(".text-error").invoke('text')
      .should('have.text', expectedPassText)
  })
})

it('tests the fail text', () => {
  // precursor condition - setup for fail
  cy.enter('#my-iframe').then(getBody => {
    getBody().find(".text-error").invoke('text')
      .should('have.text', expectedFailText)
  })
})

Leave a Comment