How to Check for Attribute Values in Cypress

How to Check for Attribute Values in Cypress

Ferenc Almasi β€’ 2021 October 06 β€’ Read time 1 min read
  • twitter
  • facebook

To check for any value on any attribute in Cypress, we can use the have.attr assertion:

Copied to clipboard! Playground
// Check for only the presence of the attribute:
cy.get('a').should('have.attr', 'href');

// Check for the value as well:
cy.get('a').should('have.attr', 'href', '#value');
cy.get('a').should('have.attr', 'class', 'name');

The same functionality can also be achieved by using the invoke command in conjunction with the eq assertion:

Copied to clipboard!
// Using invoke:
cy.get('a')
  .invoke('attr', 'href')
  .should('eq', '#anchor');

Want to learn Cypress from end to end? Check out my Cypress course on Educative where I cover everything:

Learn Cypress with Educative
How to check for attribute values in Cypress
If you would like to see more webtips, follow @flowforfrank

Resources:

  • twitter
  • facebook
Did you find this page helpful?
πŸ“š More Webtips
Mentoring

Rocket Launch Your Career

Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies:

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.