Showing posts with label Basic Authentication in Playwright. Show all posts
Showing posts with label Basic Authentication in Playwright. Show all posts

Test Basic Authentication in API by Playwright




What is Basic authentication?

Basic Authentication is the process to verify a user by means of user's login credentials, it can be username and password. 


How Playwright Java Handles basic authentication?

Playwright provides a straightforward way to handle Basic Authentication by passing credentials when creating a BrowserContext.

  • A BrowserContext is like an isolated browser profile.
  • While creating the context, Playwright allows you to define HTTP credentials (username and password).
  • Once configured, every request made within that context automatically includes the necessary Authorization header, so you don’t need to manually handle it on each request.

Advantages of This Approach

  • No need to manually add headers for each request.
  • Works across all pages, frames, and API calls within the same context.
  • Ensures a secure and centralized way of handling credentials.















Steps:

  • Set up Playwright.
  • Create a new browser context with HTTP credentials.
  • Navigate to a website that requires basic authentication.
  • Assert the successful authentication.

We’ll use the website:
https://httpbin.org/basic-auth/user/passwd











It requires basic auth with:

  • Username: user

  • Password: passwd




Playwright Java Code Example:

import com.microsoft.playwright.*;

public class BasicAuthTest {
    public static void main(String[] args) {
        try (Playwright playwright = Playwright.create()) {
            // Create a browser
            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));

            // Set up HTTP Basic Auth credentials
            Browser.NewContextOptions contextOptions = new Browser.NewContextOptions()
                    .setHttpCredentials("user", "passwd");  // username: user, password: passwd

            // Create browser context with credentials
            BrowserContext context = browser.newContext(contextOptions);

            // Open a new page
            Page page = context.newPage();

            // Navigate to the Basic Auth test URL
            page.navigate("https://httpbin.org/basic-auth/user/passwd");

            // Verify if authentication is successful
            String bodyText = page.textContent("body");
            if (bodyText.contains("\"authenticated\": true")) {
                System.out.println("Basic authentication successful!");
            } else {
                System.out.println("Basic authentication failed.");
            }

            // Close browser
            browser.close();
        }
    }
}



Code explanation:

(a) After creating Browser object, we have to setup HTTP basic auth credentials by setHttpCredentials()
(b) Create browser context with credentials
(c) Create Page object
(d) Navigate to the URL
(e) Verify if authentication is successful



Output:


Basic authentication successful!




Maven Dependencies:

<dependency>
  <groupId>com.microsoft.playwright</groupId>
  <artifactId>playwright</artifactId>
  <version>1.44.0</version>
</dependency>