createTestCafe Function

Creates a TestCafe server instance.

async createTestCafe(options) → Promise<TestCafe>
ParameterTypeDescription

options (optional)

options (optional)

See options.

Example

Create a TestCafe instance with the createTestCafe function.

(async () => {
    const createTestCafe = require('testcafe');

    const options = {
        hostname: 'localhost',
        port1:    1337,
        port2:    1338,
    };

    const testcafe = await createTestCafe(options);

    await testcafe
        .createRunner()
        .src('test.js')
        .browsers('chrome')
        .run();

    await testcafe.close();
})();

Establish an HTTPS connection with the TestCafe server. The openssl-self-signed-certificate module is used to generate a self-signed certificate for development use.

(async () => {
    const createTestCafe        = require('testcafe');
    const selfSignedSertificate = require('openssl-self-signed-certificate');

    const sslOptions = {
        key:  selfSignedSertificate.key,
        cert: selfSignedSertificate.cert,
    };

    const testCafeOptions = {
        hostname: 'localhost',
        port1:    1337,
        port2:    1338,
        sslOptions,
    };

    const testcafe = await createTestCafe(testCafeOptions);

    await testcafe.createRunner()
        .src('test.js')

        // Browsers restrict self-signed certificate usage unless you
        // explicitly set a flag specific to each browser.
        // For Chrome, this is '--allow-insecure-localhost'.
        .browsers('chrome --allow-insecure-localhost')
        .run();

    await testcafe.close();
})();

options

Type: Object

ParameterTypeDescriptionDefault

hostname (optional)

String

The hostname or IP on which the TestCafe server runs. Must resolve to the current machine. To test on external devices, use the hostname that is visible in the network shared with these devices.

Hostname of the OS. If the hostname does not resolve to the current machine - its network IP address.

port1, port2 (optional)

Number

Ports that will be used to serve tested webpages.

Free ports selected automatically.

sslOptions (optional)

Object

Options that allow you to establish an HTTPS connection between the TestCafe server and the client browser. This object should contain options required to initialize a Node.js HTTPS server. The most commonly used SSL options are described in the TLS topic in the Node.js documentation. See Test HTTPS and HTTP/2 Websites for more information.

developmentMode (optional)

Boolean

Enables/disables mechanisms to log and diagnose errors. You should enable this option before you contact TestCafe Support to report an issue.

false

cache (optional)

Boolean

If enabled, the TestCafe proxy caches webpage assets (such as stylesheets, images and scripts) for the webpages that it processes. The next time the proxy accesses the page, it loads assets from its cache instead of requesting them from the server.

false

retryTestPages (optional)

Boolean

Retry failed network requests for webpages visited during tests. Requires a secure connection.

false

configFile (optional)

String

Path to the Testcafe configuration file.

null

disableHttp2 (optional)

Boolean

Disables support for HTTP/2 requests.

false

Related configuration file properties:

See Also