Test Structure

TestCafe test files contain fixtures and tests.

Note

If you use eslint, install the TestCafe plugin to avoid the 'fixture' is not defined and 'test' is not defined errors.

Fixtures

TestCafe test files begin with a fixture declaration. A fixture is a group of tests with the same starting URL. Every test belongs to a fixture.

It is best to use one fixture per test file. If your test suite contains tests with different starting URLs, place these tests into several test files - one for each starting URL. Alternatively, you can specify the starting URL on a test-by-test basis.

Declare a fixture

Declare a fixture with the fixture keyword.

fixture `Authentication tests`;

The fixture.page method defines the starting URL of the tests in the fixture.

fixture `MyFixture`
    .page `http://devexpress.github.io/testcafe/example`;

test('Test1', async t => {
    // Starts at http://devexpress.github.io/testcafe/example
});

Tests

Tests belong to fixtures. The code for tests that consitute a particular fixture follow that fixture’s declaration.

Declare a Test

Declare a test with the test keyword. Pass two arguments to the test function - a string with the test’s name and an asynchronous function with test content.

fixture `MyFixture`;

test('Test1', async t => {
    /* Test 1 Code */
});

test('Test2', async t => {
    /* Test 2 Code */
});

The asynchronous function with the test content needs to receive the test controller t object as an argument. The test controller object provides access to the TestCafe test API.

Specify Test Content

Use TestCafe actions to interact with the page. Use element selectors and client functions to retrieve client-side information. Use assertions to evaluate page data and determine if a test is successful.

The internal structure of TestCafe tests is up to the individual user. TestCafe tests may include any code, as long as it is valid. TestCafe tests can reference third-party libraries and communicate with other APIs.

Specify a Custom Starting URL

Use the test.page function to override the starting URL from the fixture definition.

fixture `MyFixture`
    .page `http://devexpress.github.io/testcafe/example`;

test
    .page `http://devexpress.github.io/testcafe/blog/`
    ('My test', async t => {
        // Starts at http://devexpress.github.io/testcafe/blog/
    });

Hooks

Note

Test hooks and request hooks are different entitites. Read the Request Interception Guide for more information on request hooks.

Hooks are reusable action sequences that run before or after tests or fixtures. They often set up the testing environment (e.g., authenticate the user) or restore the testing environment after the end of a test (e.g., remove a database object). Read the Hooks guide to learn more.