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

247
Visualizações
Cypress: Check if Select Option Exists

I'm trying to check if there is an option in my select using the code below, but it keeps failing, can somenone give some help?

My Select have about 70 names and I'm trying to loop all them looking for the specific name.

        cy.get('[id="names"] option').each(($ele) => {
            expect($ele).to.have.text('Have This Name')
          })

Thanks in advance,

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

0

I would not use .each(), only one will pass but any other will fail.

Use .contains() if your text is specific enough (not in multiple options)

cy.contains('[id="names"] option', 'Have This Name')  // fails only if 
                                                      // no option has the text

If you have to match exactly, filter the options

cy.get('[id="names"] option')
  .filter((idx, el) => el.innerText === 'Have This Name')  // fails if filter 
                                                           // returns 0 items

If you want .each() for another reason, this will do

let found;
cy.get('[id="names"] option')
  .each(($option) => {
    if ($option.text() === 'Have This Name') {
      found = $option
      return false // return now, have found it
    }
  })
  .then(() => {    // after loop exit
    expect(found).to.have.text('Have This Name')
  })
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