Programming Interface

This section describes the API that allows you to run TestCafe tests from your Node.js modules.

Begin with creating the TestCafe server instance using the createTestCafe factory function.

const createTestCafe = require('testcafe');

createTestCafe('localhost', 1337, 1338)
    .then(testcafe => {
        /* ... */
    });

Use this instance to create other entities required to execute tests: remote browser connections and the test runner.

const createTestCafe = require('testcafe');
let runner           = null;

createTestCafe('localhost', 1337, 1338)
    .then(testcafe => {
        runner = testcafe.createRunner();

        return testcafe.createBrowserConnection();
    })
    .then(remoteConnection => {
         /* ... */
    });

Remote browser connections allow you to run tests on remote devices.

The test runner configures and launches test tasks.

The following example shows how to run tests from the myFixture.js fixture in two browsers: Google Chrome installed locally and another browser (that can be any of the supported browsers) on a remote device. The test run report will be output in the JSON format.

runner
    .src('tests/myFixture.js')
    .browsers([remoteConnection, 'chrome'])
    .reporter('json')
    .run()
    .then(failedCount => {
        /* ... */
    })
    .catch(error => {
        /* ... */
    });

For details, see the reference topics below.


Reference