Cypress Assert URL

Profile picture for user arilio666

In this article, we will discuss how we can assert URLs in cypress. Asserting URLs is a way of verifying where we are currently.

Sometimes a tester must know the URL and endpoints of the URL of the current page he is testing on. This thing can get easy for him to handle.

We can use the cy.url() to assert the URL.

Syntax:

cy.url()

Example:

1.) Eq

  • We can use the should assertion method in cypress and then pass in 'eq.'
cy.url().should('eq', 'https://www.programsbuzz.com/user/login');
  • We can see here the exact URL matches the one that we have provided to verify using cy.url().
  • URL can also be defined in our cypress as baseURl and passed to avoid hardcode.
cy.url().should('eq', Cypress.config().baseUrl + '/login')
  • This is the same as verifying https://www.programsbuzz.com/user/login.

2.) Contain

  • Using contain, we can partially assert url by passing partial endpoints inside the assertion condition.
cy.url().should('contain,' '/user');
  • So here we can witness that we have passed in partial endpoint, and it got verified using cy.url().