Use of For Loop & return method in Cypress automation :
describe(” Use Of For Loop and return method “,function(){
it(‘Test Case 15’, function() {
cy.visit(‘www.exapmle.com/’,{timeout:1000});
cy.get(‘.tab-tools > .icon > .svg-inline–fa’).click({force:true});
// Now this method will fetch the text which is already displayed & will save it.
cy.get(‘.last-updated-date > span’).then(($txt) => { const txt = $txt.text()
cy.log(txt)
// For loop will run 50 times & after each 3 seconds
// it will check Visibility of element.
// If it gets newly appeared element/Text It will be terminated before 50 runs
var i;
for (i = 0; i < 50; i++) {
cy.wait(3000)
cy.get(‘.last-updated-date > span’).each(($text) => {
const lastupdate = $text.text()
cy.log(lastupdate)
// If the New text/element is appeared then Compare it with first element
// and return/exit for loop.
if ($text.text() ==! txt) {
cy.wrap($text)
cy.log(‘Test Case is Failed’)
return;
}
else {
return;
}
return false;
})
}
// The page will be reload when for loop is terminated
// You can also Skip this one.
cy.reload();
cy.log(‘Test Case is Passed !’)
});
});
});
Excellent post. I definitely appreciate this website. Thanks!