Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing attribute text to match part of href link in Cypress

Tags:

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.

like image 680
Neil Dello Avatar asked Oct 31 '25 13:10

Neil Dello


1 Answers

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;
  });
like image 159
Fody Avatar answered Nov 04 '25 03:11

Fody



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!