Skip to content

Corporate Proxies

Owen edited this page Feb 8, 2018 · 4 revisions

Setting up for accessing files on via Corporate Proxy

It has been reported that you may run into issues within a virtual private cloud sending http requests to/from your docker containers.

You may be in a situation where you need to access an externally hosted SPA. (On AWS S3 perhaps). You may run into trouble trying to send http requests from your Corporate VPC with the containers to through your router. (Particularly if it is a Microsoft router that requires a username and password in the URL - which Chrome doesn't support.)

The solution in this case was solved by using node-http-proxy - simply pass your http basic credentials to node-http-proxy on startup, then point Chrome to this proxy running locally.

Building the Docker images

If you need to configure environment variable in order to build the image (http proxy for instance), simply set an environment variable BUILD_ARGS that contains the additional variables to pass to the docker context (this will only work with docker >= 1.9). (Note that the proxy here refers to the proxy for building the Chrome image, not running Chrome - just to make this super-clear).

Setting a proxy for running Chrome

There are a couple of options here. You can add a build arg in Selenium, which will be added as a parameter to Chrome when it is executed. For example:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--proxy \"http=http://proxyserver:port/;https=http://proxyserver:port/\""));
WebDriver driver = new ChromeDriver(capabilities);

Note that this won't work if the proxy requires username and password auth. (This is a bug in Chrome, please star the issue here if it is a pain point for you. eg this won't work:

capabilities.setCapability("chrome.switches", Arrays.asList("--proxy \"http=http://username:password@proxyserver:port/;https=http://username:password@proxyserver:port/\""));

The solution in this circumstance is to use a proxy PAC file. The file looks like this (this is JavaScript Code):

if (host == "mylocalserver.com")
{
    return 'DIRECT';
} else {
   return return "PROXY wcg2.example.com:8080 ";
}

(But there are plenty of better proxy file examples online found with a simple Google Search.)

You can use it in Chrome by adding a runarg like this:

--proxy-pac-url=file:///proxy.pac

So in Selenium it looks like:

capabilities.setCapability("chrome.switches", Arrays.asList("--proxy-pac-url=file:///proxy.pac"));

To debug proxies - look at the resulting setting in Chrome with this URL:

chrome://net-internals/proxyservice#proxy