Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Testing attribute text to match part of href link in Cypress

I’m testing all the links on a page but I only want a partial match.

I can test the full href like this

cy.visit('/');
cy.get('.class').find('a').should("have.attr", "href", "/123/456");

but I want to test the string partially, for example /456. I tried .contains() but it doesn’t work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use include,

cy.get('.class').find('a')
  .should('have.attr', 'href')
  .and('include', '/456');

or you can switch the subject

cy.get('.class').find('a')
  .invoke('attr', 'href')
  .should('contain', '/456');

or a longer version using jquery

cy.get('.class').find('a')
  .should('have.attr', 'href')
  .then(href => {
    expect(href.endsWith('/456')).to.be.true;
  });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading