1
0
mirror of synced 2026-05-26 20:53:32 +00:00

Compare commits

...

10 Commits

Author SHA1 Message Date
Yury Semikhatsky d7fee058b7 chore: version 0.170.3 (#155) 2020-12-19 18:05:19 -08:00
Yury Semikhatsky 569259d3df devops: auto update browser versions in readme (#153) 2020-12-18 18:42:11 -08:00
Yury Semikhatsky ef6adb8123 fix: allow to build options from DeviceDescriptor (#151) 2020-12-18 13:45:02 -08:00
Yury Semikhatsky ec86901e97 docs: edit examples reame 2020-12-18 13:02:17 -08:00
Yury Semikhatsky 64d9c82f71 chore: add examples project (#150) 2020-12-18 12:56:36 -08:00
Yury Semikhatsky 0f130d4358 feat: make driver-bundle required for playwright (#149) 2020-12-18 12:21:01 -08:00
Yury Semikhatsky fc0c183eec docs: add Java requirements section (#148) 2020-12-18 12:01:47 -08:00
Yury Semikhatsky 259f2481bd docs: add links to javadoc.io (#147) 2020-12-17 12:29:36 -08:00
Yury Semikhatsky 96fa086667 docs: update README.md with more instructions (#146) 2020-12-17 12:08:49 -08:00
Yury Semikhatsky f073a75bbe chore: bump version to 0.170.3-SNAPSHOT (#145) 2020-12-17 10:37:05 -08:00
21 changed files with 409 additions and 62 deletions
+31 -8
View File
@@ -1,30 +1,46 @@
# 🎭 [Playwright](https://github.com/microsoft/playwright) for Java
# 🎭 [Playwright](https://playwright.dev) for Java
[![maven version](https://img.shields.io/maven-central/v/com.microsoft.playwright/playwright)](https://search.maven.org/search?q=com.microsoft.playwright) [![Join Slack](https://img.shields.io/badge/join-slack-infomational)](https://join.slack.com/t/playwright/shared_invite/enQtOTEyMTUxMzgxMjIwLThjMDUxZmIyNTRiMTJjNjIyMzdmZDA3MTQxZWUwZTFjZjQwNGYxZGM5MzRmNzZlMWI5ZWUyOTkzMjE5Njg1NDg)
### _The project is in early development phase, the APIs match those in typescript version of Playwright but are subject to change._
#### [Website](https://playwright.dev/) | [API reference](https://www.javadoc.io/doc/com.microsoft.playwright/playwright/latest/index.html)
Playwright is a Java library to automate [Chromium](https://www.chromium.org/Home), [Firefox](https://www.mozilla.org/en-US/firefox/new/) and [WebKit](https://webkit.org/) with a single API. Playwright is built to enable cross-browser web automation that is **ever-green**, **capable**, **reliable** and **fast**.
| | Linux | macOS | Windows |
| :--- | :---: | :---: | :---: |
| Chromium <!-- GEN:chromium-version -->89.0.4344.0<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| WebKit <!-- GEN:webkit-version -->14.1<!-- GEN:stop --> | ✅ | ✅ | ✅ |
| Firefox <!-- GEN:firefox-version -->84.0b9<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
Headless execution is supported for all the browsers on all platforms. Check out [system requirements](https://playwright.dev/#?path=docs/intro.md&q=system-requirements) for details.
## Usage
Playwright supports Java 8 and above.
#### Add Maven dependency
To run Playwright simply add 2 modules to your Maven project:
Playwright is distributed as a set of [Maven](https://maven.apache.org/what-is-maven.html) modules. The easiest way to use it is to add a couple of dependencies to your Maven `pom.xml` file as described below. If you're not familiar with Maven please refer to its [documentation](https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html).
To run Playwright simply add two dependencies to your Maven project:
```xml
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>0.170.0</version>
<version>0.170.2</version>
</dependency>
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>driver-bundle</artifactId>
<version>0.170.0</version>
<version>0.170.2</version>
</dependency>
```
## Examples
You can find Maven project with the examples [here](./examples).
#### Page screenshot
This code snippet navigates to whatsmyuseragent.org in Chromium, Firefox and WebKit, and saves 3 screenshots.
@@ -146,9 +162,16 @@ public class InterceptNetworkRequests {
}
```
## Notes
## Documentation
Follow [the instructions](https://github.com/microsoft/playwright-java/blob/master/CONTRIBUTING.md#getting-code) to build the project from source and install driver.
We are in the process of converting our documentation from the Node.js form to [Javadocs](https://www.javadoc.io/doc/com.microsoft.playwright/playwright/latest/index.html). You can go ahead and use the Node.js [documentation](https://playwright.dev/) since the API is pretty much the same.
Original Playwright [documentation](https://playwright.dev/). We are converting it to javadoc.
## Contributing
Follow [the instructions](https://github.com/microsoft/playwright-java/blob/master/CONTRIBUTING.md#getting-code) to build the project from source and install the driver.
## Is Playwright for Java ready?
Yes, Playwright for Java is ready. We are still not at the version v1.0, so breaking API changes could potentially happen. But a) this is unlikely and b) we will only do that if we know it improves your experience with the new library. We'd like to collect your feedback before we freeze the API for v1.0.
> Note: We don't yet support some of the edge-cases of the vendor-specific APIs such as collecting Chromium trace, coverage report, etc.
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>com.microsoft.playwright</groupId>
<artifactId>parent-pom</artifactId>
<version>0.170.2</version>
<version>0.172.3</version>
</parent>
<artifactId>driver-bundle</artifactId>
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>com.microsoft.playwright</groupId>
<artifactId>parent-pom</artifactId>
<version>0.170.2</version>
<version>0.172.3</version>
</parent>
<artifactId>driver</artifactId>
+7
View File
@@ -0,0 +1,7 @@
This directory contains sample [`pom.xml`](./pom.xml) and source code for the Playwright examples.
You can run them in terminal like this:
```sh
mvn compile exec:java -Dexec.mainClass=org.example.PageScreenshot
```
+36
View File
@@ -0,0 +1,36 @@
<?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>0.1-SNAPSHOT</version>
<name>Playwright Client Examples</name>
<dependencies>
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>0.170.2</version>
</dependency>
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>driver-bundle</artifactId>
<version>0.170.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,40 @@
/*
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.example;
import com.microsoft.playwright.*;
public class EvaluateInBrowserContext {
public static void main(String[] args) throws Exception {
Playwright playwright = Playwright.create();
BrowserType browserType = playwright.firefox();
Browser browser = browserType.launch();
BrowserContext context = browser.newContext();
Page page = context.newPage();
page.navigate("https://www.example.com/");
Object dimensions = page.evaluate("() => {\n" +
" return {\n" +
" width: document.documentElement.clientWidth,\n" +
" height: document.documentElement.clientHeight,\n" +
" deviceScaleFactor: window.devicePixelRatio\n" +
" }\n" +
"}");
System.out.println(dimensions);
browser.close();
playwright.close();
}
}
@@ -13,23 +13,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.playwright.example;
package org.example;
import com.microsoft.playwright.*;
import java.io.File;
import java.nio.file.Paths;
public class Main {
public class InterceptNetworkRequests {
public static void main(String[] args) throws Exception {
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium().launch();
BrowserContext context = browser.newContext(
new Browser.NewContextOptions().withViewport(800, 600));
BrowserType browserType = playwright.webkit();
Browser browser = browserType.launch();
BrowserContext context = browser.newContext();
Page page = context.newPage();
page.navigate("https://webkit.org");
page.click("text=check feature status");
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("s.png")));
page.route("**", route -> {
System.out.println(route.request().url());
route.continue_();
});
page.navigate("http://todomvc.com");
browser.close();
playwright.close();
}
@@ -0,0 +1,47 @@
/*
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.example;
import com.microsoft.playwright.*;
import java.nio.file.Paths;
import static java.util.Arrays.asList;
public class MobileAndGeolocation {
public static void main(String[] args) throws Exception {
Playwright playwright = Playwright.create();
BrowserType browserType = playwright.chromium();
Browser browser = browserType.launch();
DeviceDescriptor pixel2 = playwright.devices().get("Pixel 2");
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.withViewport(pixel2.viewport().width(), pixel2.viewport().height())
.withUserAgent(pixel2.userAgent())
.withDeviceScaleFactor(pixel2.deviceScaleFactor())
.withIsMobile(pixel2.isMobile())
.withHasTouch(pixel2.hasTouch())
.withLocale("en-US")
.withGeolocation(new Geolocation(41.889938, 12.492507))
.withPermissions(asList("geolocation")));
Page page = context.newPage();
page.navigate("https://www.openstreetmap.org/");
page.click("a[data-original-title=\"Show My Location\"]");
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("colosseum-pixel2.png")));
browser.close();
playwright.close();
}
}
@@ -0,0 +1,44 @@
/*
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.example;
import com.microsoft.playwright.*;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
public class PageScreenshot {
public static void main(String[] args) throws Exception {
Playwright playwright = Playwright.create();
List<BrowserType> browserTypes = Arrays.asList(
playwright.chromium(),
playwright.webkit(),
playwright.firefox()
);
for (BrowserType browserType : browserTypes) {
Browser browser = browserType.launch();
BrowserContext context = browser.newContext(
new Browser.NewContextOptions().withViewport(800, 600));
Page page = context.newPage();
page.navigate("http://whatsmyuseragent.org/");
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("screenshot-" + browserType.name() + ".png")));
browser.close();
}
playwright.close();
}
}
+1 -2
View File
@@ -7,7 +7,7 @@
<parent>
<groupId>com.microsoft.playwright</groupId>
<artifactId>parent-pom</artifactId>
<version>0.170.2</version>
<version>0.172.3</version>
</parent>
<artifactId>playwright</artifactId>
@@ -72,7 +72,6 @@
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>driver-bundle</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.java-websocket</groupId>
@@ -326,6 +326,14 @@ public interface Browser {
this.storageStatePath = storageStatePath;
return this;
}
public NewContextOptions withDevice(DeviceDescriptor device) {
withViewport(device.viewport().width(), device.viewport().height());
withUserAgent(device.userAgent());
withDeviceScaleFactor(device.deviceScaleFactor());
withIsMobile(device.isMobile());
withHasTouch(device.hasTouch());
return this;
}
}
class NewPageOptions {
public class RecordHar {
@@ -600,6 +608,14 @@ public interface Browser {
this.storageStatePath = storageStatePath;
return this;
}
public NewPageOptions withDevice(DeviceDescriptor device) {
withViewport(device.viewport().width(), device.viewport().height());
withUserAgent(device.userAgent());
withDeviceScaleFactor(device.deviceScaleFactor());
withIsMobile(device.isMobile());
withHasTouch(device.hasTouch());
return this;
}
}
/**
* In case this browser is obtained using browserType.launch([options]), closes the browser and all of its pages (if any were
+4 -5
View File
@@ -6,7 +6,7 @@
<groupId>com.microsoft.playwright</groupId>
<artifactId>parent-pom</artifactId>
<version>0.170.2</version>
<version>0.172.3</version>
<packaging>pom</packaging>
<name>Playwright Parent Project</name>
<description>Java library to automate Chromium, Firefox and WebKit with a single API.
@@ -36,7 +36,6 @@
</scm>
<modules>
<module>api-generator</module>
<module>driver</module>
<module>driver-bundle</module>
<module>playwright</module>
@@ -44,9 +43,10 @@
<properties>
<compiler.version>1.8</compiler.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<gson.version>2.8.6</gson.version>
<junit.version>5.7.0</junit.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<websocket.version>1.5.1</websocket.version>
</properties>
<dependencyManagement>
@@ -61,7 +61,6 @@
<artifactId>driver-bundle</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
@@ -76,7 +75,7 @@
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.5.1</version>
<version>${websocket.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
+3 -3
View File
@@ -9,9 +9,9 @@ cd "$(dirname $0)/.."
PLAYWRIGHT_CLI=./driver-bundle/src/main/resources/driver/linux/playwright-cli
echo "Updating api.json from $($PLAYWRIGHT_CLI --version)"
$PLAYWRIGHT_CLI print-api-json > ./api-generator/src/main/resources/api.json
$PLAYWRIGHT_CLI print-api-json > ./tools/api-generator/src/main/resources/api.json
mvn compile -projects api-generator --no-transfer-progress
mvn compile -f ./tools/api-generator --no-transfer-progress
echo "Regenerating Java interfaces"
mvn exec:java --projects api-generator -Dexec.mainClass=com.microsoft.playwright.tools.ApiGenerator
mvn exec:java --f ./tools/api-generator -Dexec.mainClass=com.microsoft.playwright.tools.ApiGenerator
+25
View File
@@ -0,0 +1,25 @@
#!/bin/bash
set -e
set +x
if [[ $# == 0 ]]; then
echo "Missing version parameter."
echo "Usage:"
echo " $(basename $0) 0.170.3-SNAPSHOT"
exit 1
fi
trap "cd $(pwd -P)" EXIT
cd "$(dirname $0)/.."
VERSION=$1
mvn versions:set -DnewVersion=$VERSION
cd tools/api-generator
mvn versions:set -DnewVersion=$VERSION
cd -
cd tools/update-docs-version
mvn versions:set -DnewVersion=$VERSION
+16
View File
@@ -0,0 +1,16 @@
#!/bin/bash
set -e
set +x
trap "cd $(pwd -P)" EXIT
cd "$(dirname $0)/.."
./scripts/generate_api.sh
# Built from source and do local install.
mvn clean install --no-transfer-progress -DskipTests
echo "Updating browser versions in README.md"
mvn exec:java --f ./tools/update-docs-version -Dexec.mainClass=com.microsoft.playwright.tools.UpdateBrowserVersions
@@ -4,47 +4,30 @@
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>
<parent>
<groupId>com.microsoft.playwright</groupId>
<artifactId>parent-pom</artifactId>
<version>0.170.2</version>
</parent>
<groupId>com.microsoft.playwright</groupId>
<artifactId>api-generator</artifactId>
<version>0.172.3</version>
<name>Playwright - API Generator</name>
<description>Java library to automate Chromium, Firefox and WebKit with a single API.
Playwright is built to enable cross-browser web automation that is ever-green, capable,
reliable and fast.
<description>
This is an internal module used to generate Java API from the upstream Playwright
definitions. It's an internal development module not intended for external use.
</description>
<properties>
<compiler.version>1.8</compiler.version>
<gson.version>2.8.6</gson.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<source>8</source>
<failOnError>false</failOnError>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${compiler.version}</source>
<target>${compiler.version}</target>
</configuration>
</plugin>
</plugins>
</build>
@@ -52,6 +35,7 @@
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
</dependencies>
</project>
@@ -1102,6 +1102,10 @@ class NestedClass extends TypeDefinition {
}
} else {
writeBuilderMethods(output, bodyOffset);
if ("Browser.newContext.options".equals(jsonPath) ||
"Browser.newPage.options".equals(jsonPath)) {
writeDeviceDescriptorBuilder(output, bodyOffset);
}
}
output.add(offset + "}");
}
@@ -1133,6 +1137,16 @@ class NestedClass extends TypeDefinition {
output.add(bodyOffset + "}");
}
private void writeDeviceDescriptorBuilder(List<String> output, String bodyOffset) {
output.add(bodyOffset + "public " + name + " withDevice(DeviceDescriptor device) {");
output.add(bodyOffset + " withViewport(device.viewport().width(), device.viewport().height());");
output.add(bodyOffset + " withUserAgent(device.userAgent());");
output.add(bodyOffset + " withDeviceScaleFactor(device.deviceScaleFactor());");
output.add(bodyOffset + " withIsMobile(device.isMobile());");
output.add(bodyOffset + " withHasTouch(device.hasTouch());");
output.add(bodyOffset + " return this;");
output.add(bodyOffset + "}");
}
}
class Enum extends TypeDefinition {
@@ -1188,7 +1202,7 @@ public class ApiGenerator {
public static void main(String[] args) throws IOException {
File cwd = FileSystems.getDefault().getPath(".").toFile();
System.out.println(cwd.getCanonicalPath());
File file = new File(cwd, "api-generator/src/main/resources/api.json");
File file = new File(cwd, "tools/api-generator/src/main/resources/api.json");
System.out.println("Reading from: " + file.getCanonicalPath());
new ApiGenerator(new FileReader(file));
}
+41
View File
@@ -0,0 +1,41 @@
<?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.microsoft.playwright</groupId>
<artifactId>update-version</artifactId>
<version>0.172.3</version>
<name>Playwright - Update Version in Documentation</name>
<description>
This is an internal module used to update versions in the documentation based on
current driver.
</description>
<properties>
<compiler.version>1.8</compiler.version>
<gson.version>2.8.6</gson.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${compiler.version}</source>
<target>${compiler.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,56 @@
/*
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.playwright.tools;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Playwright;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import static java.nio.charset.StandardCharsets.UTF_8;
public class UpdateBrowserVersions {
public static void main(String[] args) throws Exception {
Path path = Paths.get("README.md");
String readme = new String(Files.readAllBytes(path), UTF_8);
Playwright playwright = Playwright.create();
List<BrowserType> browserTypes = Arrays.asList(
playwright.chromium(),
playwright.webkit(),
playwright.firefox()
);
for (BrowserType browserType : browserTypes) {
Browser browser = browserType.launch();
String version = browser.version();
browser.close();
readme = readme.replaceAll(
"<!-- GEN:" + browserType.name() + "-version -->([^<]+)<!-- GEN:stop -->",
"<!-- GEN:" + browserType.name() + "-version -->" + version + "<!-- GEN:stop -->");
try (FileWriter writer = new FileWriter(path.toFile())) {
writer.write(readme);
}
}
playwright.close();
}
}