grafana / xk6-browser

The browser module adds support for browser automation and end-to-end web testing via the Chrome Devtools Protocol to k6.
https://grafana.com/docs/k6/latest/javascript-api/k6-browser/
GNU Affero General Public License v3.0
342 stars 42 forks source link

k6 hosts option doesn't work on an already running browser instance #811

Open ankur22 opened 1 year ago

ankur22 commented 1 year ago

Brief summary

Try to run examples/hosts.js and it should not be able to navigate to test.k6.io since the hosts option has been used so that test.k6.io resolves to 127.0.0.254. This is the expected behaviour when working with this example file.

If we change the script so that it connects to an already browser instance, then the browser will resolve to the actual test.k6.io ip address, and not to 127.0.0.254, which is the incorrect behaviour for this example file.

The reason the behaviour is incorrect when testing on an already running browser instance, is because the k6 hosts option in k6-browser uses Chromium's --host-resolver-rules in the background to resolve hosts to a given IP. If the browser is already running then how can we inject this rule?

xk6-browser version

48ee41a59e0f0561dbcae39b91dc6fba338422a4

OS

NA

Chrome version

Version 110.0.5481.177 (Official Build) (arm64)

Docker version and image (if applicable)

NA

Steps to reproduce the problem

  1. Start a browser instance.
  2. Change the examples/hosts.js file to connect to the running browser instance from the previous step.
  3. Run k6 with the modified examples/hosts.js file.

Expected behaviour

The test should pass since test.k6.io resolves to 127.0.0.254.

Actual behaviour

The test fails since it resolves to the actual IP address of test.k6.io.

ka3de commented 1 year ago

Researching about how to block a blacklist through CDP, I found the Fetch domain, which probably would allows us to implement the "hosts" functionality with an already launched browser, although the development "cost" is higher than just specifying a flag on startup.

ankur22 commented 1 year ago

Researching about how to block a blacklist through CDP

That's a good find, could be useful in the future!

I found the Fetch domain, which probably would allows us to implement the "hosts" functionality with an already launched browser

We use fetch already so we should be able to fit that in quite easily.