Handle Authentication Popup in Chrome with Selenium WebDriver using Java

I am sharing here steps to handle an authentication pop-up in Chrome using Selenium Web driver. Traditional solutions of passing username and password in URL does not work due to security reasons i.e. driver.get(http://username:password@www.example.com. Here is the step by step way of handling authentication alert.

  1. Create a folder named ‘extension’ in the project repository
  2. Create a file named ‘manifest.json’ inside ‘extension’ folder. Copy below code into the file, replace the Url and save it.

manifest.json:

{
  “name”: “Webrequest API”,
  “version”: “1.0”,
  “description”: “Extension to handle Authentication window”,
  “permissions”: [
    “webRequest”,
    “webRequestBlocking”,
    https://ReplaceYourCompanyUrl
 
],
  “background”: {
    “scripts”: [
      “webrequest.js”
   
]
  },
  “manifest_version”: 2
}

  • Create a file named ‘webrequest.js’ inside ‘extension’ folder and copy paste below code after replacing actual UserName and Password into the file and save it.

webrequest.js:
chrome.webRequest.onAuthRequired.addListener(function(details){
     console.log(“chrome.webRequest.onAuthRequired event has fired”);
     return {
             authCredentials: {username: “YourUsername”, password: “YourPassowrd”}
         };
 },
 {urls:[“<all_urls>”]},
 [‘blocking’]);

  • Open chrome browser, go to chrome://extensions and turn on developer mode which you can say on the top right corner under header bar.
  • Now click ‘Pack Extension’ button, and give path of your ‘extension’ folder from project repository in first text box ‘Extension root directory’and click ‘Pack Extension’. File ‘extension.crx’ will be created into the project root directory

  • Now finally, give path of this extension.crx into Chrome options and this extension will be added and when you run the HTTP Authentication will be Bypassed.
  • Here is an example using TestNG and Java.

@Test(description = “Test to accept Windows Authentication to Login Web”)
public void authenticationByPassTest() {

        // Set here path of chromedriver.exe
    
System.setProperty(“webdriver.chrome.driver”, GiveHerePathOfChromeExe \\chromedriver.exe”);;
        ChromeOptions chromeoptions = new ChromeOptions();

        // set path of extension ‘extension.crx’ created by chrome extensions
       
chromeoptions.addExtensions(new File(“GiveHerePathOf\\extension.crx”));

        // Create Chrome driver
       
WebDriver driver = new ChromeDriver(chromeoptions);

// Now proceed with writing your test

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s