How to get an attribute from an element in Cypress?
below is the code i am getting the error for.
I want to get it's value property to assert that it has been updated by my test.
I have tried using its():
cy
.get(selector)
.its(“value”)
.should(“contain”, “-Updated”);
But get the error:
CypressError: Timed out retrying: cy.its() errored because the property: ‘value’ does not exist on your subject.
Hi Dhara,
In response to your inquiry on how to retrieve values and attributes of input elements in Cypress, here is the information you need.
To get the value of an input element, you should use .invoke('val')
:
cy.get('input').invoke('val').should('contain', 'mytext')
To get other attributes like placeholder, you can use .invoke('attr', 'placeholder')
:
cy.get('input').invoke('attr', 'placeholder').should('contain', 'username')
Feel free to reach out if you have any further questions or if there’s anything else I can assist you with.