jeudi 18 mars 2021

Exit from while loop with if/else in Cypress

I am writing a test case which requires me to reload the page N number of times, and compare its title for a value, if that value does not exists then break the while loop without rising error.

Below is a demo program, similar to the one that I am looking to implement.

/// <reference types='cypress' />

it("Visiting Google",function(){
    var webUrl = 'https://html5test.com/'
    cy.visit(webUrl)
    var loop_iter = 0
    while(loop_iter < 5)
    {
        cy.title().then(($text_data) =>{
            if($text_data.includes('HTML'))
            {
                cy.log(" --> ITERATION = ",loop_iter)
                cy.reload()
            }
            else
            {
                cy.log("Unknown website")
                loop_iter = 10
            }
        })
        loop_iter += 1
    }
})

I need a way to break from the while loop when the else part is executed, without rising any error. I tried changing the loop_iter value, but it wont change the value and the loop will continue, when some other url is given or if case fails.

Aucun commentaire:

Enregistrer un commentaire