Test Local Website in Firefox with Selenium Effortlessly

Set & Authenticate Proxy in Firefox using Selenium. Test Local Website on Real Devices for a user-like experience before launching

Get Started free
Home Guide How to set Proxy in Firefox using Selenium WebDriver?

How to set Proxy in Firefox using Selenium WebDriver?

By Garima Tiwari, Community Contributor -

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

Also Read: How to set a proxy in Chrome using Selenium

Talk to an Expert

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

BrowserStack Automate Banner

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

Capability Generator using Automate

Run Selenium Tests on Cloud for Free

The Way Ahead

Testing websites that are on private networks is always a tough job, and it requires setting up of Proxy Server. However, BrowserStack allows testing of private websites using its Local Testing feature. BrowserStack’s real device cloud offers 2000+ real browsers and devices for 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. BrowserStack also facilitates Cypress testing on 30+ browser versions with instant, hassle-free parallelization.

set Proxy in Firefox using Selenium WebDriver

Set up BrowserStack Local now!

By Setting browserstack.local capability to true in the test scripts, to 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");

Start Local Testing of your websites with Network Restrictions using BrowserStack Local!

Tags
Automation Testing Cross browser testing Selenium Selenium Webdriver

Featured Articles

All about TestNG Listeners in Selenium

Why are Device Farms so important for Software Testing?

Automation Tests on Real Devices & Browsers

Seamlessly Run Automation Tests on 3500+ real Devices & Browsers