2011/03/03

Firefox WebDriver on Windows Behind HTTP Proxy

Playing with Selenium (2.0a7) I was quite suprised that FirefoxDriver does not recognize HTTP proxy. As I wanted my code to work behind firewall I was pushed to do some "research" on the topic and found working solution using Proxy and FirefoxProfile . I hope publishing it can help somebody with the same problem:
 
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

...

Proxy proxy = new Proxy();
proxy.setAutodetect(true);
//hard-coded: proxy.setProxyAutoconfigUrl("http://pac.your.com");
FirefoxProfile profile = new FirefoxProfile();
profile.setProxyPreferences(proxy);
WebDriver driver =  new FirefoxDriver (profile);
 

4 comments:

  1. can you explain it in detail. I am a selenium newbie. facing Firefox and chrome proxy problem..

    ReplyDelete
    Replies
    1. Unfortunately you did no write what details are unclear or missing. So short explanation of the code - it opens Firefox with a profile that has proxy autodetection enabled. That's usually/often enough and has the advantage you do not have any hardcoded URL in your code. The line in comment is an alternative for the case when PAC script (see http://en.wikipedia.org/wiki/Proxy_auto-config) is used in your domain. Look at javadoc of the Proxy class for further options.

      Delete
  2. Hi,
    do you know how to set a proxy connection that
    requires username and password? (in selenium 2 with java)
    Thanks.

    ReplyDelete
  3. For Firefox following steps could work:

    1/ create separate testing profile by running
    "firefox -ProfileManage -no-remote"

    2/ install an extension the will do the authentication for you
    AutoAuth for basic authentication, Integrated Authentication for NTLM

    3/ locate the directory of the profile

    4/ create Firefox driver fot his profile:
    File profileDir = new File(profilePath);
    FirefoxProfile profile = new FirefoxProfile(profileDir);
    new FirefoxDriver(profile);

    ReplyDelete