Playwright Java Tutorial: Web Automation Testing | Installation and Setup

Mohammad Faisal Khatri
3 min readOct 16, 2023

--

Playwright as everyone knows is a web automation framework developed by Microsoft. It is open source and available to use for free. It is available across multiple programming languages like Java, NodeJS, Python, and .Net.

It has multiple features like support for Cross browser, Cross Platform and Cross language. It can also be used to perform mobile web testing by using the native emulation of Google Chrome for Android and Mobile Safari.

There are multiple features in Playwright that help in removing flakiness from the tests. The first one is its ability to perform Auto-Wait for elements to be actionable prior to performing actions. The next one is the web first assertion that retries the checks until the necessary conditions are met.

Playwright also has the tracing features like capture execution trace, videos, screenshots, etc.

In this tutorial blog, we will be learning about creating and setting up the project using Playwright Java.

Prerequisites

The following are the prerequisites and needs to be installed on the machine to start with smooth setup and installation.

  1. Java JDK 17
  2. IntelliJ IDE or any other IDE to create project and start writing automated tests
  3. Maven
  4. Browsers on which tests need to be run like Chrome, Firefox, etc.

Creating a new Project

The first step in setup is to create a new Maven project. I will be using IntelliJ in this tutorial. The following steps needs to be followed to create a new Maven project :

  1. Open IntelliJ, Navigate to File >> New >> Project

2. In the New Project window enter the following details:

  • Name of the Project
  • Location/path where the project needs to be saved
  • Select JDK version — I am using JDK 17
  • Archetype — Search for “quickstart” and select maven-archetype-quickstart from the result
  • Click on Create button to create the project

With this, we have created the Maven project successfully.

If you are new to IntelliJ, checkout the following video that demonstrates how to create a new Maven project :

Updating the Dependencies

After the project is created successfully, we need to add the dependencies and plugins for the following in the pom.xml:

  1. Playwright — java
  2. TestNG
  3. Maven Surefire plugin
  4. Maven Compiler plugin

FileName: pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.github.mfaisalkhatri</groupId>
<artifactId>web-automation-playwright-java</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>web-automation-playwright-java</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<playwright.version>1.38.0</playwright.version>
<testng.version>7.8.0</testng.version>
<maven.compiler.version>3.11.0</maven.compiler.version>
<surefire.version>3.1.2</surefire.version>
<java.release.version>17</java.release.version>
<maven.source.encoding>UTF-8</maven.source.encoding>
<suite-xml>test-suites/testng.xml</suite-xml>
</properties>

<dependencies>
<!-- https://mvnrepository.com/artifact/com.microsoft.playwright/playwright -->
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>${playwright.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<release>${java.release.version}</release>
<encoding>${maven.source.encoding}</encoding>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
</properties>
<suiteXmlFiles>
<suiteXmlFile>${suite-xml}</suiteXmlFile>
</suiteXmlFiles>
<argLine>${argLine}</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>

Updating the dependency versions in the Properties block is a best practice and helps in checking and updating the maven versions in the project easily.

Congratulations! we have configured the project successfully to start with web automation using Playwright Java.

Conclusion

The project setup and configuration was a piece of cake as we used Maven to create the project and updated the required dependency for Playwright Java and TestNG in pom.xml file.

In the next tutorial, we will be writing the first test script using Playwright Java and running the tests.

--

--

Mohammad Faisal Khatri

QA with 14+ years of experience in automation as well as manual testing. Freelancer, blogger and open source contributor.