philein-sophos
philein-sophos

Reputation: 372

Cypress contains statement in if/else condition

I am writing a code block to monitor a dialogbox and its contents. I've written a while loop which will track some activity progress, wherein there is status value as

"Status : In Progress"

and which when the progress completes changes to

"Status: Completed".

inside while loop, I've written an if/else block

while(true)
{

    if(cy.get('<locator>').contains('progress',{matchCase:false}))
    {
        statements
    }
    else
    {
        statements
    }
}

Here the while loop executes repeatedly until the "Status : In Progress" value is present,

i.e. the if condition becomes true, but when progress completes and status changes to "Completed" it fails and raises AssertionError, and stops the test case without entering the else block.

I have some statements which are required to be executed when if condition fails, and instead of returning AssertionError control should go to the else block.

How do I do that ?

Upvotes: 2

Views: 14266

Answers (1)

Ramona Schwering
Ramona Schwering

Reputation: 276

There is a nice guide on conditional testing in Cypress docs. When it comes to your example, I assume something like this could do the trick:

while(true)
{
    // Get the element first and use .then() command to proceed with it
    cy.get('<locator>').then(($btn) => {

          // assert on the text
          if ($btn.text().includes('progress')) {
               // do your statements
          } else {
               // do other statements
          }
  })
}

Or something similar. I hope that's helpful! 🙏

Upvotes: 4

Related Questions