How to Test that URL Should Not Include a String in Cypress

How to Test that URL Should Not Include a String in Cypress

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

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:

Copied to clipboard!
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:

Copied to clipboard! Playground
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:

Learn Cypress with Educative
How to test that a URL should not include a string in Cypress
If you would like to see more webtips, follow @flowforfrank

Resources:

  • twitter
  • facebook
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

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.