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

158
Visualizações
Asserting a DOM element if it has a specific text

I'm starting out in Cypress, I'm trying to select an element with this text:

Username is missing

My code is:

cy.get('[data-id="error"]').should('have.text', 'is missing')

Is there a way that I can just use a specific part of a string (in this case, 'is missing') and still be able to use the .should() assertion? I hope to pass the assertion using specific part of a string only.

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

0

you can use contain keyword to look string that contain a substring

with should to perform directly the assertion

cy.get('[data-id="error"]').should('contain', 'is missing')

or get only element that contain text with contains

cy.get('[data-id="error"]').contains('is missing')

them perform needed assertion on it

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use regular expression with match instead of have.text:

For example the text should contain test:

cy.get('[data-id="error"]').should('match', /test/)

Or simply

cy.get('[data-id="error"]').should('contain', 'test')
about 4 years ago · Juan Pablo Isaza Relatório

0

If you want to assert an partial text with should then you can use include.text

cy.get('[data-id="error"]').should('include.text', 'is missing')
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