t.eval Method

Creates a client function and executes it immediately (without saving).

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

The following example shows how to get a document's URI with t.eval.

fixture `My fixture`
    .page `http://www.example.com/`;

test('My Test', async t => {
    const docURI = await t.eval(() => document.documentURI);
});

Since the eval method returns a value, and not an object, you cannot chain other methods of the test controller after 'eval'.

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 { 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.

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.