ClientFunction Constructor

Creates a client function.

ClientFunction( fn [, options] )
Parameter Type Description
fn Function A function to be executed on the client side.
options (optional) Object See Options.

Client functions cannot return DOM nodes. Use selectors instead.

The following example shows how to create a client function.

import { ClientFunction } from 'testcafe';

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

Options

options.dependencies

Type: Object

The dependencies option contains functions, variables, or objects used by the client function internally. Properties of the dependencies object are added to the client function's scope as variables.

The following code sample demonstrates a client function (getArticleHeaderHTML) that calls a selector (articleHeader) internally. TestCafe passes this selector to getArticleHeaderHTML as a dependency.

import { Selector, ClientFunction } from 'testcafe';

const articleHeader = Selector('#article-header');

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

When a client function calls a selector internally, the selector does not wait for the element to appear in the DOM and is executed at once, like a client function.

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 Call Client Functions from Node.js Callbacks.