When automating a login page with Playwright in Java, the process involves simulating the exact steps a real user would perform when logging into a web application. Here’s the conceptual flow:
1. Test Preparation
- Launch Browser: Use Playwright to start a browser instance (Chrome, Firefox, WebKit).
- Create Context and Page: A browser context is like a fresh user session, and a page represents a single browser tab. These are required to isolate the login test.
- Direct the browser to the application’s login URL.
- Ensure the page is fully loaded before proceeding (Playwright automatically waits for the page to be ready, but explicit waits can be used for synchronization).
- Locate Username Field: Identify the username/email input box using a selector (ID, name, placeholder, or CSS).
- Locate Password Field: Similarly, find the password input field.
- Enter Credentials: Simulate typing the username and password into their respective fields.
- Identify the login/submit button on the page.
- Simulate a user click on the button. Playwright ensures the element is ready for interaction before performing the click.
- After clicking login, the application might redirect to another page (like a dashboard).
- Playwright automatically waits for navigation, but you may also wait for a specific element on the home/dashboard page to confirm successful login.
- Positive Scenario: Verify that a unique element on the home page (e.g., a profile icon, welcome message, or logout button) is visible, confirming login success.
- Negative Scenario: Check for error messages (example: “Invalid username or password”) when wrong credentials are entered.
- Playwright allows capturing and storing cookies or session tokens. This can be reused in future tests to avoid repeated logins.
- Browser Context ensures each test runs in isolation, preventing session overlap.
- After test execution, close the page, context, and browser to free up resources.
- This ensures test environments remain clean and consistent.
Website to be automated:
https://opensource-demo.orangehrmlive.com/web/index.php/auth/login
Add maven dependency in pom.xml
:
<dependencies>
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.44.0</version>
</dependency>
</dependencies>
import com.microsoft.playwright.*;
public class OrangeHRMLoginTest {
public static void main(String[] args) {
// Step 1: Launch Playwright
try (Playwright playwright = Playwright.create()) {
// Step 2: Launch a browser (Chromium)
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
// Step 3: Create a new browser context
BrowserContext context = browser.newContext();
// Step 4: Open a new page
Page page = context.newPage();
// Step 5: Navigate to the login page
page.navigate("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
// Step 6: Wait for username input field to be visible
page.waitForSelector("input[name='username']");
// Step 7: Fill in the login credentials
page.fill("input[name='username']", "Admin");
page.fill("input[name='password']", "admin123");
// Step 8: Click the login button
page.click("button[type='submit']");
// Step 9: Print login success message
System.out.println("Login successful!");
// Optional: Close browser after verification
browser.close();
}
}
}
Line/Block | Explanation |
---|---|
Playwright.create() | Starts Playwright engine |
chromium().launch(...) | Launches Chromium browser (can be firefox() or webkit() too) |
browser.newContext() | Creates a new isolated browser context (like incognito) |
context.newPage() | Opens a new browser tab/page |
page.navigate(url) | Opens the specified URL |
page.fill(selector, value) | Enters value into input field (selector is a CSS locator) |
page.click(selector) | Clicks the element (e.g., login button) |
browser.close() | Closes the browser at the end |
For this demo:
- Username: Admin
- Password: admin123
- This script assumes that the page loads fast enough, but for real-world tests, you should use explicit waits where needed.
- You can run this from a main method or integrate it into a test framework like JUnit/TestNG for proper test cases.