-
Notifications
You must be signed in to change notification settings - Fork 10
java/playwright examples updated #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
padmavemulapati
wants to merge
3
commits into
main
Choose a base branch
from
Java_PlaywrightExamples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+613
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <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>com.deque.watcher_examples.playwright</groupId> | ||
| <artifactId>basic</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
|
|
||
| <properties> | ||
| <maven.compiler.source>17</maven.compiler.source> | ||
| <maven.compiler.target>17</maven.compiler.target> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <skip.browser.install>false</skip.browser.install> | ||
| </properties> | ||
|
Comment on lines
+11
to
+16
|
||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.microsoft.playwright</groupId> | ||
| <artifactId>playwright</artifactId> | ||
| <version>1.49.0</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-api</artifactId> | ||
| <version>5.10.2</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-engine</artifactId> | ||
| <version>5.10.2</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.deque.axe_core</groupId> | ||
| <artifactId>watcher</artifactId> | ||
| <version>4.4.0</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <version>3.5.4</version> | ||
| </plugin> | ||
| <!-- Install Playwright browsers before tests (forked so the CLI's System.exit() won't kill Maven) --> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-antrun-plugin</artifactId> | ||
| <version>3.1.0</version> | ||
| <executions> | ||
| <execution> | ||
| <id>install-playwright-browsers</id> | ||
| <phase>process-test-classes</phase> | ||
| <goals> | ||
| <goal>run</goal> | ||
| </goals> | ||
| <configuration> | ||
| <target unless="skip.browser.install"> | ||
| <java classname="com.microsoft.playwright.CLI" | ||
| classpathref="maven.test.classpath" | ||
| fork="true" | ||
| failonerror="true"> | ||
| <arg value="install"/> | ||
| <arg value="chromium"/> | ||
| </java> | ||
| </target> | ||
| </configuration> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> | ||
7 changes: 7 additions & 0 deletions
7
java/playwright/basic/src/main/java/com/deque/watcher_examples/playwright/Basic.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.deque.watcher_examples.playwright; | ||
|
|
||
| public class Basic { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| } | ||
| } |
82 changes: 82 additions & 0 deletions
82
java/playwright/basic/src/test/java/com/deque/watcher_examples/playwright/BasicTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package com.deque.watcher_examples.playwright; | ||
|
|
||
| import java.nio.file.Paths; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import com.deque.axe_core.commons.AxeWatcherOptions; | ||
| import com.deque.axe_core.playwright.AxeWatcherPage; | ||
| import com.deque.axe_core.playwright.AxeWatcherPlaywright; | ||
| import com.microsoft.playwright.BrowserContext; | ||
| import com.microsoft.playwright.BrowserType; | ||
| import com.microsoft.playwright.ElementHandle; | ||
| import com.microsoft.playwright.Playwright; | ||
|
|
||
|
|
||
| @DisplayName("My Login Application") | ||
| class BasicTest { | ||
|
|
||
| String apiKey = "test-api-key"; | ||
| String projectId = "test-project-id"; | ||
| String serverUrl = "https://axe.deque.com"; | ||
|
|
||
| Playwright playwright; | ||
| BrowserContext browserContext; | ||
| AxeWatcherPage page; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| AxeWatcherPlaywright axeWatcher = | ||
| new AxeWatcherPlaywright( | ||
| new AxeWatcherOptions().setApiKey(apiKey).setProjectId(projectId).setServerUrl(serverUrl)) | ||
| .enableDebugLogger(); | ||
|
|
||
| BrowserType.LaunchPersistentContextOptions launchOptions = | ||
| axeWatcher.configure( | ||
| new BrowserType.LaunchPersistentContextOptions() | ||
| .setHeadless(true) | ||
| .setChannel("chromium") | ||
| .setArgs(List.of("--no-sandbox", "--disable-dev-shm-usage"))); | ||
|
|
||
| playwright = Playwright.create(); | ||
| // An empty user-data dir lets Playwright create a fresh temporary profile. | ||
| browserContext = playwright.chromium().launchPersistentContext(Paths.get(""), launchOptions); | ||
|
Comment on lines
+49
to
+50
|
||
| page = axeWatcher.wrapPage(browserContext.newPage()); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| page.axeWatcher().flush(); | ||
| browserContext.close(); | ||
| playwright.close(); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("Login") | ||
| class LoginTests { | ||
| @Nested | ||
| @DisplayName("with valid credentials") | ||
| class ShouldLoginTests { | ||
| @Test | ||
| @DisplayName("should login") | ||
| void shouldLoginTest() { | ||
| page.navigate("https://the-internet.herokuapp.com/login"); | ||
|
|
||
| page.locator("#username").fill("tomsmith"); | ||
| page.locator("#password").fill("SuperSecretPassword!"); | ||
|
|
||
| page.locator("button[type='submit']").click(); | ||
|
|
||
| ElementHandle element = page.waitForSelector("#flash"); | ||
| assertNotNull(element); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <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>com.deque.watcher_examples.playwright</groupId> | ||
| <artifactId>manual-mode</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
|
|
||
| <properties> | ||
| <maven.compiler.source>17</maven.compiler.source> | ||
| <maven.compiler.target>17</maven.compiler.target> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <skip.browser.install>false</skip.browser.install> | ||
| </properties> | ||
|
Comment on lines
+11
to
+16
|
||
|
|
||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.microsoft.playwright</groupId> | ||
| <artifactId>playwright</artifactId> | ||
| <version>1.49.0</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-api</artifactId> | ||
| <version>5.10.2</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-engine</artifactId> | ||
| <version>5.10.2</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.deque.axe_core</groupId> | ||
| <artifactId>watcher</artifactId> | ||
| <version>4.4.0</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <version>3.5.4</version> | ||
| </plugin> | ||
| <!-- Install Playwright browsers before tests (forked so the CLI's System.exit() won't kill Maven) --> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-antrun-plugin</artifactId> | ||
| <version>3.1.0</version> | ||
| <executions> | ||
| <execution> | ||
| <id>install-playwright-browsers</id> | ||
| <phase>process-test-classes</phase> | ||
| <goals> | ||
| <goal>run</goal> | ||
| </goals> | ||
| <configuration> | ||
| <target unless="skip.browser.install"> | ||
| <java classname="com.microsoft.playwright.CLI" | ||
| classpathref="maven.test.classpath" | ||
| fork="true" | ||
| failonerror="true"> | ||
| <arg value="install"/> | ||
| <arg value="chromium"/> | ||
| </java> | ||
| </target> | ||
| </configuration> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> | ||
7 changes: 7 additions & 0 deletions
7
...laywright/manual-mode/src/main/java/com/deque/watcher_examples/playwright/ManualMode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.deque.watcher_examples.playwright; | ||
|
|
||
| public class ManualMode { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| } | ||
| } |
112 changes: 112 additions & 0 deletions
112
...right/manual-mode/src/test/java/com/deque/watcher_examples/playwright/ManualModeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package com.deque.watcher_examples.playwright; | ||
|
|
||
| import java.nio.file.Paths; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import com.deque.axe_core.commons.AxeWatcherOptions; | ||
| import com.deque.axe_core.playwright.AxeWatcherPage; | ||
| import com.deque.axe_core.playwright.AxeWatcherPlaywright; | ||
| import com.microsoft.playwright.BrowserContext; | ||
| import com.microsoft.playwright.BrowserType; | ||
| import com.microsoft.playwright.ElementHandle; | ||
| import com.microsoft.playwright.Playwright; | ||
|
|
||
|
|
||
| @DisplayName("My Login Application") | ||
| class ManualModeTest { | ||
|
|
||
| String apiKey = "test-api-key"; | ||
| String projectId = "test-project-id"; | ||
| String serverUrl = "https://axe.deque.com"; | ||
|
|
||
| Playwright playwright; | ||
| BrowserContext browserContext; | ||
| AxeWatcherPage page; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| AxeWatcherPlaywright axeWatcher = | ||
| new AxeWatcherPlaywright( | ||
| new AxeWatcherOptions() | ||
| .setApiKey(apiKey) | ||
| .setProjectId(projectId) | ||
| .setServerUrl(serverUrl) | ||
| // Disable automatic analysis. | ||
| .setAutoAnalyze(false)) | ||
| .enableDebugLogger(); | ||
|
|
||
| BrowserType.LaunchPersistentContextOptions launchOptions = | ||
| axeWatcher.configure( | ||
| new BrowserType.LaunchPersistentContextOptions() | ||
| .setHeadless(true) | ||
| .setChannel("chromium") | ||
| .setArgs(List.of("--no-sandbox", "--disable-dev-shm-usage"))); | ||
|
|
||
| playwright = Playwright.create(); | ||
| browserContext = playwright.chromium().launchPersistentContext(Paths.get(""), launchOptions); | ||
|
|
||
| page = axeWatcher.wrapPage(browserContext.newPage()); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| // Flush axe-watcher results after each test. | ||
| page.axeWatcher().flush(); | ||
| browserContext.close(); | ||
| playwright.close(); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("Login") | ||
| class LoginTests { | ||
| @Nested | ||
| @DisplayName("with valid credentials") | ||
| class ShouldLoginTests { | ||
| @Test | ||
| @DisplayName("should login") | ||
| void shouldLoginTest() { | ||
| /* | ||
| Let's calculate the number of page states. | ||
| Auto-analyze is false, so it will not analyze automatically. | ||
|
|
||
| We navigate to the page, | ||
| then manually analyze it. (+1) | ||
| We fill out the form, | ||
| then turn on auto-analysis. | ||
| We click the button, | ||
| causing an auto-analysis (+1), | ||
| then assure the element appears. | ||
|
|
||
| We turn off auto-analysis, | ||
| then analyze manually (+1). | ||
|
|
||
| So, we expect the total number of page states to be 3. | ||
| */ | ||
| page.navigate("https://the-internet.herokuapp.com/login"); | ||
| // Analyze after navigating to the page. | ||
| page.axeWatcher().analyze(); | ||
|
|
||
| page.locator("#username").fill("tomsmith"); | ||
| page.locator("#password").fill("SuperSecretPassword!"); | ||
| // Start automatic axe analysis. | ||
| page.axeWatcher().start(); | ||
|
|
||
| page.locator("button[type='submit']").click(); | ||
|
|
||
| ElementHandle element = page.waitForSelector("#flash"); | ||
| assertNotNull(element); | ||
|
|
||
| // Stop automatic axe analysis. | ||
| page.axeWatcher().stop(); | ||
| // Analyze after logging in. | ||
| page.axeWatcher().analyze(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be touched in a PR like this. All PRs should have descriptions.