evaluate(javascriptFunction [,...args])

🚧

This is a TDK feature

This is not supported in Testim's visual editor

This method is used to run custom JavaScript in the browser application page. This is useful as an escape hatch and in order to implement interactions that are not available out of the box with Testim or in order to interact with the page JavaScript.

Note: this method does not support closures and both the function and the return value must be serializable:

  • javascriptFunction {function} a JavaScript function to evaluate
  • args {any[]} arguments to pass to the function, must be serializable
  • returns: Promise which fulfills with the element's return value (in JavaScript).
// ✅ get the page's html content
const allHtml = await evaluate(() => document.body.outerHTML);
expect(allHtml).to.equal('<html>...</html>'); 
// ✅ call a custom function
const result = await evaluate(() => window.someFunction());

// ❌ pass a function and not a string
await evaluate('window.someFunction()');
// ❌ do not use closures
const foo = '15';
await evaluate(() => window[foo]());

Full Example:

'use strict';

const expect = require('chai').expect;
const { go, evaluate, test } = require('testim');

test("evaluate", async () => {
    await go('http://jsbin.testim.io/quh/1');
    const ten = await evaluate((ron) => ron, 10);
    expect(ten).to.equal(10);
    const string = await evaluate((benji) => benji, "benji");
    expect(string).to.equal("benji");
    const boolean = await evaluate((elad) => elad, true);
    expect(boolean).to.equal(true);
    const object = await evaluate((obj) => obj, {x: 3});
    expect(object).to.deep.equal({x: 3});
});