How to execute javascript of a target web page

Hi there,
When a website or a web page is requested by k6, it shows only http request. Is it possible to execute the javascript of that page too?

For exampel, I am doing stress testing of http://localhost/example.html
but in the example.html there is a javascript code, so is possible to execute that?

Thanks!
Best Regards!
Naser

Hi @hafiznaseraslam92,

Welcome to the forum.

Using xk6-browser, we launch a Chromium browser and it loads the page and execute all the Javascripts in it (as if you’re visiting the page yourself by using your browser). If you want to execute a piece of Javascript code on a page, you can use page.eval (documented here). Here’s an example.

Thanks!

Hi @inanc ,
Thank you so much for the help. I downloaded the git repo and went inside it and run the evaluate.js by following command but it is showing following error.
k6 run evaluate.js

ERRO[0000] GoError: unknown module: k6/x/browser
at go.k6.io/k6/js.(*InitContext).Require-fm (native)
at file:///C:/Users/Naser/Downloads/xk6-browser-main/xk6-browser-main/examples/evaluate.js:2:0(21)
at native hint=“script exception”

The screenshot is also attached below.

Please help me to fix this issue. Thank you so much in advance!

Hi @hafiznaseraslam92,

Sure thing! It seems xk6-browser hasn’t been correctly configured on your system. You can find the installation steps in this document. Let me know how it goes.

Thanks.