Mahshad
Mahshad

Reputation: 89

Testing text of an element using Cypress

I'd like to validate the text of an element (p element, for instance) with the help of Cypress. I have used this code:

cy.get('#word').should('have.value', 'Color')

and I received this: expected <p#word> to have value Color, but the value was '' Evidently, it validates the CSS but not the html element value. How can I validate the element content here?

Upvotes: 1

Views: 750

Answers (1)

Alapan Das
Alapan Das

Reputation: 18586

If you are asserting the inner Text, instead of have.value you have to use have.text.

cy.get('#word').should('have.text', 'Color')

Or, If you want to assert a partial string, you can use include.text

cy.get('#word').should('include.text', 'Color')

Upvotes: 2

Related Questions