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

156
Visualizações
Checking length of element in beforeEach on Cypress

I'm trying to setup my beforeEach to create a new email if none exist before starting my tests, but when I try to count how many emails there are in my beforeEach, it always logs as 0, even if there are more. If I log the count in my it block, it shows the correct count.

Does this look correct? How can I properly check the count in the beforeEach so that it doesn't always create a new email for every test?

describe("email", () => {
  beforeEach(() => {
    cy.visit("/");

    let count = Cypress.$('#emails').length

    // always logs 0
    cy.log(count)

    if(count == 0) {
      createNewEmail()
    }
  });

  it("email", () => {
    let count = Cypress.$('#emails').length

    // logs correct amount
    cy.log(count)
  });
});

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

0

Cypress commands are asynchronous. So when the test executes cy.visit("/") it does not mean that the next line will be executed after the page gets really loaded.

You can fix this as follows:

 beforeEach(() => {
    cy.visit("/").then(() => {
      let count = Cypress.$('#emails').length
      cy.log(count)
      if(count == 0) {
        createNewEmail()
      }
    })
}    

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