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

342
Visualizações
Cypress: array is empty after exiting .then block

Im trying to store few strings inside an array.

it('Action -> delete -> check list of camapaigns to delete',function(){
    let campArr=[]
    
    cy.get('md-checkbox').then((box)=>{
        for(let i=1;i<box.length;i++){
            cy.get(`.md-body > :nth-child(${i}) > :nth-child(3)`).then(($el)=>{
                let elToText=$el.text()
                campArr[i-1]=elToText
            })
        }
    })
    cy.log(campArr.length)
    cy.get('md-checkbox').eq(0).click()
    cy.get('.btn-group>button').eq(0).click()
    cy.get('.dropdown-menu-left > :nth-child(3) > .ng-binding').click()
    cy.log(campArr[0])
    for(let i=0;i<campArr.length;i++){
        cy.get(`div.ng-scope > ul > :nth-child(${i+1})`).should('contain.text',campArr[i])
        cy.log('checked')
    }
})

I was able to store the strings inside the array but after exiting .then block, the array is empty. What should i do to keep the values inside the array?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The easiest way to make access to the campArr synchronous with the assignment is to use a then callback:

it('Action -> delete -> check list of camapaigns to delete',function(){
    let campArr=[]
    
    cy.get('md-checkbox').then((box)=>{
        for(let i=1;i<box.length;i++){
            cy.get(`.md-body > :nth-child(${i}) > :nth-child(3)`).then(($el)=>{
                let elToText=$el.text()
                campArr[i-1]=elToText
            })
        }
    })
    cy.log(campArr.length)
    cy.get('md-checkbox').eq(0).click()
    cy.get('.btn-group>button').eq(0).click()
    cy.get('.dropdown-menu-left > :nth-child(3) > .ng-binding').click().then(() => {
       cy.log(campArr[0])
       for(let i=0;i<campArr.length;i++){ 
           cy.get(`div.ng-scope > ul > :nth-child(${i+1})`).should('contain.text',campArr[i])
           cy.log('checked')
       }
    }
})
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