Proxy in Selenium Webdriver using C# | EngineerDiaries

Jan 21, 2021, 1821 Views, 0

Contents

  • What is HTTP Proxy Server?
  • HTTP Proxy VS VPN
  • Code for proxy in Selenium driver in c#
  • Code Explanation
  • Summary
  • 1.What is HTTP Proxy Server?

    In a normal setup, we have a client and a server where the client sends a request to the server, and the server sends an HTTP response back to the client. But with the Proxy server, there is a middleware added to the whole scenario where the client is using a proxy meaning not using its original identity. So, in the tech world ID of the system is through an IP address, hence the proxy server masks the IP address with a proxy value. There are quite a few reasons especially in the case of testing using selenium webdriver:
    1. When you have to test UI for various timezones
    2. Using the proxy for different geolocations for maps
    3. Using the proxy to test for the experience of people in different countries.

    Proxy in Selenium Webdriver using C# | EngineerDiaries

    2.HTTP Proxy VS VPN

    Now the basic question that arises is what is the difference between a proxy and a VPN. Though both mask your IP addresses, VPN encrypts all the traffic onto your system from all the applications. SO, the proxy was just good to let you get over content restrictions based on your geographies, VPN becomes a necessity while using public Wi-Fi for an additional security layer for the protection of all the browsing information. But both have a common drawback that is the performance issue. One could definitely imagine that a third tier is added which would send the request to another remote server first get a response and then send it back to the server needed to interact with.

    3.Code for proxy in Selenium driver in c#

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using WebApplication1.Models;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Remote;
    using OpenQA.Selenium.Support.UI;
    using SeleniumExtras.WaitHelpers;
    using System.Net;
    using SeleniumProxyAuth;
    using SeleniumProxyAuth.Models;

    namespace WebApplication1.Controllers
    {
        public class HomeController : Controller
        {
            public IActionResult Index()
            {
                ProxyAuth auth = new ProxyAuth("11.22.33.44", 80, "prox - username2", "proxy - password2");
                var proxyServer = new SeleniumProxyServer();

                var localPort = proxyServer.AddEndpoint(auth);

                ChromeOptions options = new ChromeOptions();
                options.BinaryLocation = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";


                DesiredCapabilities capabilities = new DesiredCapabilities();
               
                capabilities.SetCapability(ChromeOptions.Capability, options);
               
                options.AddArgument($"--proxy-server=127.0.0.1:{localPort}");
              // Create the driver
                ChromeDriver driver = new ChromeDriver("C:\\WebDriver\\bin", options);

                driver.Navigate().GoToUrl("http://ip.smartproxy.com/");
                // Dispose the driver
                driver.Dispose();
           
                return View();

            }
        }
    }

    4.Code Explanation

    1. Here we are using the NuGet package SeleniumProxyAuth. Official documentation of the same is here
    2.ProxyAuth is used to create a proxy address with parameters representing-IP address, port, username, password respectively. Hence
    a proxy server is created.
    3. chromeOptions are used to add headers to the driver. Here we are using chromedriver.
    4. http://ip.smartproxy.com/ to locate the new IP address as without proxy you should be getting your public IP
    5. You may need to allow your browser to let proxy serevr in if it restricts.

    5.Summary

    1. Proxy is used to mask the identity of the system for testing for different geographic locations.
    2. VPN encrypts the data from all the traffic on the system and is safer than the Proxy server. Hence must be used while using WIfi in public places.
    3. Here we have used the SeleniumProxyAuth NuGet package to build a proxy server that the selenium web driver could use.
    4. Build SeleniumProxyServer using proxy IP, port, username, and password.
    5. Add proxy server headers in an argument in the driver.

    Comments Section: