Handling Asynchronous Commands in Cypress: Ensuring URL Changes After Click

Handling Asynchronous Commands in Cypress: Ensuring URL Changes After Click

21 May 2024 Stephan Petzl Leave a comment Tech-Help

When working with Cypress to automate tests, you may encounter situations where you need to verify that a URL changes after a specific action, such as clicking a button. This can be particularly challenging due to the asynchronous nature of Cypress commands. In this article, we will guide you through a practical example to handle this scenario effectively.


Imagine you have an editor page. When you add any content and click the “Save” button, the URL changes by appending a random ID. Your goal is to check if the ID changes every time you click the “Save” button.

Common Pitfall

A common mistake is to assign the URL result to a variable directly and then try to compare it after the button click. The code might look something like this:

const currentURL = cy.url();
cy.url().should('not.eq', currentURL);

This approach fails because cy.url() returns a chainable type, not a primitive string value. Thus, the comparison will not behave as expected.


The correct way to handle this is to use the .then() method to work with the yielded value of the cy.url() command. Here’s how you can do it:

cy.url().then(url => {
  cy.url().should('not.eq', url);

In this solution, cy.url().then(url => { ... }) ensures that the URL is captured and can be compared after the “Save” button is clicked. This approach leverages Cypress’s asynchronous command execution model effectively.

Further Reading

To deepen your understanding of Cypress’s asynchronous nature, you can refer to the following articles:

Enhancing Your Testing Workflow with Repeato

While Cypress is a powerful tool for web automation, managing and maintaining tests can sometimes be cumbersome. This is where Repeato can significantly enhance your testing workflow. Repeato is a no-code test automation tool designed for iOS and Android applications. It offers an intuitive test recorder and a scripting interface for advanced use cases.

Repeato’s computer vision and AI capabilities make it particularly fast to edit and run tests. Although primarily focused on mobile apps, Repeato also allows testing websites inside an Android emulator or device, with explicit web testing support coming soon. This makes it an excellent choice for comprehensive test automation across different platforms.

For more information on how Repeato can help streamline your testing processes, visit our documentation or check out our blog.

Like this article? there’s more where that came from!