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#
- Create a folder named 'extension'
- 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 }
- 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']);
Open chrome browser, go to chrome://extensions and turn on developer mode
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 #
- Copy the .crx file into your framework
- Configure your webdriver creation to load the extension like
options.addExtensions(new File("path/to/extension.crx")); options.addArguments("--no-sandbox");
- Invoke your webdriver and application URL
- 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
*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:[email protected]");
.