In the past, we have seen different ways to handle authentication in Selenium using AutoIT, Robot Class, and sometimes even using Actions class as well right. In Selenium 4 we have CDP support which allows us the bypass these credentials.
If you are new to Chrome DevTools Protocol then I would recommend you to read official docs which will help you to understand in detail.
Steps to handle authentication In Selenium 4
1- getDevTools and create session
2- Enable network
3- Create a hashmap which will have our authentication as header
4- Add header as part of our request.
5- Continue with your test.
Note- Please inlcude Base64 from org.apache.commons.codec.binary.Base64 package
Sample Code to Handle authentication pop up in Selenium 4 using Chrome DevTools Protocol API.
package class13_map_selenium4features; import java.util.HashMap; import java.util.Map; import java.util.Optional; import org.apache.commons.codec.binary.Base64; import org.openqa.selenium.By; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.devtools.DevTools; import org.openqa.selenium.devtools.v95.network.Network; import org.openqa.selenium.devtools.v95.network.model.Headers; import io.github.bonigarcia.wdm.WebDriverManager; public class HandleBasicAuth { public static void main(String[] args) { // Use webdrivermanager to handle chrome browser driver WebDriverManager.chromedriver().setup(); // Start Chrome Browser ChromeDriver driver=new ChromeDriver(); // Get devTools DevTools chromeDevTools=driver.getDevTools(); // Create sessions chromeDevTools.createSession(); // Enable network chromeDevTools.send(Network.enable(Optional.of(0), Optional.of(0), Optional.of(0))); // Create hashmap for storing key value pair Map<String, Object> header=new HashMap<>(); // Create authentication string- please replace with your application username and password - in current case guest is username and password as well. String basicAuth ="Basic " + new String(new Base64().encode(String.format("%s:%s", "guest", "guest").getBytes())); // add Authorization as key and basicAuth as value header.put("Authorization", basicAuth); // add authentication as part of header chromeDevTools.send(Network.setExtraHTTPHeaders(new Headers(header))); // please replace this with your application url driver.get("https://jigsaw.w3.org/HTTP/"); // click on link and your request should be authenticated driver.findElement(By.linkText("Basic Authentication test")).click(); } }
You can also add in as part of your framework where you are invoking the browser. In my case I have added this as part of BrowserFactory class which maintain my browser session. Do not hardcode credentials in the code.
You can get username and password as part of parameter or you can take from config file which is nothing but properties file in Java.
I hope this article helped you if yes then please share with your friends.
Thanks
Mukesh Otwani
Sesh says
This approach works for Basic Authentication but fails for OAuth2 with PKCE. Do we have an approach for that,
Mukesh Otwani says
Hi Sesh,
I’ll post for OAuth2 as well in the future. Please stay tuned 🙂
kiran says
Any idea how to use this in JavaScript, some of the DevTools options are not available with JavaScript
Mukesh Otwani says
Will post for JS too.
Pk says
Will it work if we are getting authentication popup mid of flow
Mukesh Otwani says
Hi Pk,
It should work but it also depends on application behavior too.
Sooraj says
Any idea how to use this in C#, some of the DevTools options are not available with C#
Mukesh Otwani says
Hi Sooraj, can you tell me which DevTools options are not available for C#?
Sooraj says
DevTools chromeDevTools=driver.getDevTools();
// Create sessions
chromeDevTools.createSession(); is not available.
c#:
IDevTools devTools = driver as IDevTools;
IDevToolsSession session= devTools.GetDevToolsSession();
Mukesh Otwani says
Thanks Sooraj.