Learn about Browser based Authentication, newly introduced feature in Selenium 4 (BiDirectional API)

Browser based Authentication,​​ newly introduced​​ feature​​ in Selenium​​ 4​​ (BiDirectional API)

Let us​​ go to the website​​ https://the-internet.herokuapp.com/​​ 


Click ‘Basic Auth’ link that you see above.​​ 

Notice below that a ‘Sign in’ popup comes up asking user to enter username/password credentials.​​ 

This is a typical example of​​ Browser based Authentication. The reason we call it a browser based authentication is because​​ this authentication is generated by the browser​​ 

Enter username and password as ‘admin’

Click ‘Sign in’.​​ 

Notice below that the browser has authenticated the user

 

Let us see application based authentication now. Launch the application​​ https://www.way2automation.com/​​ 

You would see ‘Member Login’ link as shown below​​ 

Let us click this link. Notice below that the below authentication is generated by the application. It is NOT generated by browser


Let us see how to automate​​ 
Browser based Authentication. Selenium 4 has introduced BiDirectional API to accomplish this. To understand this, go to official documentation of Selenium​​ https://www.selenium.dev/documentation/webdriver/bidirectional/bidi_api/​​ 

Look at the below section

 

Just copy the code snippet that is shown above

Paste it in eclipse ide

Import HasAuthentication, UsernameAndPassword

Import ‘Predicate’ from java.util.function

Import ‘URI’ from java.net.URI

Next, to resolve the lambda expression error, click ‘Change project compliance and JRE to 1.8’

Save. There should not be any error now as shown below​​ 

On line number 17 that you see above, replace​​ your-domain.com​​ with​​ the-internet.herokuapp.com

On line number 20, replace​​ https://your-domain.com/login​​ with​​ https://the-internet.herokuapp.com/basic_auth

On line number 19, replace​​ password​​ with​​ admin

The reason being, both username and password is admin as shown below

Save the script and run. Notice below that browser based authentication is successful​​ 

Code snippet

package​​ sel4scripts;

 

import​​ java.net.URI;

import​​ java.util.function.Predicate;

import​​ org.openqa.selenium.HasAuthentication;

import​​ org.openqa.selenium.UsernameAndPassword;

import​​ org.openqa.selenium.WebDriver;

import​​ org.openqa.selenium.chrome.ChromeDriver;

import​​ io.github.bonigarcia.wdm.WebDriverManager;

 

public​​ class​​ BrowserBasedAuth {

 

public​​ static​​ void​​ main(String[]​​ args) {

 WebDriverManager.chromedriver().setup();

 WebDriver​​ driver​​ =​​ new​​ ChromeDriver();

 Predicate<URI>​​ uriPredicate​​ =​​ uri​​ ->​​ uri.getHost().contains("the-internet.herokuapp.com");

 

 ((HasAuthentication)​​ driver).register(uriPredicate, UsernameAndPassword.of("admin",​​ "admin"));

driver.get("https://the-internet.herokuapp.com/basic_auth");

 }

 

}

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

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

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 1st Nov 2024