Firefox Driver – Using Proxy

Sometimes we have to use Proxy in our Selenium tests, typical example – if we are behind Corporate Proxy.

I stumble across this situation, so here is the fix.

Snippet:

FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("network.proxy.type", 2);
firefoxProfile.setPreference("network.proxy.autoconfig_url", "http://www.example.com/example.dat");
firefoxProfile.setPreference("network.proxy.no_proxies_on", "localhost");
WebDriver driver = new FirefoxDriver(firefoxProfile);

List of possibilities (integer values) for the “network.proxy.type”

0 – Direct connection (or) no proxy.

1 – Manual proxy configuration

2 – Proxy auto-configuration (PAC).

4 – Auto-detect proxy settings.

5 – Use system proxy settings.

Other way :


import static java.lang.String.format;

import java.net.URL;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class DriverConfig {

// Change these for your values.
private static String proxyHost = "";
private static int proxyPort = 0;
private static String ignoreProxy = "localhost, 127.0.0.1";
private static String seleniumGridHubHost = "localhost";

/**
* Firefox preference value for specifying Manual Proxy configuration.
*/
private static final int MANUAL_PROXY_CONFIG = 1;

public static WebDriver localFirefox() {

FirefoxProfile profile = createProfile();

return new FirefoxDriver(profile);
}

private static FirefoxProfile createProfile() {
FirefoxProfile profile = new FirefoxProfile();

// Use native events on all platforms
profile.setEnableNativeEvents(true);

boolean useProxy = !proxyHost.isEmpty();

if (useProxy) {
// Configures the same proxy for all variants
profile.setPreference("network.proxy.type", MANUAL_PROXY_CONFIG);
profile.setPreference("network.proxy.http", proxyHost);
profile.setPreference("network.proxy.http_port", proxyPort);
profile.setPreference("network.proxy.ssl", proxyHost);
profile.setPreference("network.proxy.ssl_port", proxyPort);
profile.setPreference("network.proxy.socks", proxyHost);
profile.setPreference("network.proxy.socks_port", proxyPort);
profile.setPreference("network.proxy.ftp", proxyHost);
profile.setPreference("network.proxy.ftp_port", proxyPort);
profile.setPreference("network.proxy.no_proxies_on", ignoreProxy);
}
return profile;
}

public static WebDriver remoteFirefox() throws Exception {
DesiredCapabilities capabilities = DesiredCapabilities.firefox();

capabilities.setCapability(FirefoxDriver.PROFILE, createProfile());

URL remoteAddress = new URL(format("http://%s:4444/wd/hub/", seleniumGridHubHost));
// Remote Driver, e.g. on a VM
RemoteWebDriver driver = new RemoteWebDriver(remoteAddress, capabilities);

return driver;
}
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s