Looks like I'm not the only one who needs to chain the Selenium RC proxy to another HTTP proxy. The docs are misleading, here's how to do it.
Selenium RC uses a built-in proxy server so that it can inject itself into the domain of the site being tested. Sometimes however, you need it to go through another proxy server.
The options documentation says you can supply http.proxyHost and http.proxyPort Java system properties to do this, which implies that requests that selenium makes will be proxied through that.
However, that doesn't actually happen for the domain under test; Selenium RC's proxy passes those straight onto the domain's server, without using the proxy.
(When supplying those options, the proxy.pac file that Selenium RC puts out looks like this:
function FindProxyForURL(url, host) {
return 'PROXY localhost:4444; PROXY localhost:8090';
}But that does not create a proxy chain – it just creates a failover chain, where the browser will use the Selenium RC proxy and only if that doesn't work will it use your proxy.)
For most cases though, there is a simple solution. We don't actually need our requests to the domain under test to pass through Selenium RC – we only need the requests for the selenium wrapper stuff to go to it, so that they appear in domain.
So if we can make the requests to /selenium-server/ go to Selenium RC, and everything else go straight to the other proxy server, everyone's happy.
You don't need to make your own proxy PAC files and explicitly start browsers, as there is a -avoidProxy Selenium RC option to do it.
For example:
java -Dhttp.proxyHost=localhost -Dhttp.proxyPort=8090 \
-jar selenium-server.jar -avoidProxy
Which produces a proxy.pac file like this:
function FindProxyForURL(url, host) {
if(shExpMatch(url, '*/selenium-server/*')) {
return 'PROXY localhost:4444; PROXY localhost:8090';
} else if (shExpMatch(host, 'local')) {
return 'DIRECT';
} else if (shExpMatch(host, '*.local')) {
return 'DIRECT';
} else if (shExpMatch(host, '169.254/16')) {
return 'DIRECT';
} else if (shExpMatch(host, '*.169.254/16')) {
return 'DIRECT';
} else {
return 'PROXY localhost:8090';
}
}From this you can see that it will use our proxy on port 8090 as desired.
One catch you can also see, however, is that it will bypass the proxy for *.local sites – so don't use that as your hostname for the domain under test. Similarly, it bypasses the proxy for sites identified by IP address (rather than by name) and using just one (!?) of the private IP address ranges, so use named hosts instead.
Comments
Rather than using the command line(in cmd) option can't we set the proxy&port via java code(using selenium api) ?or set in proxy.pac via java code using selenium api ?
I have tried several methods for proxy chaining w/ Selenium (including yours) but have not had any success yet. I followed your suggestions but am still not seeing my test requests going through the proxy.
I started a personal web proxy (WebScarab) at localhost:8008 and started my server like this: java -jar selenium-server.jar -avoidProxy -Dhttp.proxyHost=localhost -Dhttp.proxyPort=8008 , however none of the requests go through the proxy.
My test is simply:
DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com");
selenium.start();
selenium.open("/");
selenium.type("q", "dog");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
assertTrue(selenium.isTextPresent("Image results for dog"));
selenium.stop();
Have you had any similar problems? or suggestions?
Praitheesh, sorry for not approving your comment sooner. I have no idea sorry, ask the Selenium RC guys if they could add that as a feature.
Benjamin, I'd suggest you pause everything once the browser is open, and check the proxy.pac file yourself to see what it says. It should be in your temporary files directory, but you should be able to see the path by running ps and looking at the browser command.
just letting you know I was having similar trouble with my set. Your suggestion worked like a charm. Now I can do all my personal testing while at work too.. Thanks
Hi Will
I am getting a strange problem. Proxy chaining works with IE but when i test with firefox, the selenium-server does not forward the request to my HTTP proxy, instead it goes directly to domain web server.
I have tried avoidProxy but it does not help. I am not able to find proxy.pac file. Will appreciate any help.
Hi Benjamin,
Try the following.It worked for me.
java -Dhttp.proxyHost=localhost -Dhttp.proxyPort=8008 -jar selenium-server.jar -avoidProxy
Hi Benjamin,
Pls try with -proxyInjectionMode option also.
java -Dhttp.proxyHost=localhost -Dhttp.proxyPort=8008 -jar selenium-server.jar -proxyInjectionMode -avoidProxy
hi vari ,
For Firefox,there is no need to provide proxy details.Provide the firefox profile of user.(I have added the firefox profile ,for windows XP)
java -jar selenium-server.jar -firefoxProfileTemplate "C:\Documents and Settings\<login username>\Application Data\Mozilla\Firefox\Profiles\jivq34n0.default"
I would like to add that if you are trying this out on Windows 7, it seems the proxy auto-detect settings are stored in C:\Users\<login>\AppData\Roaming\Mozilla\Firefox\Profiles\<random>.default. This tripped me up since at first I was using C:\Users\<login>\AppData\Local\<etc...>.
Otherwise using Rosary's method works perfectly!
Hi
As result of all methods mentioned above (SRC 1.03):
1 proxy settings and -avoidProxy with selenium server doesn't work for me
2 custom FF profile + FoxyProxy + .pac file worked
Thanks!