grafana / xk6-browser

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

Unable to set up proxy #449

Open asteriosgr opened 2 years ago

asteriosgr commented 2 years ago

As the title suggests I am unable to initiate the xk6-browser using a proxy. I am aware that the README is a bit wrong on the way it defines the proxy, because proxy support through variables is still on TODO.

https://github.com/grafana/xk6-browser/blob/main/common/connection.go#L146

So currently the proxy value is in theory taken from the environment variables.

However when exporting them, a proxy is still not used. For example:

export http_proxy='http://proxy.net:43' export https_proxy='http://proxy.net:43' export HTTP_PROXY='http://proxy.net:43' export HTTPS_PROXY='http://proxy.net:43'

Is there any workaround for this issue?

inancgumus commented 2 years ago

Hi @asteriosgr,

This is for connecting to the Browser's WebSocket DevTools URL. Even if we set it, the browser wouldn't use it.

To fix this issue, I need some information from you. Could you tell me your motivation for using a proxy in this case? Is it about passing proxy information to the launched browser and letting the browser use the proxy?

asteriosgr commented 2 years ago

Hi @inancgumus . I am trying to test a website using playwright. The website is accessible only through a specific proxy. With plain playwright, I am able to set up a proxy in the following way: proxy: { server: PROXY},

However, with the xk6 browser, I tried setting proxy also as a chrome argument (flag), and also through the way it is mentioned in the README https://github.com/grafana/xk6-browser#launch-options . In both cases I was unable to establish connection to the PROXY. Then going through the code I noticed that you should try exporting it as an env variable, https://pkg.go.dev/net/http#ProxyFromEnvironment . Once again this did not do the trick.

So, TLDR, I simply need to access a website through a PROXY. However, I am unable to set up a proxy.

inancgumus commented 2 years ago

@asteriosgr, I can let you pass the proxy options to the browser if that works for you (since we're also missing and not passing these options to the browser yet). Could you check whether you can access the website by launching chrome with these command-line arguments:

<path-to-your-chrome-executable> --proxy-server="<your-proxy-url-here>"

For example (chromium is my $PATH environment variable):

chromium --proxy-server="https://myproxy:myproxyport"

Please launch Chrome with the proxy server argument and then head to your website.

asteriosgr commented 2 years ago

Well as I mentioned earlier, I am starting chrome with the following args: const browser = launcher.launch(‘chromium’, { headless: false, args: [--proxy-server=“http://myproxy:port”,--no-sandbox, ‘--ignore-certificate-errors’, ‘--incognito’], proxy: { server: myproxy:myport}});

Where myproxy and myport of course is the proxy value and the port number I am using.

Still using the chrome proxy argument, is not working.

Edit: A quick workaround is to set up a proxy at a system (OS) level. It works that way. However, this is not ideal for apparent reasons, so I would like to avoid that if possible.

inancgumus commented 2 years ago

@asteriosgr, sorry, I couldn't explain what I meant. My previous message was that instead of running via xk6-browser or Playwright, starting Chrome directly from the command line with the proxy arguments, going to the website behind a proxy, and telling me if it works that way. Then we can fix the args option and let you test the website in your script too.

asteriosgr commented 2 years ago

On my end, setting the --proxy-server argument, is not doing anything in the local chrome browser.