Create automated website tests for free with LoadFocus.com.

If you need to run Selenium WebDriver UI tests in a browser that sends the traffic through a proxy, here is a code example of a Selenium test.
In this article we will show how easy it is to configure WebDriver to use a proxy by presenting a small code snippet that uses the FireFox driver and for which we set the HTTP, SSL and FTP proxy:

package com.webdriver.automation.tests;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.Proxy.ProxyType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class WebdriverUsingProxy {

private List<String> urlList = new ArrayList<String>();

@Before
public void setup() {
urlList.add("http://news.bbc.co.uk");
urlList.add("https://news.google.com");
}

@Test
public void passTraffixThroughProxyTest() {
String httpProxy = "10.10.1.3:8080";
String sslProxy = "10.10.1.3:8080";
String ftpProxy = "10.10.1.3:8080";

DesiredCapabilities capability = new DesiredCapabilities();
addProxyCapabilities(capability, httpProxy, sslProxy, ftpProxy);

for (String url : urlList){
WebDriver driver = new FirefoxDriver(capability);
driver.get(url);
driver.close();
}
}

public static DesiredCapabilities addProxyCapabilities(DesiredCapabilities capability, String httpProxy, String sslProxy,
String ftpProxy) {
Proxy proxy = new Proxy();
proxy.setProxyType(ProxyType.MANUAL);
proxy.setHttpProxy(httpProxy);
proxy.setSslProxy(sslProxy);
proxy.setFtpProxy(ftpProxy);

capability.setCapability(CapabilityType.PROXY, proxy);
capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
return capability;
}
}

In the code above we set the proxy type to manual and we’ve set the HTTP, SSL and FTP proxy to 10.10.1.3:8080. This is done using the Proxy object and setting the desired capability, pretty easy 🙂

If you want to see a easy and complete way to locate web elements check our last Selenium WebDriver blog post.

Have fun passing traffic through proxy in your tests. Also check Load Focus, our Cloud Load and Performance Application Testing Service. You can stress your app/website/API with thousands of concurrent users from all over the world.