evaluate<T> method

Future<T> evaluate<T>(
  1. @Language('js') String pageFunction, {
  2. List? args,
})

If the function passed to the executionContext.evaluate returns a Promise, then executionContext.evaluate would wait for the promise to resolve and return its value.

If the function passed to the executionContext.evaluate returns a non-Serializable value, then executionContext.evaluate resolves to null. DevTools Protocol also supports transferring some additional values that are not serializable by JSON: -0, NaN, Infinity, -Infinity, and bigint literals.

var executionContext = await page.mainFrame.executionContext;
var result = await executionContext.evaluate('() => Promise.resolve(8 * 7)');
print(result); // prints "56"

An expression can also be passed in instead of a function.

print(await executionContext.evaluate('1 + 2')); // prints "3"

Parameters:

  • pageFunction: Function to be evaluated in executionContext
  • args: Arguments to pass to pageFunction

Returns Future which resolves to the return value of pageFunction

Implementation

Future<T> evaluate<T>(@Language('js') String pageFunction,
    {List<dynamic>? args}) async {
  try {
    var result = await _evaluateInternal<T>(pageFunction,
        args: args, returnByValue: true);
    return result;
  } catch (error) {
    if (error is ServerException &&
        (error.message.contains('Object reference chain is too long') ||
            error.message.contains("Object couldn't be returned by value"))) {
      return null as T;
    } else {
      rethrow;
    }
  }
}