How to Break 'for' loop inside promise 'Then' in Cypress Typescript as in below code:
for (let i = 2; i <= iterationspage; i++) {
cy.wait(5000)
cy.get(".page").contains(i).click()
cy.log("clicked on page"+i)
// let FlagFound='False'
homePage.getProductNamesSearchResults().each(($el, index, $list)=> {
const expProductName=$el.text()
if(expProductName.includes(this.addtocart.ProductToBuy)){
homePage.getAddToCartButton().eq(index).click()
homePage.getPriceTagForSearchedProducts().eq(index).then(function(productPrice){
cy.log(productPrice.text())
cy.log(expProductName)
})
//break
}
})
}
In fact you have two loops to break
let iterationspage = 10;
let data = ['test1','test2','test3','test4']
for (let i = 2; i <= iterationspage; i++) {
result = data.every(($el, index, $list) => {
if ($el === 'test3') {
return false;
}
console.log($el);
return true;
});
if (!result) {
break;
}
}
Cypress.env("lengthProd",12)//page count
findItem(ProductToFind)
function findItem(value: any){
const homePage= new HomePage()
const viewCartPopUp= new ViewCartPopUp()
const shoppingCartPage= new ShoppingCartPage()
function findInPage(index1: any){
let found=false
cy.get("div.bottom div div div cx-pagination a.page").its("length").then(len=>{
if(index1>Cypress.env("lengthProd")){
return false
}else{
cy.wait(5000)
cy.get(".page").contains(index1).click({force:true})
cy.log("clicked on page"+index1)
cy.wait(10000)
homePage.getProductNamesSearchResults().each(($el, index, $list)=> {
const expProductName=$el.text()
if(expProductName===value){
found =true
homePage.getAddToCartButton().eq(index).click()
homePage.getPriceTagForSearchedProducts().eq(index).then(function(productPrice){
cy.log(productPrice.text())
cy.log(expProductName)
})
return false
}
}).then(()=>{
if(!found){
findInPage(++index1)
}
})
}
})
}
findInPage(1)
}