Unable to execute javascript in the page

Hello again,

I get the following error when I try to execute (evaluate) some javascript on the page:

  • ERRO[0011] frame cannot evaluate: cannot call function on expression (“_W.GoToPageId(‘0’);\n//# sourceURL=xk6_browser_evaluation_script\n”) in execution context (3) in frame (180F033EE166BBC7A138206EB07BF009): %!w()

the code I use to evaluate is:


I guess I am doing it wrong but cannot find examples to put me on the right path.
Any hints on how to run javascript on the opened page?


Hi there,

that’s an unhelpful error message, which we recently improved in the upcoming v0.3.0. My guess would be that _W is undefined on the page, so the method call fails.

You can confirm this with your current xk6-browser version with:

page.evaluate(() => console.log(typeof _W));

If that logs "undefined", then that’s the problem you should look into.

Like I said, we improved this recently in v0.3.0, which will be released this next Monday, May 9th. In this update you should see a more informative error message. For example:

ERRO[0000] error calling evaluate: frame cannot evaluate: cannot call function on expression ("() => {\n    console.log(typeof _W.GoToPageId);\n  }\n//# sourceURL=__xk6_browser_evaluation_script__\n") in execution context (1) in frame (401F0B8510E25C382A502F3ACE5C97B5) with session (78DC0399C0B2E966852F94AFE1BCF8ED): exception "Uncaught" (1:23): ReferenceError: _W is not defined

Notice the ReferenceError: _W is not defined.

We’ll work on cleaning up the error message itself to make it less noisy.

If you don’t want to wait for the official release, you can build your own xk6-browser binary right now with:

xk6 build --output xk6-browser --with github.com/grafana/xk6-browser@main

Also note that like in the example above, you can pass an actual function instead of a string, which would be easier to work with. In that case, you should pass arguments to the function itself, since values defined in the k6 scope aren’t available to the browser. So this would work as well:

page.evaluate((pid) => _W.GoToPageId(pid), pageId);

There’s a small example using evaluate() here.

We’re currently working on updating the documentation, so this should be clearer soon.

1 Like

It works now. Thank you.