Skip to main content
  1. Articles/

Page Object Model with Playwright + Java

·4 mins

Woman with laptop

What is Playwright #

Playwright an end-to-end web automation tool is getting popular nowadays due to its advanced features such as auto-waiting of web elements before performing any action (click, fill, select, etc), testing in full isolation (incognito mode of browser), download and upload of files at ease, etc.

In this article, I have explained the core classes used to get started with the first Playwright script. Later I also explained how to create a framework using the Page Object Model and TestNG.

Playwright is an interface that provides a method to launch a browser instance (Chromium/Chrome/Firefox/WebKit).

Playwright playwright = Playwright.create();

A Browser is created via BrowserType.launch()

Browser browser= playwright.chromium().launch();

By default, Playwright launches the browser in headless mode. If you need to run in non-headless mode, you can disable headless mode by passing in a LaunchOption.

Browser browser =  playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));

BrowserContext provides a way to operate multiple independent browser sessions.

BrowserContext browserContext=browser.newContext();

Now that we have the browser, we need to launch the website under test. To do so, we need Page class which provides methods to interact with a single tab in a browser.

Page page= browserContext.newPage();

navigate() method of Page Class helps to redirect to the requested URL.


These steps help you launch the website under test which is nothing but a precondition to run any test in the framework. You may add all these details to a BaseTest and create test cases that should extend this class.

Let us start with the framework creation!

Framework creation #

For a Maven project, add the following dependencies:

  1. Create a package under src/main/java as “”. Under this package create a PlaywrightFactory class.
public class PlaywrightFactory {
Playwright playwright;
protected Page page;
Browser browser;
BrowserContext browserContext;
@Parameters({"appURL", "browserType"})
public Page getPage(String appURL, String browserType) {
playwright = Playwright.create();
switch (browserType) {
case "chrome":
browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setChannel("chrome").setHeadless(false));
case "chromium":
browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
case "firefox":
browser = playwright.firefox().launch(new BrowserType.LaunchOptions().setChannel("chrome").setHeadless(false));
case "safari":
browser = playwright.webkit().launch(new BrowserType.LaunchOptions().setChannel("chrome").setHeadless(false));
browserContext = browser.newContext();
page = browserContext.newPage();
return page;

This class takes 2 parameters from the testing.xml file as “appURL” and “browserType”. You may pass your requirement for these parameters in the testing.xml file.

  1. Create testng.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "">
<suite name="Suite">
<parameter name="appURL" value=""></parameter>
<parameter name="browserType" value="chrome"></parameter>
<test thread-count="5" name="Test">
<class name=""/>
</test> <!-- Test -->
</suite> <!-- Suite -->
  1. Create a package under src/main/java as “”. Under this package create a BaseTest class.
public class BaseTest {
protected Page page;
protected HomePage hp;
protected SearchPage search;
PlaywrightFactory play;
@Parameters({ "appURL", "browserType" })
public void setUp(String appURL, String browserType) {
play = new PlaywrightFactory();
page = play.getPage(appURL, browserType);
hp = new HomePage(page);
public void tearDown() {

This class has two annotations @BeforeClass and @AfterClass having methods setUp() and tearDown() respectively. setUp() method launches the desired webpage under test before any test class is executed. teardown() method closes the browser after all the methods for a test class are executed completely.

  1. Under “” package, create two classes HomePage and SearchPage.
public class HomePage{
Page page;
private String search="input#search_query_top";
public HomePage(Page page) {;
public SearchPage search(String searchTxt) {
return new SearchPage(page);

This class has page locators in the form of a String, required action methods, and a page constructor.

  1. Finally create a package under src/main/test as “”. Under this package create a PlayTest class.
public class PlayTest extends BaseTest {
@Test(priority = 1)
public void verifyTitle() {
Assert.assertEquals(page.title(), "My Store");
@Test(priority = 2)
public void verifySearch() {
search ="dress");
Assert.assertEquals(search.getSearchText().trim().toLowerCase(), "\"dress\"");

This is a test class having test methods and assertions. This class should extend BaseTest to call setUp() and tearDown() methods before running any @Test method.


Run as testng.xml file as TestNG suite and see the result.

To get the complete code refer to my GitHub repository.