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

I am trying to process an authentication popup in one of my new Webdriver scripts. I have a working solution for IE, but I'm struggling with Chrome. IE was as simple as following the tips on this page: How to handle a popup using Selenium WebDriver using Java . This thread does not show an excellent solution for Chrome, although some commentators note that the solution does not work for Chrome. The problem is that when you try to execute the code below in Chrome, the login popup is not a warning.

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

This is not a Windows-level popup (), the webpage is simply password protected. I know that there are several other examples of this question in Stack Overflow, but I have not seen anything but two years. I hope there is a better solution in 2017. Thanks in advance.

+8
source share
3 answers

* edit Chrome no longer supports this.

Isn't this a “limited” pop-up that can be handled by adding an address with a username and password?

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

+2
source

I know your situation is with Java, but it might be useful. I managed to get through the warning using this approach in C #:

 // I am using a static instance of the driver for this. Driver.Instance.Navigate().GoToUrl(url); var alert = Driver.Instance.SwitchTo().Alert(); alert.SendKeys($"{Driver.user_name}" + Keys.Tab + $"{Driver.user_password}" + Keys.Tab); alert.Accept(); Driver.Instance.SwitchTo().DefaultContent(); 

Then I used the test user account as user_name and password values.

0
source

For others, it may be useful to solve this problem in Chrome using the Chrome extension. Thanks to @SubjectiveReality for giving me this idea.

Sending a username and password as part of the URL, for example https: // username: password@www.mytestsite.com , can be useful if the same server authenticates and hosts the application. However, most enterprise applications have enterprise-wide authentication, and the application server can redirect the request to authentication servers. In such cases, submitting credentials in the URL will not work.

Here is the solution:

Step 1: Create a Chrome Extension

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

{"name": "API Webrequest", "version": "1.0", "description": "Extension for processing the authentication window", "permissions": ["", "webRequest", "webRequestBlocking"], "background" ): {"scripts": ["webrequest.js"]}, "manifest_version": 2}

  1. Create a file called "webrequest.js" inside the "extension" folder and copy the code below 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 the Chrome browser, go to chrome: // extensions and enable developer mode

  2. Click "Package Extension", select the root directory as "extension" and add the extension. You must create a file with the extension .crx

Step 2. Add the extension to your test automation infrastructure.

  1. Copy the .crx file to your framework
  2. Configure your web driver to download the extension as
 options.addExtensions(new File("path/to/extension.crx")); options.addArguments("--no-sandbox"); 
  1. Call your web driver and application url
  2. You will never see the authentication pop-up displayed as the extension mentioned above.

Happy testing!

Recommendations:

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

0
source

Source: https://habr.com/ru/post/1263967/


All Articles