How to handle Http popup authentication in Playwright Java?
Nov 27, 2023
How can I handle this kind of login?
For this, we need to use the “context” , below is the code.
// Test method to demonstrate HTTP popup authentication handling.
@Test
public void navigation() throws InterruptedException {
// Define username and password credentials for authentication
String username = "admin";
String password = "admin";
// Create a Playwright instance
Playwright playwright = Playwright.create();
// Launch a Chromium browser in non-headless mode
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
// Create a Browser Context with HTTP credentials
BrowserContext context = browser.newContext(new Browser.NewContextOptions().setHttpCredentials(username, password));
// Create a new Page within the Browser Context
Page page = context.newPage();
// Navigate to the protected URL requiring authentication
page.navigate("https://the-internet.herokuapp.com/digest_auth");
// Introduce a delay to observe the authentication popup handling
Thread.sleep(6000);
}