ClientFunction Constructor
Creates a client function.
ClientFunction(fn [, options]) → ClientFunction
Parameter | Type | Description |
---|---|---|
fn |
Function | A function to be executed on the client side. |
options (optional) |
Object | See Options. |
Note
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.
Dependencies passed to ClientFunction
must be Selectors, ClientFunctions or a serializable object.
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 { ClientFunction, Selector } from 'testcafe';
const articleHeader = Selector('header>h1');
const getArticleHeaderHTML = ClientFunction(() => articleHeader().innerHTML, {
dependencies: { articleHeader },
});
Note
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.
When dependencies are passed to a client function, TypeScript cannot find them during compilation. This happens because dependencies are added to the function’s scope at runtime and can cause an error:
Error: TypeScript compilation failed.
Cannot find name 'dependencyFoo'.
Add the // @ts-ignore
TypeScript comment to suppress this 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 Call Client Functions from Node.js Callbacks.