How to handle Errors in Cypress

On This Page Understanding Error Handling in CypressMarch 25, 2026 · 6 min read · Tool Comparison

How to handle Errors in Cypress

Effective fault treatment is a fundamental component of building reliable and maintainable. In Cypress, managing errors and exception ensures tests run smoothly and cater meaningful resultant.

In Cypress, errors can originate from the webpage under test, issues in the exam script, or unexpected application doings. If not handled decent, these errors can cause tryout to betray untimely and create debugging more unmanageable.

This article explores different strategies for address errors in Cypress, focusing on scenarios such as unexpected condition codes, examination failures, and exceptions from the covering under trial.

Understanding Error Handling in Cypress

An Exception or Error is an unnatural event that may separate the normal flow of trial script execution, causing the tests to neglect. In Cypress, elision may initiate from the Application/web Page Under Test or your mechanization script.

Error handle in Cypress refers to managing errors that occur during examination performance. These errors can halt from the application under exam, network failures, or issues within the test book itself. Error handle ensures tests don & # 8217; t fail unexpectedly and provides clearer insights into topic.

Effectual error cover offers multiple benefits, including:

  • Improved Test Stability:Reduces false failures cause by transient issues, such as timing or meshing postponement.
  • Clearer Debugging:Provides actionable fault content, making it easy to identify and resolve issues.
  • Coherent Test Execution:Ensures tests run reliably still when unexpected errors happen in the application or network.
  • Better Reporting:Offers more detailed context in test failures, aiding in quicker root-cause analysis.
  • Enhanced Efficiency:Allows trial to preserve running after fault are handled, reducing examination intermission and saving time.
  • Increased Test Coverage:Helps ensure that more scenarios are tested, even in the presence of error.

By leveraging platforms like, you can extend Cypress ’ s capableness by accomplish your examination on a variety of real browsers and device in the cloud.

This facilitate ensure consistent mistake handling across different environments and supply a comprehensive sight of your application ’ s behavior under diverse weather, such as different browsers and operating system.

How to perform Cypress Error Handling?

Unlike other, Cypress doesn & # 8217; t allow you to use the try and catch cube to handle the exclusion. Cypress provides a unique mechanism for manage elision in your code.

Handling different types of Exceptions, such as:

  1. Handling Exception from Webpage Under Test in Cypress
  2. Handling Exceptions within the Code in Cypress
  3. Validate Specific Exceptions
  4. Handling Failure Exception in Cypress
  5. Handing Exception due to Unexpected Status Code in Cypress

Here is a detailed breakdown of each type below.

Handling Exception from Webpage Under Test in Cypress

Consider a scenario where you are pilot to one of the web pages, which is throwing exceptions.

For example, navigatinghttps: //wxyz.in throws

Uncaught SyntaxError: Unexpected token ' & lt; '

This has nothing to do with your tryout, but still, the test would fail due to the lead webpage throwing fault.

Read More:

Handling Exceptions within the Code in Cypress

Executing the above test script in Cypress causes the examination to fail with the fault message& # 8220; The following error originated from your application code, not from Cypress. & # 8221;

For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

As mentioned before, using a try-catch block doesn & # 8217; t help. To handle the mistake from the Web page under test, cypress provides the special bidding

cy.on ('uncaught: exclusion ', (err, runnable) = & gt; {})

This command always listens to the elision return false and will cut these errors from fail tests. The above command can be modified to catch the exception as seen below.

describe ('Exception Handling In Cypress ', () = & gt; {it ('Navigate to webpage ', () = & gt; {cy.on ('uncaught: exception ', (err, runnable) = & gt; {revert false}) cy.visit ('https: //www.wxyz.in ');})})

Validate Specific Exceptions

It is not good to discount all the exceptions, there are hazard you may lose the important bugs in your application so it is always recommend to plow only known exception.

The err.messageprovides the full exception substance, you postulate to validate employifstatus. Modify the codification to manage specific Cypress uncaught Exceptions as seen below:

describe ('Exception Handling In Cypress ', () = & gt; {it ('Navigate to webpage ', () = & gt; {cy.on ('uncaught: exclusion ', (err, runnable) = & gt; {if (err.message.includes ('Unexpected token ')) {console.log ('Application Error Javascript Token ') homecoming false;} return true}) cy.visit ('https: //www.wxyz.in ');})})

In the above codification, If the & # 8216;Unexpected token& # 8216; erroneousness is thrown in the application, Cypress cut it, if there is any other exception thrown, then it will differentiate the test as a fail.

Read More:

Handling Failure Exception in Cypress

The above method handles simply Cypress uncaught exception scenarios; it doesn & # 8217; t cover the exception caused by your test script or mechanisation code.

Scenario: You might hold to click on the button, but it might not exist

Cypress cast an error stating, & # 8220;Timed out rehear after 4000ms: Expected to find factor: # buttondoestexist, but ne'er found it.”

describe ('Exception Handling In Cypress ', () = & gt; {it ('Navigate to webpage ', () = & gt; {cy.visit ('https: //www.google.in '); cy.get (' # buttondoestexist ')})})

To handle the above exception, you need to use the following Cypress command

Cypress.on ('fail ', (error, runnable) = & gt; {}

Modify the above code to handle the exception as seen below

describe ('Exception Handling In Cypress ', () = & gt; {it ('Navigate to webpage ', () = & gt; {Cypress.on ('fail ', (error, runnable) = & gt; {}) cy.visit ('https: //www.google.in '); cy.get (' # buttondoestexist ')})})

The above codification enables us to legislate the test even if there is an exclusion. However, in a real-world scenario, one must handle different exceptions.

Here, error handling necessitate persevering option based on the use cause, for example, pass the test only for & # 8220;buttondoestexist& # 8221; erroneousness when the button to be chatter perform not exist.

You can qualify the code to handle the exception for a specific scenario in such cases.

describe ('Exception Handling In Cypress ', () = & gt; {it ('Navigate to webpage ', () = & gt; {Cypress.on ('fail ', (erroneousness, runnable) = & gt; {if (! error.message.includes ('buttondoestexist ')) {throw fault}}) cy.visit ('https: //www.google.in '); cy.get (' # buttondoestexist ')})})

If you execute the test, it will be marked as a walk though there is an exclusion.

Handling Exception due to Unexpected Status Code in Cypress

Cypress is designed so that if the web page return any state code other than 200, it will throw an exception. Consider Scenario, you wanted to examine the status code of some website other than 200 (Negative scenarios). In this case, you need to handle the exception to avoid unwanted test failures.

Cypress provides the choicefailOnStatusCode: false, where you involve to pass this option tocy.visit () command.

Example: Webpage shed 400 Bad petition.

describe ('Exception Handling In Cypress ', () = & gt; {it ('Navigate to webpage ', () = & gt; {cy.visit ('https: //somewebsitethrows400.com/r/files ')})})

Cypress code to care condition code exception:

describe ('Exception Handling In Cypress ', () = & gt; {it ('Navigate to webpage ', () = & gt; {cy.visit ('https: //somewebsitethrows400.com/r/files ', {failOnStatusCode: false})})})

Talk to an Expert

Conclusion

Errors are common in web application, and many arise due to browser compatibility. Since webpages render differently across several browser and browser versions, it & # 8217; s crucial to test compatibility across multiple function system. These compatibility errors are typically identified during cross-browser testing.

Setting up the necessary infrastructure for cross-platform testing can be time-consuming and expensive. However, with a real device cloud like BrowserStack, quizzer gain access to over 3500+ existent device and browsers, ensuring comprehensive test coverage under real user conditions.

This approach simplify the testing process, reduces price, and warranty that applications perform reliably across different environment.

Tags
59,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free