Selenium RC and Proxy Chaining (Double Proxies)
We had a proxy problem with Selenium RC today. The site-under-test is behind a proxy, and has the same domain name as the production site (let’s call it shop.mystore.com). Normally, developers set their proxy server in their browser, which then causes requests for shop.mystore.com to go to the proxy server which then sends the request to the developer site-under-test, instead of going to the production site.
We want to set up Selenium so the developers can created automated tests around visiting the site-under-test.
Selenium does have the ability to do a “double proxy”, using system properties (proxyPort elided for length):
java -Dhttp.proxyHost=proxy.server.com -Dhttp.proxyPort=3128 -jar selenium-server.jar
But it turns out, unfortunately, that this doesn’t seem to work in practice. After some investigation of my own, I discovered that the proxy configuration file (proxy.pac) in Selenium is not generated properly. My own tests, and a timely visit to this discussion of the proxy chaining issue, it appears that there’s a simple workaround:
java -Dhttp.proxyHost=proxy.server.com ... -jar selenium-server.jar -avoidProxy
Setting -avoidProxy causes the proxy configuration (proxy.pac) to work properly, sending the selenium requests to the developer site-under-test.
*Update*
I have discovered there are additional steps that need to occur in some situations, especially when you want to use Chrome and/or SSL. Hopefully this will be fixed in the 1.0 release of Selenium. Here are the additional steps for using SSL:
- Use the chrome browser
- Start the Selenium Server programatically, instead of at the command line. Yes, that means write code that calls the Selenium server and initializes the proxies, etc before you call the .start() method.
- BUT - There’s a bug in 0.92 (and possibly later) that means you can’t set the proxyHost and proxyPort programatically ( ! )
Here’s what you can do. I realize this is complicated.
- When you start up firefox or chrome via Selenium, it uses a special temporary profile directory to manage the proxy files and so forth.
- Start up Selenium from the command line, with the proxyHost and proxyPort values set. Then find this profile directory. Copy it to some other safe, easy-to-remember location. We’ll call it “/var/tmp/SeleniumProxyHack“
- Go back to your programmatic launcher for Selenium, and tell it to use /var/tmp/SeleniumProxyHack as the Firefox Template directory.
- From my possibly faulty human recollection - you also need to modify another file in that directory, to tell it to look at the proxy.pac file in /var/tmp/SeleniumProxyHack, instead of wherever it looked before.
Here’s a snippet of code that I use to programatically start Selenium
// these don't work in *chrome in Selenium 0.9.2
System.setProperty("http.proxyHost", "proxy.dev.server.com");
System.setProperty("http.proxyPort", "3128");
seleniumServer = new SeleniumServer(portToUse);
seleniumServer.setProxyInjectionMode(false);
seleniumServer.setAvoidProxy(true);
seleniumServer.setReusingBrowserSessions(true);
String firefoxTemplateLocaton = "/var/tmp/seleniumProxyHack";
SeleniumServer.setFirefoxProfileTemplate(new File(firefoxTemplateLocation));
seleniumServer.start();
Yes, I wish there were an easier way as well. And no, I can’t guarantee this will work. But it did work properly for me in a fairly hairy case involving SSL and a fairly well-secured system.

June 26th, 2008 at 9:03 pm
This doesn’t work for me. Even with the -avoidProxy it passes the selenium request through the external proxy, get’s a 404, and just hangs. That’s assuming you’re using iexplore or firefox, the chrome doesn’t even honor the proxy call. I was excited to use this tool, but it’s turning out to be a, how do you say, real piece of s***.