How to Test that URL Should Not Include a String in Cypress
If you want to verify that your URL should not include a string in Cypress, you can use the should('not.contain')
assertion with either a string or a regex:
cy.url().should('not.contain', '/home');
// You can also use a regex
cy.url().should('not.contain', /home/);
You can also use this against the previous URL. Let's say you are testing the navigation and you would like to verify the URL after navigating away from the page. This can be done with a then
callback in the following way:
cy.visit('https://cypress.io');
cy.url().then(url => {
cy.get('.login').click();
cy.url().should('not.contain', url);
// You can also check equality
cy.url().should('not.eq', url);
});
In the then
callback, now you have access to the previous URL that you can use against the current one. Want to learn Cypress from end to end? Check out my Cypress course on Educative where I cover everything:
Resources:
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: