Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!