DEV Community

Garima Tiwari
Garima Tiwari

Posted on • Updated on • Originally published at browserstack.com

How to set Proxy in Firefox using Selenium WebDriver?

Internet bandwidth is an important asset, especially at workplaces where multiple resources use the same network simultaneously. High-speed internet enables work to be completely faster and more efficiently. This is particularly true when working with web applications, where the internet is actively used.

Setting up Proxy Servers makes the system faster by compressing traffic, managing cache data, and frequently visited web pages. This enhances internet speed and decreases page load speed significantly.

The proxy server is an entity between the client and the server. Whenever placing requests, the entire traffic from the server flows to the client via the proxy server. Setting up the proxy server while testing a web application on Selenium WebDriver could be helpful in capturing traffic.

Proxy servers let the user access the URL of the web application for testing, despite the complex topologies of the network. This network at the workplace complies with the strict policies and thus has many restrictions that hinders testing web applications on it.

This could also mock the backend request calls made by the web application under test, which could be useful in the testing process.

While running a Selenium test, it is essential to set the proxy settings of the browser, since the WebDriver launches the browser all over again, every time a test is run. However, each time the browser is launched, the proxy settings reset automatically and need to be reset every time.

This article discusses the various methods by which to manage proxy settings in Selenium WebDriver.

Setting up Firefox Proxy using Selenium

There are three ways of setting up Firefox Proxy using Selenium:

  1. By adding preferred Proxy Server Host and Port details to FirefoxOptions class, that can be later used in the tests.
  2. By setting Firefox Profile using FirefoxProfile class.
  3. By setting up desired capabilities.

1. Using FirefoxOptions Class

The following code sets up Proxy by using Class FirefoxOptions:

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class proxyTest {
public static void main(String[] args) {

Proxy proxy = new Proxy();
//Adding the desired host and port for the http, ssl, and ftp Proxy Servers respectively
proxy.setHttpProxy("<HOST:PORT>");
proxy.setSslProxy("<HOST:PORT>");
proxy.setSslProxy("<HOST:PORT>");
proxy.setFtpProxy("<HOST:PORT>");
FirefoxOptions options = new FirefoxOptions();
options.setCapability("proxy", proxy);
//Calling new Firefox profile for test
WebDriver driver = new FirefoxDriver(options);
driver.get("https://www.browserstack.com/");
driver.manage().window().maximize();
driver.quit();
}
}
Enter fullscreen mode Exit fullscreen mode

2. Using FirefoxProfile Class

Similar to the previous method, one can also set Firefox Proxy in Selenium using FirefoxProfile Class. Use the code below to do it:

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

public class proxyTest {
public static void main(String[] args) {


Proxy proxy = new Proxy();
//Adding the desired host and port for the http, ssl, and ftp Proxy Servers respectively 
proxy.setHttpProxy("<HOST:PORT>");
proxy.setSslProxy("<HOST:PORT>");
proxy.setSslProxy("<HOST:PORT>");
proxy.setFtpProxy("<HOST:PORT>");

FirefoxProfile profile = new FirefoxProfile();
profile.setProxyPreferences(proxy);

//Calling new Firefox profile for test
WebDriver driver = new FirefoxDriver(profile);
driver.get("https://www.browserstack.com/");
driver.manage().window().maximize();
driver.quit();
}
}
Enter fullscreen mode Exit fullscreen mode

3. Using Desired Capabilities

Similarly one can also set Proxy in Firefox using Selenium Webdriver, through its desired capabilities.

import java.io.IOException;
import org.openqa.selenium.Proxy;
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 proxyTest {
public static void main(String[] args) {

Proxy proxy = new Proxy();
//Adding the desired host and port for the http, ssl, ftp Proxy Servers respectively
proxy.setHttpProxy("<HOST:PORT>");
proxy.setSslProxy("<HOST:PORT>");
proxy.setSslProxy("<HOST:PORT>");
proxy.setFtpProxy("<HOST:PORT>");

DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.PROXY, proxy);

//Calling new Firefox profile for test
WebDriver driver = new FirefoxDriver(dc);
driver.get("https://www.browserstack.com/");
driver.manage().window().maximize();
driver.quit();
}
}

Enter fullscreen mode Exit fullscreen mode

BrowserStack allows its users to set Desired Capabilities through Capabilities Generator as shown below.

Image description

The Way Ahead

Testing websites that are on private networks is always a tough job, and it requires setting up of Proxy Server. However, using BrowserStack Local, private websites can be tested on real browsers and devices by manual and automated testing. The latter is accomplished via a Cloud Selenium Grid. The cloud also provides integrations with popular CI/CD tools such as Jira, Jenkins, TeamCity, Travis CI, and much more. Additionally, there are in-built debugging tools that let testers identify and resolve bugs immediately.

Image description

By Setting browserstack.local capability to true in the test scripts, you can test localhost websites. Then, put the following code snippet in the test script to set up a proxy, while using BrowserStack Local.

bsLocalArgs.put("proxyHost", "127.0.0.1");
bsLocalArgs.put("proxyPort", "8000");  
Enter fullscreen mode Exit fullscreen mode

One can also perform Local Testing of your websites with Network Restrictions using BrowserStack Local.

Top comments (0)