follow me icons

Sunday, September 26, 2010

Firefox Proxy configuration using Selenium Webdriver and Capybara



Quite often we have to run the automation test within the proxy setting. The default browser profile setup that Capybara use is the anonymous profile which does not use proxy setting by default. This will cause problem as the browser will not be able to access the webpage.

To see all of the available configuration in Firefox simply type "about:config" on the firefox address bar. You will then see the following screen:


One solution is to set the browser profile to use a proxy setting. The code can be placed inside the env.rb files to override the anonymous browser profile that Capybara uses.


# Need to override the driver to setup our proxy profile
class Capybara::Driver::Selenium
def self.driver
unless @driver
profile = Selenium::WebDriver::Firefox::Profile.new
profile["network.proxy.type"] = 1
profile["network.proxy.http"] = "proxy setting"
profile["network.proxy.http_port"] = 8080
@driver = Selenium::WebDriver.for(:firefox, :profile => profile)

at_exit do
@driver.quit
end
end
@driver
end
end


If you need to use the Automatic proxy configuration (pac file) URL then you would need to add the following line:

profile["network.proxy.autoconfig_url"]="proxy.pac"


And change the network.proxy.type into 2

profile["network.proxy.type"] = 1



If you need Capybara to use a different browser other than Firefox you can change the webdriver browser's profile. To change the default profile for other browsers you just need to change the @driver to use the browser that you want.

@driver = Selenium::WebDriver.for :ie
or
@driver = Selenium::WebDriver.for :chrome


Please note that webdriver currently only supports Firefox, IE and chrome.


4 comments:

  1. As of Capybara 0.4.0, you would need to use the following code

    Capybara.register_driver :selenium do |app|
    profile = Selenium::WebDriver::Firefox::Profile.new
    profile["network.proxy.type"] = 1
    profile["network.proxy.http"] = "proxy setting"
    profile["network.proxy.http_port"] = 8080
    Capybara::Driver::Selenium.new(app, :browser => :firefox, :profile => profile)
    end

    ReplyDelete
  2. If you get this error
    uninitialized constant Selenium (NameError)
    ./features/support/env.rb:19:in `block in '

    Try adding require "selenium-webdriver" on the env.rb file

    ReplyDelete
  3. Hi I tried this way and it helped me set manual network setting in fire fox .

    address : "x.x.x.x:8080" and Import respective packages

    enter code here



    public class SeleniumUtil

    WebDriver driver;

    SeleniumUtil()

    String PROXY = "192.1.1.1:8080";

    org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
    ProxysetSslProxy=proxy.setHttpProxy(PROXY).setFtpProxy(PROXY).setSslProxy(PROXY);

    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability(CapabilityType.PROXY, proxy);
    driver = new FirefoxDriver(cap);

    @BeforeSuite(alwaysRun = true)
    public void setupBeforeSuite()

    ReplyDelete
  4. Hi BA,

    profile = Selenium::WebDriver::IE::Profile.new

    The above statement does not work with Internet explorer so it says and suggestion?
    uninitialized constant Selenium::WebDriver::IE (NameError)

    ReplyDelete