Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to handle an authentication pop-up in one of my new Webdriver scripts. I have a working solution for IE, but I am struggling with Chrome. IE was as simple as following the advice on [this page]:How to handle authentication popup with Selenium WebDriver using Java. That thread doesn't show a great solution for Chrome, although several commentors point out, that the solution does not work for Chrome. The problem is, when you try to do the below code on Chrome, the login popup isn't an Alert.

 WebDriverWait wait = new WebDriverWait(driver, 10);      
 Alert alert = wait.until(ExpectedConditions.alertIsPresent());     
 alert.authenticateUsing(new UserAndPassword(**username**, **password**));

It's not a windows level () authentication pop-up, the web page is simply password protected. I know there are several other instances of this question on Stack Overflow, but I don't see any more recently than 2 years old. I am hoping there is a better solution now in 2017. Thanks in advance.

like image 318
Travis Needham Avatar asked Feb 08 '17 13:02

Travis Needham


2 Answers

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

like image 57
Bhuvanesh Mani Avatar answered Sep 21 '22 18:09

Bhuvanesh Mani


*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]");.

like image 44
Grzegorz Górkiewicz Avatar answered Sep 24 '22 18:09

Grzegorz Górkiewicz