How to set Proxy in Firefox using Selenium WebDriver?
Tags: testing, tutorial, proxy, java
Published: 2022-02-21 12:10:30

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](https://www.browserstack.com/guide/selenium-webdriver-tutorial) could be helpful in capturing traffic.

Proxy servers let the user access the URL of the [web application for testing](https://www.browserstack.com/web-application-testing-on-cloud), 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](https://www.browserstack.com/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();
}
}
```

### 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();
}
}
```

### 3. Using Desired Capabilities

Similarly one can also set Proxy in Firefox using Selenium Webdriver, through its [desired capabilities](https://www.browserstack.com/guide/desired-capabilities-in-selenium-webdriver).

```
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();
}
}

```

BrowserStack allows its users to set Desired Capabilities through [Capabilities Generator](https://www.browserstack.com/automate/capabilities) as shown below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g6m00uemblchtgq5w0ve.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mliizw2qiyva9v5zklst.png)

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](https://www.browserstack.com/docs/live/local-testing/behind-proxies-firewalls-vpns).

```
bsLocalArgs.put("proxyHost", "127.0.0.1");
bsLocalArgs.put("proxyPort", "8000");  
```

One can also perform [Local Testing of your websites with Network Restrictions](https://www.browserstack.com/docs/live/local-testing/behind-proxies-firewalls-vpns) using BrowserStack Local.