Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

284
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda