-
Notifications
You must be signed in to change notification settings - Fork 19
chore(): add experimental support for playwright java #166
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
Sid200026
wants to merge
2
commits into
main
Choose a base branch
from
users/ssingharoy/mpt-java-experimental
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.
Open
Changes from all commits
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -348,3 +348,6 @@ MigrationBackup/ | |
|
||
# Ionide (cross platform F# VS Code tools) working folder | ||
.ionide/ | ||
|
||
# Java | ||
target/ |
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,60 @@ | ||
# Run Playwright Java tests with Microsoft Playwright Testing (Experimental) | ||
|
||
This sample demonstrates how to run Playwright Java tests at scale using Microsoft Playwright Testing. It showcases the benefits of accelerating test suite completion by leveraging more parallel cloud browsers. | ||
|
||
**Note** - You won't be able to get the support for reporting and authentication to the service from the client running the tests via Entra ID. | ||
|
||
If you have not yet created a workspace, please follow the [Get Started guide](../../../README.md#get-started) | ||
|
||
## Sample setup | ||
|
||
1. Clone this sample: | ||
|
||
```bash | ||
git clone https://github.com/microsoft/playwright-testing-service | ||
cd playwright-testing-service/samples/experimental/java | ||
``` | ||
|
||
1. Install dependencies: | ||
|
||
```bash | ||
mvn install | ||
``` | ||
|
||
1. Set up Authentication using Access Tokens: | ||
|
||
Currently, only access tokens are supported for Java. See [Set up authentication using access tokens](../../../../README.md#set-up-authentication-using-access-tokens) | ||
|
||
1. Set access token generated above as environment variable for your project: | ||
|
||
```bash | ||
PLAYWRIGHT_SERVICE_ACCESS_TOKEN= # Paste Access Token value from previous step | ||
``` | ||
|
||
1. Set up environment: | ||
|
||
In the [Playwright portal](https://aka.ms/mpt/portal), copy the command under **Add region endpoint in your set up** and set the following environment variable: | ||
|
||
```bash | ||
PLAYWRIGHT_SERVICE_URL= # Paste region endpoint URL | ||
``` | ||
|
||
## Run tests | ||
|
||
Run Playwright tests against browsers managed by the service using the configuration you created above. You can run up to 100 parallel workers with the service | ||
|
||
```bash | ||
mvn compile exec:java -D exec.mainClass="org.example.App" | ||
``` | ||
|
||
## Add more configuration | ||
|
||
You can use the following environment variables to specify configuration parameters for the service: | ||
|
||
1. **PLAYWRIGHT_SERVICE_RUN_ID**: This variable allows you to change the ID of the test run. The run ID is a unique identifier for a test run and is used to track test runs in the portal. | ||
|
||
Example : | ||
|
||
```bash | ||
export PLAYWRIGHT_SERVICE_RUN_ID = "my_custom_runId" | ||
``` |
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,42 @@ | ||
<?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>org.example</groupId> | ||
<artifactId>examples</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>Playwright Client - Microsoft Playwright Testing Integration Examples</name> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.microsoft.playwright</groupId> | ||
<artifactId>playwright</artifactId> | ||
<version>1.49.0</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.12.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<repositories> | ||
<repository> | ||
<id>snapshots-repo</id> | ||
<url>https://oss.sonatype.org/content/repositories/snapshots</url> | ||
<releases><enabled>false</enabled></releases> | ||
<snapshots><enabled>true</enabled></snapshots> | ||
</repository> | ||
</repositories> | ||
</project> |
40 changes: 40 additions & 0 deletions
40
samples/experimental/java/src/main/java/org/example/App.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,40 @@ | ||
package org.example; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
import org.example.PlaywrightService.BrowserConnectOptions; | ||
|
||
import com.microsoft.playwright.Browser; | ||
import com.microsoft.playwright.Locator; | ||
import com.microsoft.playwright.Page; | ||
import com.microsoft.playwright.Playwright; | ||
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; | ||
import com.microsoft.playwright.options.AriaRole; | ||
|
||
public class App { | ||
public static void main(String[] args) { | ||
try (Playwright playwright = Playwright.create()) { | ||
BrowserConnectOptions browserConnectOptions = PlaywrightService.getConnectOptions(); | ||
Browser browser = playwright.chromium().connect(browserConnectOptions.wsEndpoint, browserConnectOptions.connectOptions); | ||
|
||
Page page = browser.newPage(); | ||
page.navigate("http://playwright.dev"); | ||
|
||
// Expect a title "to contain" a substring. | ||
assertThat(page).hasTitle(Pattern.compile("Playwright")); | ||
|
||
// create a locator | ||
Locator getStarted = page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("Get Started")); | ||
|
||
// Expect an attribute "to be strictly equal" to the value. | ||
assertThat(getStarted).hasAttribute("href", "/docs/intro"); | ||
|
||
// Click the get started link. | ||
getStarted.click(); | ||
|
||
// Expects page to have a heading with the name of Installation. | ||
assertThat(page.getByRole(AriaRole.HEADING, | ||
new Page.GetByRoleOptions().setName("Installation"))).isVisible(); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
samples/experimental/java/src/main/java/org/example/PlaywrightService.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,50 @@ | ||
package org.example; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import com.microsoft.playwright.BrowserType.ConnectOptions; | ||
|
||
public class PlaywrightService { | ||
public static class BrowserConnectOptions { | ||
public final String wsEndpoint; | ||
public final ConnectOptions connectOptions; | ||
|
||
public BrowserConnectOptions(String wsEndpoint, ConnectOptions connectOptions) { | ||
this.wsEndpoint = wsEndpoint; | ||
this.connectOptions = connectOptions; | ||
} | ||
} | ||
|
||
public static BrowserConnectOptions getConnectOptions() { | ||
return PlaywrightService.getConnectOptions("linux"); | ||
} | ||
|
||
public static BrowserConnectOptions getConnectOptions(String osName) { | ||
return PlaywrightService.getConnectOptions(osName, ""); | ||
} | ||
|
||
public static BrowserConnectOptions getConnectOptions(String osName, String runId) { | ||
String playwrightRunId = System.getenv("PLAYWRIGHT_SERVICE_RUN_ID"); | ||
if (runId != null && !runId.isEmpty()) { | ||
System.setProperty("PLAYWRIGHT_SERVICE_RUN_ID", runId); | ||
} | ||
if (playwrightRunId == null || playwrightRunId.isEmpty()) { | ||
playwrightRunId = UUID.randomUUID().toString(); | ||
System.setProperty("PLAYWRIGHT_SERVICE_RUN_ID", playwrightRunId); | ||
} | ||
|
||
String serviceUrl = System.getenv("PLAYWRIGHT_SERVICE_URL"); | ||
String serviceAccessToken = System.getenv("PLAYWRIGHT_SERVICE_ACCESS_TOKEN"); | ||
|
||
Map<String, String> headers = new HashMap<>(); | ||
headers.put("Authorization", "Bearer " + serviceAccessToken); | ||
|
||
String wsEndpoint = String.format("%s?os=%s&runId=%s&api-version=2023-10-01-preview", | ||
serviceUrl, osName, playwrightRunId); | ||
ConnectOptions connectOptions = new ConnectOptions(); | ||
connectOptions.setHeaders(headers); | ||
return new BrowserConnectOptions(wsEndpoint, connectOptions); | ||
} | ||
} |
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.
can we provide a way to pass on worker count ?
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.
or add a link from OSS around parallisation
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.
Java has ways on how you use it:
I recommend to also provide a way for junit, probably similar to how we do it in other languages, that the user can specify / override
connectOptions
.