[Solved] Windows Authentication Automation with WebDriver and DevTools API
In the last several years, more than a thousand times — we got this question: How to automate window authentication? and every time, we have answered the workaround and not the solution. This time, we want to present the solution and of course, with Selenium 4.x version.
I am sure, you will love this solution and if you do so, please take a moment to clap, share, and let others know!
HTTP Basic Authentication
There are several authentications available like basic, digestive, token, NTLM, etc. In which, the basic authentication scheme is a widely used, industry-standard method for collecting user name and password information. The basic authentication transmits user names and passwords across the network in an unencrypted form.
Why Selenium does not automate this authentication window?
Selenium language bindings (1) works through WebDriver API (2) and that communicates to browser vendor driver binary (3) to communicate with the browser (4). In this entire process, the API and driver executable is designed to communicate only with HTML elements that are developed by the web developers and not the authentication window shown by the browser.
Is that limitation of WebDriver (Selenium)? No, it is an absolutely carefully designed feature NOT to handle other than browser DOM elements.
Okay, then how can we handle such authentication pop-up in Selenium automation? There are several popular workarounds to make it work like: AutoIt, Sikuli, Robot, and even chrome extensions. And most of them fail at large especially when you run in different machine configurations, and in parallel. These workarounds certainly break if you like to run the browsers in headless mode!!
Are you one such person who experienced what I said above? If yes, then you should try the solution now. Yes, I mean now and you will love it for sure.
Here are the Steps:
1Make sure you are using Selenium Webdriver 4.0.0-alpha-2 or above. If you are not using the expected version, update your dependency in maven or gradle! Same time, if you have restrictions on upgrading, there is an alternate solution of using developer tools directly with your selenium version too.
2 Add the following code snippet to your existing selenium script. Note that your window authentication username and password should be replaced.
String username = “admin”; // authentication username
String password = “admin”; // authentication password// Get the devtools from the running driver and create a session
DevTools devTools = driver.getDevTools();
devTools.createSession();// Enable the Network domain of devtools
devTools.send(Network.enable(Optional.of(100000), Optional.of(100000), Optional.of(100000)));// Encoding the username and password using Base64 (java.util)
String auth = username +”:”+ password;
String encodeToString = Base64.getEncoder().encodeToString(auth.getBytes());// Pass the network header -> Authorization : Basic <encoded String>
Map<String, Object> headers = new HashMap<String, Object>();
headers.put(“Authorization”, “Basic “+encodeToString);
Note: If you are stuck on this solution while implementing, refer to this code repository to get a reference: https://github.com/testleaf-software/devtools-examples
To understand how this works — you need to deep dive the chrome developer tools API documentation and here it is: https://chromedevtools.github.io/devtools-protocol/tot/Network/#method-setExtraHTTPHeaders
3 After adding the above code snippet, if you invoke the URL of your application, the authentication will be sent by your chrome developer tools header and your application will pass through the authorization stage.
Thanks for taking the time to read this and hope this benefits you in your test automation career. I always learnt that finding the right solutions drive efficiency whereas workarounds will come back again like a nightmare.