Testing web apps in Java using Playwright | Applitools Documentation
Quickstart for Visual Testing with Playwright in Java
This quickstart will show you how to visually test web apps in Java using Playwright. Visual testing can help you catch problems that traditional automation struggles to find.
After preparing your environment, this three-step quickstart should take about 15 minutes to complete.
Step 1: Preparing your environment
You'll need a few things to run this quickstart:
An Applitools account, which you can register for free.
The Java Development Kit (JDK), version 8 or higher. To install it, follow the JDK Installation Guide.
A good Java editor, such as:
Apache Maven for project builds and dependencies.
Installing Maven
If you use JetBrains IntelliJ IDEA, Eclipse IDE, or Apache NetBeans, then you do not need to install Maven separately because these IDEs come bundled with Maven.
Otherwise, you will need to install Maven and add it to the system PATH variable. Follow instructions on the Installing Apache Maven page. On macOS and Linux, you can install Maven with Homebrew by running brew install maven.
If you installed Maven separately from an IDE, then you can test that Maven is working by running the mvn -v command to print its version.
Step 2: Getting your example project
Downloading the example project
The example project is located at https://github.com/applitools/example-playwright-java. Clone this repository to your local machine:
git clone https://github.com/applitools/example-playwright-java.git
cd example-playwright-java
Don't have Git?
Instead of running git clone, you can download the project as a ZIP file and extract it.
Installing the dependencies
The example project uses Apache Maven for package management. All dependencies are listed in the project's pom.xml file. If you are using a Java IDE, then the IDE should install the dependencies and build the project for you. However, you may also install dependencies from the command line using the following command:
mvn install
Deciding how to run tests
There are two ways to test the visual snapshots captured by the test:
- Using Applitools Ultrafast Grid for cross-browser testing in the cloud
- Using Applitools Classic runner on your local machine
Walking through the code
The project contains one visual test case, which is located at src/test/java/com/applitools/example/AcmeBankTests.java.
package com.applitools.example;
import com.applitools.eyes.BatchInfo;
import com.applitools.eyes.EyesRunner;
import com.applitools.eyes.RectangleSize;
import com.applitools.eyes.TestResultsSummary;
import com.applitools.eyes.config.Configuration;
import com.applitools.eyes.playwright.ClassicRunner;
import com.applitools.eyes.playwright.Eyes;
import com.applitools.eyes.playwright.fluent.Target;
import com.applitools.eyes.visualgrid.BrowserType;
import com.applitools.eyes.visualgrid.model.DeviceName;
import com.applitools.eyes.visualgrid.model.ScreenOrientation;
import com.applitools.eyes.visualgrid.services.RunnerOptions;
import com.applitools.eyes.playwright.visualgrid.VisualGridRunner;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import org.junit.jupiter.api.*;
public class AcmeBankTests {
...
}
Step 3: Running your tests
Setting Applitools variables
Before running the visual test, you must find your Applitools API key and set it as an environment variable named APPLITOOLS_API_KEY.
Launching visual tests
The easiest way to launch this test is through a Java IDE. Just make sure to set the APPLITOOLS_API_KEY environment variable in your test's run configuration.
You can also launch this test from the command line using the following Maven command:
mvn test
When you run tests with the Applitools Ultrafast Grid, the Test Manager will show a separate result for each rendering. When you run tests with the Applitools Classic runner, the Test Manager will show the one snapshot from your local machine.
You can also change the web page to inject visual bugs:
page.navigate("https://demo.applitools.com/index_v2.html");
Taking the next steps with Applitools
Congratulations on completing this quickstart! There's still so much to learn about visual testing with Applitools, but you're off to a great start.
Resources for next steps:
Documentation Search