Examples
This section lists examples from the testcafe-examples repository. All examples are ready to run.
Locating Elements #
| Example | Description |
|---|---|
| Find Element By Trimmed Text | Ignores white-space characters when searching for an element by text. |
| Use XPath Selectors | Find an input with an XPath selector. |
Testing the DOM #
| Example | Description |
|---|---|
| Access Element Properties | Access DOM element properties and verify them with assertions. |
| Iterate Over Table Rows | Verify cell content for even and odd rows. |
| Test <select> Elements | Open a <select> element's drop-down list, click an item, and verify the value. |
| Page Manipulation | Execute custom code on the page to obtain data or manipulate the page state (for instance, to reload the page). |
| Change Element's Style | Modify an element's style from test code. |
| Check Whether Web Page Is Opened With TestCafe | Use code to check whether the application is opened with TestCafe |
| Iterate Over a List of Elements | Iterate over a list of elements and use their values in assertions. |
| Scroll Elements Into View | Scroll the page manually during tests. |
| Select a Table Row by Cell Content | Select a table row based on the content of its cells. |
| Submit a Form | Submit a form with DOM API. |
| Check If an Image Has Loaded | Check whether an image on the page has loaded. |
| Simulate the Web Page Losing Focus | Simulate the web page losing focus. |
Test Organization #
| Example | Description |
|---|---|
| Extract Code to Helpers | Extract test code to functions defined in a separate module. |
| Create Data-Driven Tests | Create a test that performs parameterized actions. |
| Use Page Model | Extract element selectors and common operations with these elements to a page model. |
| Pass Parameters to Tests | Pass parameters to TestCafe tests. |
Upload/Download #
| Example | Description |
|---|---|
| Check the Downloaded File Name and Content | Check the name and the content of a file downloaded during a test. |
| Select Files To Upload | Add files to an <input type="file">. |
Control HTTP Requests #
| Example | Description |
|---|---|
| Set a Custom Referrer | Set a custom referrer for the specified requests during tests. |
Interoperability #
| Example | Description |
|---|---|
| Inject Custom Client Scripts | Inject Node.js modules into the tested webpage. |
| Import Third-Party Modules | Import a third-party Node.js module into test code. |
| Extended Client-Side Error Tracking | Filter client-side errors that occur during a test. |
Browser API Mocks #
| Example | Description |
|---|---|
| Mock Date | Substitutes current data with mockdate. |
| Mock Geolocation API | Substitutes the current location with a mock location. |
| Mock Camera/Microphone Access | Use mock data for the microphone and camera. |
Wait Mechanisms #
| Example | Description |
|---|---|
| Wait Until an Element Property Has a Specific Value | Use an assertion to pause the test until an element's width is 100%. |
| Wait For File Download | Specify that the test should wait for the file to download. |