• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / Handle Authentication Pop Up in Selenium 4 using Chrome DevTools Protocols API

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

March 9, 2022 by Mukesh Otwani 10 Comments

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

Filed Under: Advance Selenium Tagged With: Selenium 4 Authentication, Selenium 4 CDP

Reader Interactions

Comments

  1. Sesh says

    August 25, 2022 at 5:55 PM

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

    Reply
    • Mukesh Otwani says

      August 26, 2022 at 10:31 PM

      Hi Sesh,

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

      Reply
  2. kiran says

    July 14, 2022 at 2:25 PM

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

    Reply
    • Mukesh Otwani says

      July 22, 2022 at 1:08 PM

      Will post for JS too.

      Reply
  3. Pk says

    March 11, 2022 at 7:57 AM

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

    Reply
    • Mukesh Otwani says

      March 14, 2022 at 10:39 PM

      Hi Pk,

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

      Reply
  4. Sooraj says

    March 10, 2022 at 12:35 AM

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

    Reply
    • Mukesh Otwani says

      March 10, 2022 at 1:06 PM

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

      Reply
      • Sooraj says

        March 10, 2022 at 10:16 PM

        DevTools chromeDevTools=driver.getDevTools();

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

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

        Reply
        • Mukesh Otwani says

          March 17, 2022 at 12:17 PM

          Thanks Sooraj.

          Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • Syllabus For Playwright Online Training Program
  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?