How to handle authentication popup in Chrome with Selenium WebDriver using Java How to handle authentication popup in Chrome with Selenium WebDriver using Java google-chrome google-chrome

How to handle authentication popup in Chrome with Selenium WebDriver using Java


May be helpful for others to solve this problem in chrome with the help of chrome extension. Thanks to @SubjectiveReality who gave me this idea.

Sending username and password as part of url like https://username:password@www.mytestsite.com may be helpful if same server performs both authentication and hosts the application. However most corporate applications have firmwide authentications and app server may reroute the request to authentication servers. In such cases, passing credentials in URL wont work.

Here is the solution:

#Step1: Create chrome extension#

  1. Create a folder named 'extension'
  2. Create a file named 'manifest.json' inside 'extension' folder. Copy below code into the file and save it.

{"name":"Webrequest API","version":"1.0","description":"Extension to handle Authentication window","permissions":["<all_urls>","webRequest","webRequestBlocking"],"background": {"scripts" : ["webrequest.js"]},"manifest_version": 2}

  1. Create a file named 'webrequest.js' inside 'extension' folder and copy paste below code into the file and save it.
chrome.webRequest.onAuthRequired.addListener(function handler(details){ return {'authCredentials': {username: "yourusername", password: "yourpassword"}};},{urls:["<all_urls>"]},['blocking']);
  1. Open chrome browser, go to chrome://extensions and turn on developer mode

  2. Click 'Pack Extension', select root directory as 'extension' and pack extension. It should create a file with extension '.crx'

#Step2: Add extension into your test automation framework #

  1. Copy the .crx file into your framework
  2. Configure your webdriver creation to load the extension like
options.addExtensions(new File("path/to/extension.crx"));options.addArguments("--no-sandbox");
  1. Invoke your webdriver and application URL
  2. You wont see the authentication popup appearing as its handled by above extension

Happy Testing!

References:

http://www.adambarth.com/experimental/crx/docs/webRequest.html#apiReferencehttps://developer.chrome.com/extensions/webRequest#event-onAuthRequiredchrome.webRequest.onAuthRequired Listenerhttps://gist.github.com/florentbr/25246cd9337cebc07e2bbb0b9bf0de46


*edit Chrome no longer supports this.

Isn't that a "restricted" pop-up that can be handled by prepending the address with username and password?

Instead of driver.get("http://www.example.com/"); go for driver.get("http://username:password@www.example.com");.


Updated answer of the solution provided by @Bhuvanesh Mani

manifest.json

    {        "name": "Webrequest API",        "version": "1.0",        "description": "Extension to handle Authentication window",        "permissions": [            "webRequest",            "webRequestBlocking",            "<all_urls>"        ],        "background": {            "scripts": [                "webrequest.js"            ]        },        "manifest_version": 2    }

webrequest.js

    chrome.webRequest.onAuthRequired.addListener(function(details){        console.log("chrome.webRequest.onAuthRequired event has fired");        return {                authCredentials: {username: "yourusername", password: "yourpassword"}            };    },    {urls:["<all_urls>"]},    ['blocking']);

Not certain why the '--no sandbox' option is required, but for me this is not required.

I did need to perform a registry edit to allow adding extensions since this is disabled with a corporate AD policy:

  • Open regedit.exe as admin
  • navigate toComputer\HKEY_USERS\
  • Change the REG_SZ value that is now set to * to for example -.
  • restart the chrome instance in which you want add extensions to.

The only difference with the original answer is the url's that also need to be present in the permissions.

extra's
if you want to see console output, turn on developer mode and click on the Inspect views background page for your extension in the extensions screen in Chrome