Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
Break for loop inside promise Then in Cypress Typescript

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
        }
    })
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

In fact you have two loops to break

  • the main for loop where break will stop it
  • the each loop where you can't use break because you are in a callback -> you can use every and return false to stop this loop when condition is meet

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;
      }
}

about 4 years ago · Juan Pablo Isaza Report

0

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)
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!