When a JavaScript error occurs on a tested web page, TestCafe stops test execution and posts an error message. Third-party code execution (for example, controls, logging, and analytics services) can cause unexpected errors that are not related to the tested app. You can filter these errors, so that they are not included in the test.
The following example demonstrates how to extend TestCafe's standard error tracking and how to filter client-side errors that occur during a test.
Test Code: index.js
The test page has two buttons (Error 1
and Error 2
). When clicked, these buttons throw an error with .level
and .url
custom properties that are used to filter errors and have the following values:
Button | .level | .url |
---|---|---|
Error 1 |
critical |
https://my-domain.example.com |
Error 2 |
warning |
https://foreign-domain.example.com |
During the test, both buttons are clicked. To capture the errors, the test.clientScripts method injects a client script into the page. This script collects all the errors that occur during the test and records them into an array.
Read up on how to include and use custom scripts in your tests: Inject Client Scripts
The array is then checked with the Array.some JavaScript method to find out whether any critical
errors have occurred on the tested web page. If such errors have occurred, then the test passes, otherwise, it fails.
To launch only this example, use the following command:
npm run extended-error-tracking
When a JavaScript error occurs on a tested web page, TestCafe stops test execution. To prevent this, the
-e
CLI option is used in this example.
- Test Structure:
- Element Identification and Actions:
- Assertion and Evaluation:
- Custom Scripts:
- Command Line Interface Options: