How to handle Errors in Cypress
On This Page Understanding Error Handling in CypressMarch 25, 2026 · 6 min read · Tool Comparison
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. 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: 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. 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: Here is a detailed breakdown of each type below. Consider a scenario where you are pilot to one of the web pages, which is throwing exceptions. For example, navigatinghttps: //wxyz.in throws This has nothing to do with your tryout, but still, the test would fail due to the lead webpage throwing fault. Read More: 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 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. 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: 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: 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.” To handle the above exception, you need to use the following Cypress command Modify the above code to handle the exception as seen below 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. If you execute the test, it will be marked as a walk though there is an exclusion. 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. Cypress code to care condition code exception: 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. # Ask-and-Contributeabout this topic with our Discord community. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.How to handle Errors in Cypress
Understanding Error Handling in Cypress
How to perform Cypress Error Handling?
Handling Exception from Webpage Under Test in Cypress
Uncaught SyntaxError: Unexpected token ' & lt; '
Handling Exceptions within the Code in Cypress
cy.on ('uncaught: exclusion ', (err, runnable) = & gt; {})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
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 ');})})Handling Failure Exception in Cypress
describe ('Exception Handling In Cypress ', () = & gt; {it ('Navigate to webpage ', () = & gt; {cy.visit ('https: //www.google.in '); cy.get (' # buttondoestexist ')})})Cypress.on ('fail ', (error, runnable) = & gt; {}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 ')})})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 ')})})Handling Exception due to Unexpected Status Code in Cypress
describe ('Exception Handling In Cypress ', () = & gt; {it ('Navigate to webpage ', () = & gt; {cy.visit ('https: //somewebsitethrows400.com/r/files ')})})describe ('Exception Handling In Cypress ', () = & gt; {it ('Navigate to webpage ', () = & gt; {cy.visit ('https: //somewebsitethrows400.com/r/files ', {failOnStatusCode: false})})})Conclusion
Related Guides
Automate This With SUSA
Test Your App Autonomously