Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Proxy Authentication in seleniumWebdriver for Chrome Browser

I'm trying to Automate a web application selenium 2.0 [webdriver+java].The web application is currently deployed in our UAT servers on our local network.My test cases are executing, but I have to manually enter the Proxy Authentication details for my Chrome instance at the start of the test execution. I have tried all the solutions provided on stack overflow but still, the authentication message pops out.

enter image description here

This is the code I'm using in my driver initializing process

package com.misyn.ess.ui;

import java.util.Arrays;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

/**
 *
 * @author User
 */
public class DriverClass {

    private String baseUrl;
    private String driverPath;
    private String driverName;
    private static WebDriver driver;
    private static DriverClass driverClass;

        private DriverClass() {
            try {
                baseUrl = "http://192.168.0.10:8282/ess";
                driverPath = "E:\\Work_Folder\\SelTools\\chromedriver.exe";
                driverName = "webdriver.chrome.driver";

                //Set the location of the ChromeDriver
            System.setProperty(driverName, driverPath);
            //Create a new desired capability
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            // Create a new proxy object and set the proxy
            Proxy proxy = new Proxy();
            proxy.setHttpProxy("192.168.0.200:3128");
            proxy.setSocksUsername("avishka");
            proxy.setSocksPassword("12345678");
            //Add the proxy to our capabilities 
            capabilities.setCapability("proxy", proxy);
            //Start a new ChromeDriver using the capabilities object we created and added the proxy to
            driver = new ChromeDriver(capabilities);

            //Navigation to a url and a look at the traffic logged in fiddler
            driver.navigate().to(baseUrl);


    //            System.setProperty(driverName, driverPath);
    //            driver = new ChromeDriver();
    //            driver.get(baseUrl);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

Can anyone give me a solution how to give this proxy username and password thing from the application itself than manually entering details on the pop-up(Authentication), any help would be much appreciated.Thanks

the currently answered one is only for

As of Selenium 3.4 it is still in beta Right now implementation is only done for InternetExplorerDriver

Where I'm using selenium 3.0 and Google Chrome as my web browser.

like image 901
Avishka Perera Avatar asked Jul 06 '17 05:07

Avishka Perera


People also ask

How does Selenium handle proxy popups?

You can handle proxy authentication popups using Selenium web driver by switching to the HTTP proxy authentication alert and passing the user name and password directly to the alert. With the help of send keys method.

Can we handle a proxy using Selenium in Java?

We can handle proxy in Selenium in Java with the help of PROXY class.


1 Answers

You can do via MultiPass for HTTP basic authentication

Download the extension from
https://chrome.google.com/webstore/detail/multipass-for-http-basic/enhldmjbphoeibbpdhmjkchohnidgnah

Download the extension as crx. You can get it as crx from chrome-extension-downloader

After that the config is simple.

import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

/**
 *
 * @author Phystem
 */
public class ChromeAuthTest {

    WebDriver driver;

    public ChromeAuthTest() {
        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
    }

    private void initDriver() {
        ChromeOptions cOptions = new ChromeOptions();
        cOptions.addExtensions(new File("MultiPass-for-HTTP-basic-authentication_v.crx"));
        driver = new ChromeDriver(cOptions);
        configureAuth(
                "https://the-internet.herokuapp.com/basic_auth",
                "admin",
                "admin");
    }

    private void configureAuth(String url, String username, String password) {
        driver.get("chrome-extension://enhldmjbphoeibbpdhmjkchohnidgnah/options.html");
        driver.findElement(By.id("url")).sendKeys(url);
        driver.findElement(By.id("username")).sendKeys(username);
        driver.findElement(By.id("password")).sendKeys(password);
        driver.findElement(By.className("credential-form-submit")).click();
    }

    public void doTest() {
        initDriver();
        driver.get("https://the-internet.herokuapp.com/basic_auth");
        System.out.println(driver.getTitle());
        driver.quit();
    }

    public static void main(String[] args) {
        new ChromeAuthTest().doTest();
    }
}

I have used a sample site for testing.

Provide your url,username and password in the configure Auth function and try

like image 80
Madhan Avatar answered Sep 20 '22 18:09

Madhan