Advance Selenium

Handle Authentication Pop Up in Selenium 4 using Chrome DevTools Protocols API

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.

 

Handle Authentication Pop Up in Selenium 4 using Chrome DevTools Protocols API

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

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

10 thoughts on “Handle Authentication Pop Up in Selenium 4 using Chrome DevTools Protocols API

  1. Sesh says:

    This approach works for Basic Authentication but fails for OAuth2 with PKCE. Do we have an approach for that,

    1. Hi Sesh,

      I’ll post for OAuth2 as well in the future. Please stay tuned 🙂

  2. kiran says:

    Any idea how to use this in JavaScript, some of the DevTools options are not available with JavaScript

    1. Will post for JS too.

  3. Pk says:

    Will it work if we are getting authentication popup mid of flow

    1. Hi Pk,

      It should work but it also depends on application behavior too.

  4. Sooraj says:

    Any idea how to use this in C#, some of the DevTools options are not available with C#

    1. Hi Sooraj, can you tell me which DevTools options are not available for C#?

      1. Sooraj says:

        DevTools chromeDevTools=driver.getDevTools();

        // Create sessions
        chromeDevTools.createSession(); is not available.

        c#:
        IDevTools devTools = driver as IDevTools;
        IDevToolsSession session= devTools.GetDevToolsSession();

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.