Examples

This section lists examples from the testcafe-examples repository. All examples are ready to run.

See also: Recipes

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.
Change Element’s Style Modify an element’s style from test code.
Check If an Image Has Loaded Check whether an image on the page has loaded.
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.
Iterate Over Table Rows Verify cell content for even and odd rows.
Multi-User Tests Run multi-user tests.
Page Manipulation Execute custom code on the page to obtain data or manipulate the page state (for instance, to reload the page).
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.
Simulate the Web Page Losing Focus Simulate the web page losing focus.
Submit a Form Submit a form with DOM API.
Test <select> Elements Open a <select> element’s drop-down list, click an item, and verify the value.

Test Organization

Example Description
Create Data-Driven Tests Create a test that performs parameterized actions.
Extract Code to Helpers Extract test code to functions defined in a separate module.
Pass Parameters to Tests Pass parameters to TestCafe tests.
Use Page Model Extract element selectors and common operations with these elements to a page model.
Filter Tests by Metadata Skip tests that include certain metadata.

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
Extended Client-Side Error Tracking Filter client-side errors that occur during a test.
Import Third-Party Modules Import a third-party Node.js module into test code.
Inject Custom Client Scripts Inject Node.js modules into the tested webpage.

Browser API Mocks

Example Description
Mock Camera/Microphone Access Use mock data for the microphone and camera.
Mock Date Substitutes current data with mockdate.
Mock Geolocation API Substitutes the current location with a mock location.

Important

TestCafe v3.1.0 and up can respond to geolocation prompts with a Native Dialog Handler.

Wait Mechanisms

Example Description
Wait For File Download Specify that the test should wait for the file to download.
Wait Until an Element Property Has a Specific Value Use an assertion to pause the test until an element’s width is 100%.