Handle Proxy in Selenium

How to Handle Proxy in Selenium WebDriver

Most of the time when we create any WebDriver automation code using public websites like Google, Facebook, Yahoo… etc, which works very well at our home pc but not at office.

This may have various reasons. One of the reasons is PROXY at your office network.

What is Proxy?

proxy or proxy server is basically another computer which serves as a hub through which internet requests are processed. By connecting through one of these servers, your computer sends your requests to the proxy server which then processes your request and returns what you were wanting.

In this way it serves as an intermediary between your home machine and the rest of the computers on the internet. Proxies are used for a number of reasons such as to filter web content, to go around restrictions such as parental blocks, to screen downloads and uploads and to provide anonymity when surfing the internet.

How to Handle Proxy in Selenium Webdriver

You have now two options to handle this so based on your preferences you can use any one of this. I always consider the second approach.

1- Manual Proxy Configuration
2- Automatic Proxy Configuration.

1- Manual Proxy Configuration

To handle proxy setting in Selenium we have a separate class called Proxy that is available inside org.openqa.selenium package

Approach-

1-Create object of proxy class and set HTTP proxy or FTP proxy based on requirement

These are methods available for the proxy.

2 -Use DesiredCapability class to customize capability of browser and pass the proxy object.

3- While initiating browser pass capability object

Let’s implement the same- Set Proxy Settings in Selenium (Manual Proxy Configuration)

package pack7;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class ManualProxySetting {

	public static void main(String[] args) {

		//Set proxy IP and port. Here localhost Is proxy IP and 8080 Is Port number.
		//You can change both values as per your requirement.
		String PROXY = "localhost:8080";
		//Bellow given syntaxes will set browser proxy settings using DesiredCapabilities.
		Proxy proxy = new Proxy();
		proxy.setHttpProxy(PROXY).setFtpProxy(PROXY).setSslProxy(PROXY).setSocksProxy(PROXY);
		DesiredCapabilities cap = new DesiredCapabilities();
		cap.setCapability(CapabilityType.PROXY, proxy);
		//Use Capabilities when launch browser driver Instance.
		FirefoxDriver driver = new FirefoxDriver(cap);
		driver.get("http://www.javaseleniumworld.com/");

	}
}

Once you execute above program, proxy setting will be set in browser, you can check through browser setting and You can see the proxy settings has been added now.

 

Question: Let’s assume you have set of 100 test cases and now you have to make changes in all test cases?

Answer: I always use the Base class concept of Selenium so that it will reflect to all the test cases.

Automatic Proxy Configuration:

To handle Automatic proxy setting in Selenium we have a separate method called setProxyAutoconfigUrl(args) of Proxy class.

Approach:

1 – You have to pass third party Proxy server site as argument in setProxyAutoconfigUrl() method.

2 -Use DesiredCapability class to customize capability of browser and pass the proxy object.

3- While initiating browser pass capability object

Here your application URL will open through proxy server site.

Please refer the below example for the same.

package pack7;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class AutomaticProxySetting {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Proxy proxy = new Proxy();
		proxy.setProxyAutoconfigUrl("http://www.freeproxy-server.net/");
		DesiredCapabilities cap = new DesiredCapabilities();
		cap.setCapability(CapabilityType.BROWSER_NAME, "firefox");
		cap.setCapability(CapabilityType.PROXY, proxy);

		FirefoxDriver driver = new FirefoxDriver(cap);
		driver.get("http://www.javaseleniumworld.com/");

	}
}

Once you execute above program, proxy setting will be set in browser, you can check through browser setting and You can see the proxy settings has been added now.

Leave a Reply

Your email address will not be published. Required fields are marked *