t.eval Method
Creates a client function and executes it immediately (without saving). Returns a promise-wrapped value and cannot be chained with other methods of the test controller.
t.eval(fn [, options]) → Promise<any>
| 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);
});
Always place
evalin a separate call.
evalreturns a promise-wrapped value instead of a local context so you cannot chain other methods of the test controller aftereval. If you chainevalon to other methods of the test controller, their promises don't ever resolve which breaks the execution order.
fixture `My fixture`
.page `https://devexpress.github.io/testcafe/example/`
const timeout = 1000;
test ('My Test', async t => {
await t
.wait(timeout)
.eval(() => location.reload(true));
// The timeout is skipped and the action executes right away.
// Do not chain eval on to other methods of the test controller
});
test ('My Test', async t => {
await t.wait(timeout);
await t.eval(() => location.reload(true));
// Passes after a timeout
});
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.