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:[email protected] 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#apiReference
https://developer.chrome.com/extensions/webRequest#event-onAuthRequired
chrome.webRequest.onAuthRequired Listener
https://gist.github.com/florentbr/25246cd9337cebc07e2bbb0b9bf0de46

Leave a Comment