Showing posts with label First Playwright Script. Show all posts
Showing posts with label First Playwright Script. Show all posts

First Playwright Script in Java


























How to Write Your First Playwright Script in Java

Automation testing has become an essential skill for modern QA engineers. With Java, we can automate browser actions such as opening a website, clicking buttons, filling forms, submitting data, and validating results.

Playwright is a powerful automation framework developed by Microsoft that allows us to control modern web browsers programmatically. It supports multiple browsers including:

  • Chromium (Chrome, Edge)

  • Firefox

  • WebKit (Safari)

In this guide, we will create our first Playwright script using Java.


Important Things to Know Before Starting

Before writing your first script, you should understand a few basic concepts:

  • Playwright was originally built on Node.js, but it provides official support for Java.

  • It allows interaction with web elements like buttons, text fields, dropdowns, and links.

  • It is widely used for UI testing and end-to-end automation.

  • It supports cross-browser testing with the same code base.

If you are familiar with Selenium, you will find Playwright easier in terms of auto-waiting and browser management.


Steps to Create Your First Playwright Script in Java

Playwright automation follows five simple steps.


Step 1: Set Up the Environment

Before writing any code:

  • Install Java (JDK 11 or above)

  • Install Maven or Gradle

  • Create a new Java project

  • Add Playwright dependency

Playwright will automatically download the required browser binaries when you run the project for the first time.


Maven Dependency

Add the following dependency in your pom.xml file:

<dependencies> <dependency> <groupId>com.microsoft.playwright</groupId> <artifactId>playwright</artifactId> <version>1.45.0</version> </dependency> </dependencies>

Make sure you use the latest stable version available.


Step 2: Launch the Browser

The first thing in automation is launching a browser instance.

Playwright allows you to:

  • Run in headless mode (faster, no browser UI)

  • Run in headed mode (browser UI visible)

For learning purposes, headed mode is recommended.


Step 3: Create a Browser Context

A BrowserContext works like an incognito window.

Each context:

  • Has separate cookies

  • Has separate local storage

  • Is completely isolated

This helps in running multiple tests independently.


Step 4: Create a Page and Navigate

A Page represents a single browser tab.

Once the page is created, you can navigate to any website and begin interacting with it.


Step 5: Interact with Web Elements

Playwright supports multiple selector strategies such as:

  • ID

  • CSS

  • Text

  • XPath

You can perform actions like:

  • click()

  • fill()

  • selectOption()

  • take screenshots

  • hover

  • wait for elements


Step 6: Close the Browser

After completing your test steps, always close the browser.

This ensures proper cleanup of resources and prevents memory leaks.


First Playwright Script in Java

Below is a simple example. This script:

import com.microsoft.playwright.*; public class FirstPlaywrightTest { public static void main(String[] args) { try (Playwright playwright = Playwright.create()) { // Launch Chromium browser in headed mode Browser browser = playwright.chromium().launch( new BrowserType.LaunchOptions().setHeadless(false) ); // Create a new browser context BrowserContext context = browser.newContext(); // Open a new page Page page = context.newPage(); // Navigate to website page.navigate("https://example.com"); // Get page title String title = page.title(); System.out.println("Page title is: " + title); // Close browser browser.close(); } } }


Code Explanation

LineExplanation
Playwright.create()Initializes the Playwright engine
playwright.chromium().launch()Launches Chromium browser
browser.newContext()Creates isolated browser session
context.newPage()Opens new browser tab
page.navigate()Loads the website
page.title()Returns the page title
browser.close()Closes browser instance


How to Run in Eclipse

  1. Right-click on the Java class

  2. Select Run As

  3. Click Java Application

The browser will open, navigate to the website, and print the title in the console.


Best Practices for Beginners

  • Always use try-with-resources for Playwright

  • Avoid using Thread.sleep()

  • Use Playwright’s built-in waiting mechanism

  • Keep headless mode enabled in CI/CD environments

  • Always close browser after execution


Conclusion

Writing your first Playwright script in Java is straightforward once you understand the flow:

  1. Initialize Playwright

  2. Launch browser

  3. Create context

  4. Open page

  5. Perform actions

  6. Close browser

These basic steps form the foundation of any advanced Playwright automation framework.

Once you are comfortable with this structure, you can move forward to handling alerts, dropdowns, iframes, and building a complete automation framework.