Thursday, September 12, 2019

Post#22.Handle PROXY in Selenium Webdriver


Handle PROXY in Selenium WebDriver

A proxy is nothing but a mask over your computer address, every company will have proxy at least for installing software.
Basically proxy is nothing but showing someone else's computer as your computer through masking.
By connecting through one of these proxy servers, your computer sends your requests to the proxy server which then processes your request and returns what you were wanting.

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.

Steps to Handle Proxy in selenium: If you ever visited Proxy section of the Browser you might have noticed that there are two types of proxy settings.
  • Automatic configuration
  • Manual Configuration
You might have read using Capabilities class to set the Proxy but with updated versions of selenium will support Browser Options class.


Automatic proxy in selenium:
We can set the automatic proxy using ChromeOptions/FirefoxOptions/InternetExplorerOptions class, for setting proxy we have to configure Proxy in selenium WebDriver.
setProxyAutoconfigUrl is the method we have to call from the Object of Proxy class to set the automatic proxy.

Don't close the browser in your code as we want to ensure whether our code worked or not. Don't forget to pass the Browser options object to Browser Driver.
package pack2;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class AutoProxyConfig
{
public static void main(String[] args) {
              System.setProperty("webdriver.gecko.driver", "D:\\PATH\\geckodriver.exe");
              FirefoxOptions fo = new FirefoxOptions();
              // Create object Proxy class
              Proxy prox = new Proxy();
              prox.setProxyAutoconfigUrl("https://www.proxysite.com/");
              // register the proxy with options class
              fo.setProxy(prox);
              // create object to firefx driver
              WebDriver driver = new FirefoxDriver(fo);
}
}
Open the proxy setting in the browser that you opened with automation.



Manual Proxy Configuration in Selenium:
In below image i have highlighted what are the required for the manual proxy,

We have to set:
  • Http Proxy
  • SSL Proxy
  • FTP Proxy
  • Socks Prox
Important note: To Socks Proxy, we must include the Socks version, there are two versions available 1. SOCKs v4, 2. SOCKS v5. We can pass integer number 4 or 5 to mention v4 and v5.
If we did not set the version then selenium throws below exception, as well as you need let the system know that You are setting manual Proxy.

Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: InvalidArgumentError: Expected [object Undefined] undefined to be an integer

Let see the program, how to set manual proxy in selenium

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

public class ManualProxyConfig
{
              public static void main(String[] args) {
                             System.setProperty("webdriver.gecko.driver", "D:\\PATH\\geckodriver.exe");
                             FirefoxOptions fo = new FirefoxOptions();
                             String proxyAddress = "dummyhost:8080";
                             // Create object Proxy class
                             Proxy prox = new Proxy();
                             // setting manual proxy
                             prox.setProxyType(ProxyType.MANUAL);
                             prox.setHttpProxy(proxyAddress);
                             prox.setFtpProxy(proxyAddress);
                             prox.setSslProxy(proxyAddress);
                             // set the SOCKS version
                             prox.setSocksVersion(4);
                             prox.setSocksProxy(proxyAddress);
                             // register the proxy with options class
                             fo.setProxy(prox);
                             // create object to firefx driver, register options class object
                             WebDriver driver = new FirefoxDriver(fo);
              }
}
Open the proxy setting in the browser that you opened with automation.


No Proxy Configuration in Selenium:
We can also set No Proxy option in the browser using selenium, to set No proxy we have to use ProxyType as Direct. Normally we use No Proxy to bypass all the proxies set.
package pack2;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class NoProxyConfig
{
              public static void main(String[] args) {
                             System.setProperty("WebDriver.gecko.driver", "D:\\PATH\\geckodriver.exe");
                             FirefoxOptions fo = new FirefoxOptions();
                             // Create object Proxy class
                             Proxy prox = new Proxy();
                             prox.setProxyType(ProxyType.DIRECT);
                             // register the proxy with options class
                             fo.setProxy(prox);
                             // create object to firefx driver, register options class
                             WebDriver driver = new FirefoxDriver(fo);
              }
}
Output of Direct Proxy program with selenium


Auto Detect Proxy Configuration in Selenium:
By using ProxyType Enum we can set the Auto detect proxy in browser
package pack2;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class AutoDetectProxyConfig
{
              public static void main(String[] args) {
                             System.setProperty("WebDriver.gecko.driver", "D:\\PATH\\geckodriver.exe");
                             FirefoxOptions fo = new FirefoxOptions();
                             // Create object Proxy class
                             Proxy prox = new Proxy();
                             prox.setProxyType(ProxyType.AUTODETECT);
                             // register the proxy with options class
                             fo.setProxy(prox);
                             // create object to firefx driver, register options class
                             WebDriver driver = new FirefoxDriver(fo);
              }
}
Output of Auto proxy program


System Proxy Configuration in Selenium:
System proxy is also similar to Auto Proxy, setting system proxy reads the proxy value from the system.
package pack2;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class SystemProxyConfig
{
              public static void main(String[] args) {
                             System.setProperty("WebDriver.gecko.driver", "D:\\PATH\\geckodriver.exe");
                             FirefoxOptions fo = new FirefoxOptions();
                             // Create object Proxy class
                             Proxy prox = new Proxy();
                             prox.setProxyType(ProxyType.SYSTEM);
                             // register the proxy with options class
                             fo.setProxy(prox);
                             // create object to firefx driver, register options class
                             WebDriver driver = new FirefoxDriver(fo);
              }
}

Getter methods in Proxy:
There are few methods present in the proxy class which will retrieve the details about the proxy, which come handy when you want to instruct the browser to perform some tasks based on the details.
  • getFtpProxy : : This method retrieves the ftp proxy of the browser
  • getHttpProxy : fetches the http proxy
  • getNoProxy : gets the values of no proxy
  • getProxyAutoconfigUrl : this methods retrieves the url of the automatic proxy
  • getProxyType : fetches the proxy type
  • getSocksPassword : fetches the password of the SOCKS proxy
  • getSocksProxy : fetches the proxy of the SOCKS
  • getSocksUsername : fetches the username of the SOCKS proxy
  • getSocksVersion : fetches Socks version, the value could be 4 or 5
  • getSslProxy : fetches the SSL proxy
  • isAutodetect : This methods return true or false, true if the proxy is Auto Detect/ Direct

No comments:

Post a Comment