Skip to content

Commit aa3fd4e

Browse files
initial commit
1 parent dee3dc2 commit aa3fd4e

File tree

4 files changed

+285
-2
lines changed

4 files changed

+285
-2
lines changed

.github/workflows/test.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Run all tests
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
workflow_dispatch:
9+
10+
jobs:
11+
test:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Check out code
16+
uses: actions/checkout@v3
17+
18+
- name: Use java ${{ matrix.java-version }}
19+
uses: actions/setup-java@v3
20+
with:
21+
java-version: 17
22+
distribution: 'adopt'
23+
24+
- name: Execute tests
25+
env:
26+
APPLITOOLS_API_KEY: ${{ secrets.APPLITOOLS_API_KEY }}
27+
HEADLESS: true
28+
run: mvn clean install test

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,39 @@
1-
# example-playwright-java-ufg
2-
Applitools Example: Playwright Java with the Ultrafast Grid
1+
# Applitools Example: Playwright Java JUnit with the Ultrafast Grid
2+
3+
This is the example project for the [Playwright Java tutorial](https://applitools.com/tutorials/quickstart/web/playwright/java).
4+
It shows how to start automating visual tests
5+
with [Applitools Eyes](https://applitools.com/platform/eyes/)
6+
and the [Ultrafast Grid](https://applitools.com/platform/ultrafast-grid/)
7+
using [Playwright](https://playwright.dev/java) in Java.
8+
9+
It uses:
10+
11+
* [Java](https://www.java.com/) as the programming language
12+
* [Playwright](https://playwright.dev/java) for browser automation
13+
* [Google Chrome](https://www.google.com/chrome/downloads/) as the local browser for testing
14+
* [Apache Maven](https://maven.apache.org/index.html) for dependency management
15+
* [JUnit 5](https://junit.org/junit5/) as the core test framework
16+
* [Applitools Eyes](https://applitools.com/platform/eyes/) for visual testing
17+
* [Applitools Ultrafast Grid](https://applitools.com/platform/ultrafast-grid/) for cross-browser execution
18+
19+
To run this example project, you'll need:
20+
21+
1. An [Applitools account](https://auth.applitools.com/users/register), which you can register for free.
22+
2. The [Java Development Kit (JDK)](https://www.oracle.com/java/technologies/downloads/), version 8 or higher.
23+
3. A good Java editor, such as [JetBrains IntelliJ IDEA](https://www.jetbrains.com/idea/).
24+
4. [Apache Maven](https://maven.apache.org/download.cgi) (typically bundled with IDEs).
25+
5. An up-to-date version of [Google Chrome](https://www.google.com/chrome/downloads/).
26+
6. A corresponding version of [ChromeDriver](https://chromedriver.chromium.org/downloads).
27+
28+
The main test case is [`AcmeBankTests.java`](src/test/java/com/applitools/example/AcmeBankTests.java).
29+
30+
To execute tests, set the `APPLITOOLS_API_KEY` environment variable
31+
to your [account's API key](https://applitools.com/tutorials/guides/getting-started/registering-an-account),
32+
and then run:
33+
34+
```
35+
mvn test
36+
```
37+
38+
**For full instructions on running this project, take our
39+
[Playwright Java tutorial](https://applitools.com/tutorials/quickstart/web/playwright/java)!**

pom.xml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.applitools.example</groupId>
8+
<artifactId>example-playwright-java-ufg</artifactId>
9+
<version>1.0.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>1.8</maven.compiler.source>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
</properties>
15+
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.apache.maven.plugins</groupId>
20+
<artifactId>maven-surefire-plugin</artifactId>
21+
<version>3.0.0-M6</version>
22+
</plugin>
23+
</plugins>
24+
</build>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>com.applitools</groupId>
29+
<artifactId>eyes-playwright-java5</artifactId>
30+
<version>5.40.0</version>
31+
<scope>test</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.junit.jupiter</groupId>
35+
<artifactId>junit-jupiter</artifactId>
36+
<version>5.9.1</version>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.seleniumhq.selenium</groupId>
41+
<artifactId>selenium-java</artifactId>
42+
<version>4.6.0</version>
43+
<scope>test</scope>
44+
</dependency>
45+
</dependencies>
46+
47+
</project>
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package com.applitools.example;
2+
3+
import com.applitools.eyes.BatchInfo;
4+
import com.applitools.eyes.RectangleSize;
5+
import com.applitools.eyes.TestResultsSummary;
6+
import com.applitools.eyes.config.Configuration;
7+
import com.applitools.eyes.playwright.Eyes;
8+
import com.applitools.eyes.playwright.fluent.Target;
9+
import com.applitools.eyes.playwright.visualgrid.BrowserType;
10+
import com.applitools.eyes.visualgrid.model.DeviceName;
11+
import com.applitools.eyes.visualgrid.model.ScreenOrientation;
12+
import com.applitools.eyes.visualgrid.services.RunnerOptions;
13+
import com.applitools.eyes.playwright.visualgrid.VisualGridRunner;
14+
import com.microsoft.playwright.Browser;
15+
import com.microsoft.playwright.BrowserContext;
16+
import com.microsoft.playwright.Page;
17+
import com.microsoft.playwright.Playwright;
18+
import org.junit.jupiter.api.*;
19+
20+
import java.time.Duration;
21+
22+
public class AcmeBankTests {
23+
// This JUnit test case class contains everything needed to run a full visual test against the ACME bank site.
24+
// It runs the test once locally,
25+
// and then it performs cross-browser testing against multiple unique browsers in Applitools Ultrafast Grid.
26+
27+
// Test control inputs to read once and share for all tests
28+
private static String applitoolsApiKey;
29+
private static boolean headless;
30+
31+
// Applitools objects to share for all tests
32+
private static BatchInfo batch;
33+
private static Configuration config;
34+
private static VisualGridRunner runner;
35+
36+
// Test-specific objects
37+
private static Playwright playwright;
38+
private static Browser browser;
39+
private BrowserContext context;
40+
private Page page;
41+
private Eyes eyes;
42+
43+
@BeforeAll
44+
public static void setUpConfigAndRunner() {
45+
// This method sets up the configuration for running visual tests in the Ultrafast Grid.
46+
// The configuration is shared by all tests in a test suite, so it belongs in a `BeforeAll` method.
47+
// If you have more than one test class, then you should abstract this configuration to avoid duplication.
48+
49+
// Read the Applitools API key from an environment variable.
50+
applitoolsApiKey = System.getenv("APPLITOOLS_API_KEY");
51+
52+
// Read the headless mode setting from an environment variable.
53+
// Use headless mode for Continuous Integration (CI) execution.
54+
// Use headed mode for local development.
55+
headless = Boolean.parseBoolean(System.getenv().getOrDefault("HEADLESS", "true"));
56+
57+
// Create the runner for the Ultrafast Grid.
58+
// Concurrency refers to the number of visual checkpoints Applitools will perform in parallel.
59+
// Warning: If you have a free account, then concurrency will be limited to 1.
60+
runner = new VisualGridRunner(new RunnerOptions().testConcurrency(5));
61+
62+
// Create a new batch for tests.
63+
// A batch is the collection of visual checkpoints for a test suite.
64+
// Batches are displayed in the Eyes Test Manager, so use meaningful names.
65+
batch = new BatchInfo("Example: Playwright Java JUnit with the Ultrafast Grid");
66+
67+
// Create a configuration for Applitools Eyes.
68+
config = new Configuration();
69+
70+
// Set the Applitools API key so test results are uploaded to your account.
71+
// If you don't explicitly set the API key with this call,
72+
// then the SDK will automatically read the `APPLITOOLS_API_KEY` environment variable to fetch it.
73+
config.setApiKey(applitoolsApiKey);
74+
75+
// Set the batch for the config.
76+
config.setBatch(batch);
77+
78+
// Add 3 desktop browsers with different viewports for cross-browser testing in the Ultrafast Grid.
79+
// Other browsers are also available, like Edge and IE.
80+
config.addBrowser(800, 600, BrowserType.CHROME);
81+
config.addBrowser(1600, 1200, BrowserType.FIREFOX);
82+
config.addBrowser(1024, 768, BrowserType.SAFARI);
83+
84+
// Add 2 mobile emulation devices with different orientations for cross-browser testing in the Ultrafast Grid.
85+
// Other mobile devices are available, including iOS.
86+
config.addDeviceEmulation(DeviceName.Pixel_2, ScreenOrientation.PORTRAIT);
87+
config.addDeviceEmulation(DeviceName.Nexus_10, ScreenOrientation.LANDSCAPE);
88+
89+
// Start Playwright and launch the browser.
90+
playwright = Playwright.create();
91+
browser = playwright.chromium().launch(new com.microsoft.playwright.BrowserType.LaunchOptions().setHeadless(headless));
92+
}
93+
94+
@BeforeEach
95+
public void openBrowserAndEyes(TestInfo testInfo) {
96+
// This method sets up each test with its own Page and Applitools Eyes objects.
97+
98+
// Get a new context from the browser
99+
context = browser.newContext();
100+
101+
// Create a new page in the context.
102+
// Creating a new context is not mandatory and a new page can be created from the browser instance.
103+
// page = browser.newPage();
104+
page = context.newPage();
105+
106+
// Create the Applitools Eyes object connected to the VisualGridRunner and set its configuration.
107+
eyes = new Eyes(runner);
108+
eyes.setConfiguration(config);
109+
110+
// Open Eyes to start visual testing.
111+
// It is a recommended practice to set all four inputs:
112+
eyes.open(
113+
page, // Page object to "watch"
114+
"ACME Bank Web App", // The name of the app under test
115+
testInfo.getDisplayName(), // The name of the test case
116+
new RectangleSize(1024, 768)); // The viewport size for the local browser
117+
}
118+
119+
@Test
120+
public void logIntoBankAccount() {
121+
// This test covers login for the Applitools demo site, which is a dummy banking app.
122+
// The interactions use typical Selenium WebDriver calls,
123+
// but the verifications use one-line snapshot calls with Applitools Eyes.
124+
// If the page ever changes, then Applitools will detect the changes and highlight them in the Eyes Test Manager.
125+
// Traditional assertions that scrape the page for text values are not needed here.
126+
127+
// Load the login page.
128+
page.navigate("https://demo.applitools.com");
129+
130+
// Verify the full login page loaded correctly.
131+
eyes.check(Target.window().fully().withName("Login page"));
132+
133+
// Perform login.
134+
page.locator("#username").fill("andy");
135+
page.locator("#password").fill("i<3pandas");
136+
page.locator("#log-in").click();
137+
138+
// Verify the full main page loaded correctly.
139+
// This snapshot uses LAYOUT match level to avoid differences in closing time text.
140+
eyes.check(Target.window().fully().withName("Main page").layout());
141+
}
142+
143+
@AfterEach
144+
public void cleanUpTest() {
145+
146+
// Close Eyes to tell the server it should display the results.
147+
eyes.closeAsync();
148+
149+
// Close the page.
150+
page.close();
151+
152+
// Warning: `eyes.closeAsync()` will NOT wait for visual checkpoints to complete.
153+
// You will need to check the Eyes Test Manager for visual results per checkpoint.
154+
// Note that "unresolved" and "failed" visual checkpoints will not cause the JUnit test to fail.
155+
156+
// If you want the JUnit test to wait synchronously for all checkpoints to complete, then use `eyes.close()`.
157+
// If any checkpoints are unresolved or failed, then `eyes.close()` will make the JUnit test fail.
158+
}
159+
160+
@AfterAll
161+
public static void printResults() {
162+
163+
// Close the Playwright instance.
164+
playwright.close();
165+
166+
// Close the batch and report visual differences to the console.
167+
// Note that it forces JUnit to wait synchronously for all visual checkpoints to complete.
168+
TestResultsSummary allTestResults = runner.getAllTestResults();
169+
System.out.println(allTestResults);
170+
}
171+
}

0 commit comments

Comments
 (0)