ClientFunction Constructor

Creates a client function.

ClientFunction(fn [, options]) → ClientFunction
Parameter Type Description
fn Function Client-side JavaScript function.
options (optional) Object See Options.

Warning

Client functions cannot return DOM nodes. Use selectors to inspect the DOM.

Do not use client functions to interact with the page or change the application’s front-end code. Use test actions instead.

Example

import { ClientFunction } from 'testcafe';

const getWindowLocation = ClientFunction(() => window.location);

Options

options.dependencies

Type: Object

Use the dependencies option to pass variables and helper functions to client functions.

The dependencies object may include the following items:

The clientFunction from the following example obtains a Selector query from a dependency:

import { ClientFunction, Selector } from 'testcafe';

const articleHeader = Selector('header>h1');

const getArticleHeaderHTML = ClientFunction(() => articleHeader().innerHTML, {
    dependencies: { articleHeader },
});

Note

Selector queries within client functions are not subject to the framework’s built-in waiting mechanisms. TestCafe executes them synchronously and does not engage any applicable timeouts.

TestCafe adds client function dependencies at runtime. TypeScript users may encounter the following compilation errror:

Error: TypeScript compilation failed.
Cannot find name 'dependencyFoo'.

Use the // @ts-ignore directive to suppress the error.

options.boundTestRun

Type: Object

Use the boundTestRun option to call a client function from a Node.js callback. To use this option, assign the current test controller to the boundTestRun option.

For details, see Trigger Client Functions from Node.js Callbacks.