Compare commits
56 Commits
v1.18.0
...
release-1.22
| Author | SHA1 | Date | |
|---|---|---|---|
| 64115bfb60 | |||
| 15eefc54af | |||
| abf245ccc7 | |||
| 10592ce5c7 | |||
| ef7f50c48a | |||
| 473b1ce794 | |||
| 98ecb7e0a0 | |||
| 04eb228813 | |||
| 298e01ee80 | |||
| b6b54af13c | |||
| b8d2ccae08 | |||
| 59e7c0cc94 | |||
| 54d0366b9e | |||
| 9845a05544 | |||
| 41fd9a6f75 | |||
| 483cf0d473 | |||
| 1681c410dd | |||
| 9f6860539a | |||
| 536af6b3d8 | |||
| 8ce193d144 | |||
| 7eddd2d2b2 | |||
| 447578c582 | |||
| 58013adfac | |||
| 43d12a7662 | |||
| 1deccbb55d | |||
| 1b2d33402e | |||
| d315e7b5bf | |||
| 7dc22aa08a | |||
| 5b0ef8b7bf | |||
| 43ba37817b | |||
| 4916ba22af | |||
| 94f72694f1 | |||
| be167a161b | |||
| 187d2ad6c6 | |||
| 68a7dbc1e3 | |||
| 1153d473fb | |||
| 00d53bd1ea | |||
| d423733d9d | |||
| e127abb68e | |||
| 5ee8f23380 | |||
| b448a1789a | |||
| 79b2c70513 | |||
| d476d0a98c | |||
| 2122c5690a | |||
| 3119102b10 | |||
| 838e7a40b3 | |||
| ea6ede4670 | |||
| 3cdefc2931 | |||
| 119700a678 | |||
| 17a4143a83 | |||
| c03f4a9384 | |||
| 85b671328e | |||
| 11f898ca7f | |||
| 2aef5c6742 | |||
| 897d441c02 | |||
| f4c69faad3 |
@@ -1,6 +1,7 @@
|
||||
name: Publish
|
||||
on:
|
||||
workflow_dispatch:
|
||||
release:
|
||||
types: [published]
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
|
||||
@@ -17,10 +17,9 @@ jobs:
|
||||
username: playwright
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
- uses: actions/checkout@v2
|
||||
- name: Build Docker image
|
||||
run: docker build -t playwright-java:localbuild-focal -f Dockerfile.focal .
|
||||
- name: tag & publish
|
||||
run: |
|
||||
./scripts/tag_image_and_push.sh playwright-java:localbuild-focal playwright.azurecr.io/public/playwright/java:next
|
||||
./scripts/tag_image_and_push.sh playwright-java:localbuild-focal playwright.azurecr.io/public/playwright/java:next-focal
|
||||
./scripts/tag_image_and_push.sh playwright-java:localbuild-focal playwright.azurecr.io/public/playwright/java:sha-${{ github.sha }}
|
||||
- name: Set up Docker QEMU for arm64 docker builds
|
||||
uses: docker/setup-qemu-action@v1
|
||||
with:
|
||||
platforms: arm64
|
||||
- name: publish docker canary
|
||||
run: ./utils/docker/publish_docker.sh canary
|
||||
|
||||
@@ -3,6 +3,11 @@ on:
|
||||
release:
|
||||
types: [published]
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
is_release:
|
||||
required: true
|
||||
type: boolean
|
||||
description: "Is this a release image?"
|
||||
branches:
|
||||
- release-*
|
||||
jobs:
|
||||
@@ -17,20 +22,12 @@ jobs:
|
||||
login-server: playwright.azurecr.io
|
||||
username: playwright
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
- name: Set up Docker QEMU for arm64 docker builds
|
||||
uses: docker/setup-qemu-action@v1
|
||||
with:
|
||||
platforms: arm64
|
||||
- uses: actions/checkout@v2
|
||||
- name: Build Docker image
|
||||
run: docker build -t playwright-java:localbuild-focal -f Dockerfile.focal .
|
||||
- name: tag & publish
|
||||
run: |
|
||||
# GITHUB_REF has a form of `refs/tags/v1.3.0`.
|
||||
# TAG_NAME would be `v1.3.0`
|
||||
TAG_NAME=${GITHUB_REF#refs/tags/}
|
||||
if [[ ! "$TAG_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]];
|
||||
then
|
||||
echo "Wrong TAG_NAME format: $TAG_NAME"
|
||||
exit 1
|
||||
fi
|
||||
./scripts/tag_image_and_push.sh playwright-java:localbuild-focal playwright.azurecr.io/public/playwright/java:latest
|
||||
./scripts/tag_image_and_push.sh playwright-java:localbuild-focal playwright.azurecr.io/public/playwright/java:focal
|
||||
./scripts/tag_image_and_push.sh playwright-java:localbuild-focal playwright.azurecr.io/public/playwright/java:${TAG_NAME}
|
||||
./scripts/tag_image_and_push.sh playwright-java:localbuild-focal playwright.azurecr.io/public/playwright/java:${TAG_NAME}-focal
|
||||
- run: ./utils/docker/publish_docker.sh stable
|
||||
if: (github.event_name != 'workflow_dispatch' && !github.event.release.prerelease) || (github.event_name == 'workflow_dispatch' && github.event.inputs.is_release == 'true')
|
||||
- run: ./utils/docker/publish_docker.sh canary
|
||||
if: (github.event_name != 'workflow_dispatch' && github.event.release.prerelease) || (github.event_name == 'workflow_dispatch' && github.event.inputs.is_release != 'true')
|
||||
|
||||
+21
-32
@@ -19,38 +19,35 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: microsoft/playwright-github-action@v1.5.0
|
||||
- uses: microsoft/playwright-github-action@v1
|
||||
- name: Set up JDK 1.8
|
||||
uses: actions/setup-java@v2
|
||||
with:
|
||||
distribution: zulu
|
||||
java-version: 8
|
||||
- name: Cache Maven packages
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ~/.m2
|
||||
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
|
||||
restore-keys: ${{ runner.os }}-m2
|
||||
- name: Download drivers
|
||||
shell: bash
|
||||
run: scripts/download_driver_for_all_platforms.sh
|
||||
- name: Build with Maven
|
||||
run: mvn -B package -D skipTests --no-transfer-progress
|
||||
- name: Build & Install
|
||||
run: mvn -B install -D skipTests --no-transfer-progress
|
||||
- name: Run tests
|
||||
run: mvn test --no-transfer-progress --fail-at-end
|
||||
run: mvn test -DexcludedGroups=isolated --no-transfer-progress --fail-at-end
|
||||
env:
|
||||
BROWSER: ${{ matrix.browser }}
|
||||
- name: Run tracing tests w/ sources
|
||||
run: mvn test --no-transfer-progress --fail-at-end -D test=*TestTracing*
|
||||
run: mvn test -DexcludedGroups=isolated --no-transfer-progress --fail-at-end -D test=*TestTracing*
|
||||
env:
|
||||
BROWSER: ${{ matrix.browser }}
|
||||
PLAYWRIGHT_JAVA_SRC: src/test/java
|
||||
- name: Run driver throw tests
|
||||
run: mvn test -Dgroups=driverThrowTest --no-transfer-progress --fail-at-end
|
||||
env:
|
||||
BROWSER: ${{ matrix.browser }}
|
||||
- name: Test Spring Boot Starter
|
||||
shell: bash
|
||||
env:
|
||||
BROWSER: ${{ matrix.browser }}
|
||||
run: |
|
||||
mvn -B install -D skipTests --no-transfer-progress
|
||||
cd tools/test-spring-boot-starter
|
||||
mvn package -D skipTests --no-transfer-progress
|
||||
java -jar target/test-spring-boot*.jar
|
||||
@@ -70,7 +67,7 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: microsoft/playwright-github-action@v1.5.0
|
||||
- uses: microsoft/playwright-github-action@v1
|
||||
- name: Install Media Pack
|
||||
if: matrix.os == 'windows-latest'
|
||||
shell: powershell
|
||||
@@ -80,19 +77,18 @@ jobs:
|
||||
with:
|
||||
distribution: zulu
|
||||
java-version: 8
|
||||
- name: Cache Maven packages
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ~/.m2
|
||||
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
|
||||
restore-keys: ${{ runner.os }}-m2
|
||||
- name: Download drivers
|
||||
shell: bash
|
||||
run: scripts/download_driver_for_all_platforms.sh
|
||||
- name: Build with Maven
|
||||
run: mvn -B package -D skipTests --no-transfer-progress
|
||||
- name: Build & Install
|
||||
run: mvn -B install -D skipTests --no-transfer-progress
|
||||
- name: Run tests
|
||||
run: mvn test --no-transfer-progress --fail-at-end
|
||||
run: mvn test -DexcludedGroups=isolated --no-transfer-progress --fail-at-end
|
||||
env:
|
||||
BROWSER: chromium
|
||||
BROWSER_CHANNEL: ${{ matrix.browser-channel }}
|
||||
- name: Run driver throw tests
|
||||
run: mvn test -Dgroups=driverThrowTest --no-transfer-progress --fail-at-end
|
||||
env:
|
||||
BROWSER: chromium
|
||||
BROWSER_CHANNEL: ${{ matrix.browser-channel }}
|
||||
@@ -106,23 +102,17 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: microsoft/playwright-github-action@v1.5.0
|
||||
- uses: microsoft/playwright-github-action@v1
|
||||
- name: Set up JDK 17
|
||||
uses: actions/setup-java@v2
|
||||
with:
|
||||
distribution: adopt
|
||||
java-version: 17
|
||||
- name: Cache Maven packages
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ~/.m2
|
||||
key: m2-${{ hashFiles('**/pom.xml') }}
|
||||
restore-keys: m2
|
||||
- name: Download drivers
|
||||
shell: bash
|
||||
run: scripts/download_driver_for_all_platforms.sh
|
||||
- name: Build with Maven
|
||||
run: mvn -B package -D skipTests --no-transfer-progress
|
||||
- name: Build & Install
|
||||
run: mvn -B install -D skipTests --no-transfer-progress
|
||||
- name: Run tests
|
||||
run: mvn test --no-transfer-progress --fail-at-end
|
||||
env:
|
||||
@@ -132,7 +122,6 @@ jobs:
|
||||
env:
|
||||
BROWSER: ${{ matrix.browser }}
|
||||
run: |
|
||||
mvn -B install -D skipTests --no-transfer-progress
|
||||
cd tools/test-spring-boot-starter
|
||||
mvn package -D skipTests --no-transfer-progress
|
||||
java -jar target/test-spring-boot*.jar
|
||||
|
||||
@@ -3,14 +3,14 @@ on:
|
||||
push:
|
||||
paths:
|
||||
- '.github/workflows/test_docker.yml'
|
||||
- 'Dockerfile*'
|
||||
- '**/Dockerfile*'
|
||||
branches:
|
||||
- main
|
||||
- release-*
|
||||
pull_request:
|
||||
paths:
|
||||
- .github/workflows/test_docker.yml
|
||||
- Dockerfile.*
|
||||
- '**/Dockerfile*'
|
||||
- scripts/CLI_VERSION
|
||||
- '**/pom.xml'
|
||||
branches:
|
||||
@@ -23,7 +23,7 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Build Docker image
|
||||
run: docker build -t playwright-java:localbuild-focal -f Dockerfile.focal .
|
||||
run: bash utils/docker/build.sh --amd64 focal playwright-java:localbuild-focal
|
||||
- name: Test
|
||||
run: |
|
||||
CONTAINER_ID="$(docker run --rm --ipc=host -v $(pwd):/root/playwright --name playwright-docker-test -d -t playwright-java:localbuild-focal /bin/bash)"
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
name: "Internal Tests"
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- release-*
|
||||
|
||||
jobs:
|
||||
trigger:
|
||||
name: "trigger"
|
||||
runs-on: ubuntu-20.04
|
||||
steps:
|
||||
- run: |
|
||||
curl -X POST \
|
||||
-H "Accept: application/vnd.github.v3+json" \
|
||||
-H "Authorization: token ${GH_TOKEN}" \
|
||||
--data "{\"event_type\": \"playwright_tests_java\", \"client_payload\": {\"ref\": \"${GITHUB_SHA}\"}}" \
|
||||
https://api.github.com/repos/microsoft/playwright-internal/dispatches
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.REPOSITORY_DISPATCH_PERSONAL_ACCESS_TOKEN }}
|
||||
@@ -20,13 +20,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: microsoft/playwright-github-action@v1.5.0
|
||||
- name: Cache Maven packages
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ~/.m2
|
||||
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
|
||||
restore-keys: ${{ runner.os }}-m2
|
||||
- uses: microsoft/playwright-github-action@v1
|
||||
- name: Download drivers
|
||||
run: scripts/download_driver_for_all_platforms.sh
|
||||
- name: Regenerate APIs
|
||||
|
||||
+14
-1
@@ -8,7 +8,7 @@ Install git, Java JDK (version >= 8), Maven (tested with version 3.6.3), on Ubun
|
||||
just run the following command:
|
||||
|
||||
```sh
|
||||
sudo apt-get install git openjdk-11-jdk maven
|
||||
sudo apt-get install git openjdk-11-jdk maven unzip
|
||||
```
|
||||
|
||||
### Getting the Code
|
||||
@@ -49,6 +49,19 @@ Java interfaces for the current driver run the following commands:
|
||||
./scripts/generate_api.sh
|
||||
```
|
||||
|
||||
#### Updating driver version
|
||||
|
||||
Driver version is read from [scripts/CLI_VERSION](https://github.com/microsoft/playwright-java/blob/main/scripts/CLI_VERSION) and can be found in the upstream [GHA build](https://github.com/microsoft/playwright/actions/workflows/publish_canary.yml) logs. To update the driver to a particular version run the following commands:
|
||||
|
||||
```bash
|
||||
cat > scripts/CLI_VERSION
|
||||
<paste new version>
|
||||
^D
|
||||
./scripts/download_driver_for_all_platforms.sh -f
|
||||
./scripts/generate_api.sh
|
||||
./scripts/update_readme.sh
|
||||
```
|
||||
|
||||
### Code Style
|
||||
|
||||
- We try to follow [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html)
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
FROM ubuntu:focal
|
||||
|
||||
# === INSTALL JDK and Maven ===
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
openjdk-11-jdk maven
|
||||
|
||||
ENV JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
|
||||
|
||||
# Install utilities required for downloading driver
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
curl unzip
|
||||
|
||||
# === INSTALL playwright maven modules & browsers ===
|
||||
|
||||
# Browsers will remain downloaded in `/ms-playwright`.
|
||||
# Note: make sure to set 777 to the registry so that any user can access
|
||||
# registry.
|
||||
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
|
||||
|
||||
RUN mkdir /ms-playwright && chmod -R 777 $PLAYWRIGHT_BROWSERS_PATH
|
||||
|
||||
RUN mkdir /tmp/pw-java
|
||||
COPY . /tmp/pw-java
|
||||
RUN cd /tmp/pw-java && \
|
||||
./scripts/download_driver_for_all_platforms.sh && \
|
||||
mvn install -D skipTests --no-transfer-progress && \
|
||||
DEBIAN_FRONTEND=noninteractive mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI \
|
||||
-D exec.args="install-deps" -f playwright/pom.xml --no-transfer-progress && \
|
||||
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI \
|
||||
-D exec.args="install" -f playwright/pom.xml --no-transfer-progress && \
|
||||
rm -rf /tmp/pw-java
|
||||
@@ -11,9 +11,9 @@ Playwright is a Java library to automate [Chromium](https://www.chromium.org/Hom
|
||||
|
||||
| | Linux | macOS | Windows |
|
||||
| :--- | :---: | :---: | :---: |
|
||||
| Chromium <!-- GEN:chromium-version -->99.0.4812.0<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
|
||||
| Chromium <!-- GEN:chromium-version -->102.0.5005.40<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
|
||||
| WebKit <!-- GEN:webkit-version -->15.4<!-- GEN:stop --> | ✅ | ✅ | ✅ |
|
||||
| Firefox <!-- GEN:firefox-version -->95.0<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
|
||||
| Firefox <!-- GEN:firefox-version -->99.0.1<!-- 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/java/docs/next/intro/#system-requirements) for details.
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>com.microsoft.playwright</groupId>
|
||||
<artifactId>parent-pom</artifactId>
|
||||
<version>1.18.0-SNAPSHOT</version>
|
||||
<version>1.22.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>driver-bundle</artifactId>
|
||||
|
||||
@@ -37,12 +37,15 @@ public class DriverJar extends Driver {
|
||||
? Files.createTempDirectory(prefix)
|
||||
: Files.createTempDirectory(Paths.get(alternativeTmpdir), prefix);
|
||||
driverTempDir.toFile().deleteOnExit();
|
||||
logMessage("created DriverJar: " + driverTempDir);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initialize(Map<String, String> env) throws Exception {
|
||||
protected void initialize(Map<String, String> env, Boolean installBrowsers) throws Exception {
|
||||
extractDriverToTempDir();
|
||||
installBrowsers(env);
|
||||
logMessage("extracted driver from jar to " + driverPath());
|
||||
if (installBrowsers)
|
||||
installBrowsers(env);
|
||||
}
|
||||
|
||||
private void installBrowsers(Map<String, String> env) throws IOException, InterruptedException {
|
||||
@@ -60,6 +63,7 @@ public class DriverJar extends Driver {
|
||||
}
|
||||
ProcessBuilder pb = new ProcessBuilder(driver.toString(), "install");
|
||||
pb.environment().putAll(env);
|
||||
setRequiredEnvironmentVariables(pb);
|
||||
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
|
||||
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
|
||||
Process p = pb.start();
|
||||
@@ -135,11 +139,17 @@ public class DriverJar extends Driver {
|
||||
|
||||
private static String platformDir() {
|
||||
String name = System.getProperty("os.name").toLowerCase();
|
||||
String arch = System.getProperty("os.arch").toLowerCase();
|
||||
|
||||
if (name.contains("windows")) {
|
||||
return "win32_x64";
|
||||
}
|
||||
if (name.contains("linux")) {
|
||||
return "linux";
|
||||
if (arch.equals("aarch64")) {
|
||||
return "linux-arm64";
|
||||
} else {
|
||||
return "linux";
|
||||
}
|
||||
}
|
||||
if (name.contains("mac os x")) {
|
||||
return "mac";
|
||||
|
||||
@@ -18,16 +18,19 @@ package com.microsoft.playwright;
|
||||
|
||||
import com.microsoft.playwright.impl.Driver;
|
||||
import com.microsoft.playwright.impl.DriverJar;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestInstall {
|
||||
@@ -36,11 +39,25 @@ public class TestInstall {
|
||||
// Clear system property to ensure that the driver is loaded from jar.
|
||||
System.clearProperty("playwright.cli.dir");
|
||||
System.clearProperty("playwright.driver.tmpdir");
|
||||
// Clear system property to ensure that the default driver is loaded.
|
||||
System.clearProperty("playwright.driver.impl");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Tags({@Tag("isolated"), @Tag("driverThrowTest")})
|
||||
void shouldThrowWhenBrowserPathIsInvalid(@TempDir Path tmpDir) {
|
||||
Map<String,String> env = new HashMap<>();
|
||||
env.put("PLAYWRIGHT_DOWNLOAD_HOST", "https://127.0.0.127");
|
||||
// Make sure the browsers are not installed yet by pointing at an empty dir.
|
||||
env.put("PLAYWRIGHT_BROWSERS_PATH", tmpDir.toString());
|
||||
|
||||
assertThrows(RuntimeException.class, () -> Driver.ensureDriverInstalled(env, true));
|
||||
assertThrows(RuntimeException.class, () -> Driver.ensureDriverInstalled(env, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void playwrightCliInstalled() throws Exception {
|
||||
Path cli = Driver.ensureDriverInstalled(Collections.emptyMap());
|
||||
Path cli = Driver.ensureDriverInstalled(Collections.emptyMap(), false);
|
||||
assertTrue(Files.exists(cli));
|
||||
|
||||
ProcessBuilder pb = new ProcessBuilder(cli.toString(), "install");
|
||||
@@ -57,4 +74,19 @@ public class TestInstall {
|
||||
DriverJar driver = new DriverJar();
|
||||
assertTrue(driver.driverPath().startsWith(tmpdir), "Driver path: " + driver.driverPath() + " tmp: " + tmpdir);
|
||||
}
|
||||
|
||||
@Test
|
||||
void playwrightDriverDefaultImpl() {
|
||||
assertDoesNotThrow(() -> Driver.ensureDriverInstalled(Collections.emptyMap(), false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void playwrightDriverAlternativeImpl() {
|
||||
System.setProperty("playwright.driver.impl", "com.microsoft.playwright.impl.AlternativeDriver");
|
||||
RuntimeException thrown =
|
||||
assertThrows(
|
||||
RuntimeException.class,
|
||||
() -> Driver.ensureDriverInstalled(Collections.emptyMap(), false));
|
||||
assertEquals("Failed to create driver", thrown.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>com.microsoft.playwright</groupId>
|
||||
<artifactId>parent-pom</artifactId>
|
||||
<version>1.18.0-SNAPSHOT</version>
|
||||
<version>1.22.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>driver</artifactId>
|
||||
|
||||
@@ -18,8 +18,11 @@ package com.microsoft.playwright.impl;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.microsoft.playwright.impl.DriverLogging.logWithTimestamp;
|
||||
|
||||
/**
|
||||
* This class provides access to playwright-cli. It can be either preinstalled
|
||||
* in the host system and its path is passed as a system property or it can be
|
||||
@@ -31,11 +34,12 @@ public abstract class Driver {
|
||||
private static class PreinstalledDriver extends Driver {
|
||||
private final Path driverDir;
|
||||
PreinstalledDriver(Path driverDir) {
|
||||
logMessage("created PreinstalledDriver: " + driverDir);
|
||||
this.driverDir = driverDir;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initialize(Map<String, String> env) {
|
||||
protected void initialize(Map<String, String> env, Boolean installBrowsers) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
@@ -45,19 +49,22 @@ public abstract class Driver {
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized Path ensureDriverInstalled(Map<String, String> env) {
|
||||
public static synchronized Path ensureDriverInstalled(Map<String, String> env, Boolean installBrowsers) {
|
||||
if (instance == null) {
|
||||
try {
|
||||
instance = createDriver();
|
||||
instance.initialize(env);
|
||||
logMessage("initializing driver");
|
||||
instance.initialize(env, installBrowsers);
|
||||
logMessage("driver initialized.");
|
||||
} catch (Exception exception) {
|
||||
instance = null;
|
||||
throw new RuntimeException("Failed to create driver", exception);
|
||||
}
|
||||
}
|
||||
return instance.driverPath();
|
||||
}
|
||||
|
||||
protected abstract void initialize(Map<String, String> env) throws Exception;
|
||||
protected abstract void initialize(Map<String, String> env, Boolean installBrowsers) throws Exception;
|
||||
|
||||
public Path driverPath() {
|
||||
String cliFileName = System.getProperty("os.name").toLowerCase().contains("windows") ?
|
||||
@@ -66,9 +73,11 @@ public abstract class Driver {
|
||||
}
|
||||
|
||||
public static void setRequiredEnvironmentVariables(ProcessBuilder pb) {
|
||||
if (!pb.environment().containsKey("PW_CLI_TARGET_LANG")) {
|
||||
pb.environment().put("PW_CLI_TARGET_LANG", "java");
|
||||
pb.environment().put("PW_CLI_TARGET_LANG_VERSION", getMajorJavaVersion());
|
||||
pb.environment().put("PW_LANG_NAME", "java");
|
||||
pb.environment().put("PW_LANG_NAME_VERSION", getMajorJavaVersion());
|
||||
String version = Driver.class.getPackage().getImplementationVersion();
|
||||
if (version != null) {
|
||||
pb.environment().put("PW_CLI_DISPLAY_VERSION", version);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,9 +99,16 @@ public abstract class Driver {
|
||||
return new PreinstalledDriver(Paths.get(pathFromProperty));
|
||||
}
|
||||
|
||||
Class<?> jarDriver = Class.forName("com.microsoft.playwright.impl.DriverJar");
|
||||
String driverImpl =
|
||||
System.getProperty("playwright.driver.impl", "com.microsoft.playwright.impl.DriverJar");
|
||||
Class<?> jarDriver = Class.forName(driverImpl);
|
||||
return (Driver) jarDriver.getDeclaredConstructor().newInstance();
|
||||
}
|
||||
|
||||
abstract Path driverDir();
|
||||
|
||||
protected static void logMessage(String message) {
|
||||
// This matches log format produced by the server.
|
||||
logWithTimestamp("pw:install " + message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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.impl;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
class DriverLogging {
|
||||
private static final boolean isEnabled;
|
||||
static {
|
||||
String debug = System.getenv("DEBUG");
|
||||
isEnabled = (debug != null) && debug.contains("pw:install");
|
||||
}
|
||||
|
||||
private static final DateTimeFormatter timestampFormat = DateTimeFormatter.ofPattern(
|
||||
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX").withZone(ZoneId.of("UTC"));
|
||||
|
||||
static void logWithTimestamp(String message) {
|
||||
if (!isEnabled) {
|
||||
return;
|
||||
}
|
||||
// This matches log format produced by the server.
|
||||
String timestamp = ZonedDateTime.now().format(timestampFormat);
|
||||
System.err.println(timestamp + " " + message);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>examples</artifactId>
|
||||
<version>1.18.0-SNAPSHOT</version>
|
||||
<version>1.22.0</version>
|
||||
<name>Playwright Client Examples</name>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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 SelectorsAndKeyboardManipulation {
|
||||
public static void main(String[] args) {
|
||||
try(Playwright playwright = Playwright.create()) {
|
||||
Browser browser = playwright.firefox().launch();
|
||||
BrowserContext context = browser.newContext();
|
||||
Page page = context.newPage();
|
||||
page.navigate("https://playwright.dev/java/");
|
||||
page.locator("text=SearchK").click();
|
||||
page.locator("[placeholder=\"Search docs\"]").fill("getting started");
|
||||
page.locator("div[role=\"button\"]:has-text(\"CancelIntroductionGetting startedInstallationGetting startedUsageGetting start\")").click();
|
||||
page.waitForSelector("h1:has-text(\"Getting started\")"); // Waits for the new page to load before screenshotting.
|
||||
page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("Screenshot.png")));
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>com.microsoft.playwright</groupId>
|
||||
<artifactId>parent-pom</artifactId>
|
||||
<version>1.18.0-SNAPSHOT</version>
|
||||
<version>1.22.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>playwright</artifactId>
|
||||
|
||||
@@ -21,9 +21,9 @@ import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Exposes API that can be used for the Web API testing. Each Playwright browser context has a APIRequestContext instance
|
||||
* attached which shares cookies with the page context. Its also possible to create a new APIRequestContext instance
|
||||
* manually. For more information see <a href="https://playwright.dev/java/docs/class-apirequestcontext">here</a>.
|
||||
* Exposes API that can be used for the Web API testing. This class is used for creating {@code APIRequestContext} instance which
|
||||
* in turn can be used for sending web requests. An instance of this class can be obtained via {@link Playwright#request
|
||||
* Playwright.request()}. For more information see {@code APIRequestContext}.
|
||||
*/
|
||||
public interface APIRequest {
|
||||
class NewContextOptions {
|
||||
|
||||
@@ -21,9 +21,24 @@ import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* This API is used for the Web API testing. You can use it to trigger API endpoints, configure micro-services, prepare
|
||||
* environment or the service to your e2e test. When used on {@code Page} or a {@code BrowserContext}, this API will automatically use
|
||||
* the cookies from the corresponding {@code BrowserContext}. This means that if you log in using this API, your e2e test will be
|
||||
* logged in and vice versa.
|
||||
* environment or the service to your e2e test.
|
||||
*
|
||||
* <p> Each Playwright browser context has associated with it {@code APIRequestContext} instance which shares cookie storage with the
|
||||
* browser context and can be accessed via {@link BrowserContext#request BrowserContext.request()} or {@link Page#request
|
||||
* Page.request()}. It is also possible to create a new APIRequestContext instance manually by calling {@link
|
||||
* APIRequest#newContext APIRequest.newContext()}.
|
||||
*
|
||||
* <p> **Cookie management**
|
||||
*
|
||||
* <p> {@code APIRequestContext} retuned by {@link BrowserContext#request BrowserContext.request()} and {@link Page#request
|
||||
* Page.request()} shares cookie storage with the corresponding {@code BrowserContext}. Each API request will have {@code Cookie}
|
||||
* header populated with the values from the browser context. If the API response contains {@code Set-Cookie} header it will
|
||||
* automatically update {@code BrowserContext} cookies and requests made from the page will pick them up. This means that if you
|
||||
* log in using this API, your e2e test will be logged in and vice versa.
|
||||
*
|
||||
* <p> If you want API requests to not interfere with the browser cookies you shoud create a new {@code APIRequestContext} by calling
|
||||
* {@link APIRequest#newContext APIRequest.newContext()}. Such {@code APIRequestContext} object will have its own isolated cookie
|
||||
* storage.
|
||||
*/
|
||||
public interface APIRequestContext {
|
||||
class StorageStateOptions {
|
||||
|
||||
@@ -1017,8 +1017,9 @@ public interface Browser extends AutoCloseable {
|
||||
Page newPage(NewPageOptions options);
|
||||
/**
|
||||
* <strong>NOTE:</strong> This API controls <a href="https://www.chromium.org/developers/how-tos/trace-event-profiling-tool">Chromium Tracing</a>
|
||||
* which is a low-level chromium-specific debugging tool. API to control <a href="../trace-viewer">Playwright Tracing</a>
|
||||
* could be found <a href="https://playwright.dev/java/docs/class-tracing">here</a>.
|
||||
* which is a low-level chromium-specific debugging tool. API to control <a
|
||||
* href="https://playwright.dev/java/docs/trace-viewer">Playwright Tracing</a> could be found <a
|
||||
* href="https://playwright.dev/java/docs/api/class-tracing">here</a>.
|
||||
*
|
||||
* <p> You can use {@link Browser#startTracing Browser.startTracing()} and {@link Browser#stopTracing Browser.stopTracing()} to
|
||||
* create a trace file that can be opened in Chrome DevTools performance panel.
|
||||
@@ -1036,8 +1037,9 @@ public interface Browser extends AutoCloseable {
|
||||
}
|
||||
/**
|
||||
* <strong>NOTE:</strong> This API controls <a href="https://www.chromium.org/developers/how-tos/trace-event-profiling-tool">Chromium Tracing</a>
|
||||
* which is a low-level chromium-specific debugging tool. API to control <a href="../trace-viewer">Playwright Tracing</a>
|
||||
* could be found <a href="https://playwright.dev/java/docs/class-tracing">here</a>.
|
||||
* which is a low-level chromium-specific debugging tool. API to control <a
|
||||
* href="https://playwright.dev/java/docs/trace-viewer">Playwright Tracing</a> could be found <a
|
||||
* href="https://playwright.dev/java/docs/api/class-tracing">here</a>.
|
||||
*
|
||||
* <p> You can use {@link Browser#startTracing Browser.startTracing()} and {@link Browser#stopTracing Browser.stopTracing()} to
|
||||
* create a trace file that can be opened in Chrome DevTools performance panel.
|
||||
@@ -1053,8 +1055,9 @@ public interface Browser extends AutoCloseable {
|
||||
}
|
||||
/**
|
||||
* <strong>NOTE:</strong> This API controls <a href="https://www.chromium.org/developers/how-tos/trace-event-profiling-tool">Chromium Tracing</a>
|
||||
* which is a low-level chromium-specific debugging tool. API to control <a href="../trace-viewer">Playwright Tracing</a>
|
||||
* could be found <a href="https://playwright.dev/java/docs/class-tracing">here</a>.
|
||||
* which is a low-level chromium-specific debugging tool. API to control <a
|
||||
* href="https://playwright.dev/java/docs/trace-viewer">Playwright Tracing</a> could be found <a
|
||||
* href="https://playwright.dev/java/docs/api/class-tracing">here</a>.
|
||||
*
|
||||
* <p> You can use {@link Browser#startTracing Browser.startTracing()} and {@link Browser#stopTracing Browser.stopTracing()} to
|
||||
* create a trace file that can be opened in Chrome DevTools performance panel.
|
||||
@@ -1070,8 +1073,9 @@ public interface Browser extends AutoCloseable {
|
||||
void startTracing(Page page, StartTracingOptions options);
|
||||
/**
|
||||
* <strong>NOTE:</strong> This API controls <a href="https://www.chromium.org/developers/how-tos/trace-event-profiling-tool">Chromium Tracing</a>
|
||||
* which is a low-level chromium-specific debugging tool. API to control <a href="../trace-viewer">Playwright Tracing</a>
|
||||
* could be found <a href="https://playwright.dev/java/docs/class-tracing">here</a>.
|
||||
* which is a low-level chromium-specific debugging tool. API to control <a
|
||||
* href="https://playwright.dev/java/docs/trace-viewer">Playwright Tracing</a> could be found <a
|
||||
* href="https://playwright.dev/java/docs/api/class-tracing">here</a>.
|
||||
*
|
||||
* <p> Returns the buffer with trace data.
|
||||
*/
|
||||
|
||||
@@ -497,7 +497,6 @@ public interface BrowserContext extends AutoCloseable {
|
||||
* <li> {@code "midi"}</li>
|
||||
* <li> {@code "midi-sysex"} (system-exclusive midi)</li>
|
||||
* <li> {@code "notifications"}</li>
|
||||
* <li> {@code "push"}</li>
|
||||
* <li> {@code "camera"}</li>
|
||||
* <li> {@code "microphone"}</li>
|
||||
* <li> {@code "background-sync"}</li>
|
||||
@@ -524,7 +523,6 @@ public interface BrowserContext extends AutoCloseable {
|
||||
* <li> {@code "midi"}</li>
|
||||
* <li> {@code "midi-sysex"} (system-exclusive midi)</li>
|
||||
* <li> {@code "notifications"}</li>
|
||||
* <li> {@code "push"}</li>
|
||||
* <li> {@code "camera"}</li>
|
||||
* <li> {@code "microphone"}</li>
|
||||
* <li> {@code "background-sync"}</li>
|
||||
|
||||
@@ -52,8 +52,7 @@ public interface BrowserType {
|
||||
*/
|
||||
public Double slowMo;
|
||||
/**
|
||||
* Maximum time in milliseconds to wait for the connection to be established. Defaults to {@code 30000} (30 seconds). Pass {@code 0} to
|
||||
* disable timeout.
|
||||
* Maximum time in milliseconds to wait for the connection to be established. Defaults to {@code 0} (no timeout).
|
||||
*/
|
||||
public Double timeout;
|
||||
|
||||
@@ -73,8 +72,7 @@ public interface BrowserType {
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Maximum time in milliseconds to wait for the connection to be established. Defaults to {@code 30000} (30 seconds). Pass {@code 0} to
|
||||
* disable timeout.
|
||||
* Maximum time in milliseconds to wait for the connection to be established. Defaults to {@code 0} (no timeout).
|
||||
*/
|
||||
public ConnectOptions setTimeout(double timeout) {
|
||||
this.timeout = timeout;
|
||||
@@ -130,7 +128,7 @@ public interface BrowserType {
|
||||
/**
|
||||
* Browser distribution channel. Supported values are "chrome", "chrome-beta", "chrome-dev", "chrome-canary", "msedge",
|
||||
* "msedge-beta", "msedge-dev", "msedge-canary". Read more about using <a
|
||||
* href="https://playwright.dev/java/docs/browsers/#google-chrome--microsoft-edge">Google Chrome and Microsoft Edge</a>.
|
||||
* href="https://playwright.dev/java/docs/browsers#google-chrome--microsoft-edge">Google Chrome and Microsoft Edge</a>.
|
||||
*/
|
||||
public Object channel;
|
||||
/**
|
||||
@@ -222,7 +220,7 @@ public interface BrowserType {
|
||||
/**
|
||||
* Browser distribution channel. Supported values are "chrome", "chrome-beta", "chrome-dev", "chrome-canary", "msedge",
|
||||
* "msedge-beta", "msedge-dev", "msedge-canary". Read more about using <a
|
||||
* href="https://playwright.dev/java/docs/browsers/#google-chrome--microsoft-edge">Google Chrome and Microsoft Edge</a>.
|
||||
* href="https://playwright.dev/java/docs/browsers#google-chrome--microsoft-edge">Google Chrome and Microsoft Edge</a>.
|
||||
*/
|
||||
public LaunchOptions setChannel(BrowserChannel channel) {
|
||||
this.channel = channel;
|
||||
@@ -231,7 +229,7 @@ public interface BrowserType {
|
||||
/**
|
||||
* Browser distribution channel. Supported values are "chrome", "chrome-beta", "chrome-dev", "chrome-canary", "msedge",
|
||||
* "msedge-beta", "msedge-dev", "msedge-canary". Read more about using <a
|
||||
* href="https://playwright.dev/java/docs/browsers/#google-chrome--microsoft-edge">Google Chrome and Microsoft Edge</a>.
|
||||
* href="https://playwright.dev/java/docs/browsers#google-chrome--microsoft-edge">Google Chrome and Microsoft Edge</a>.
|
||||
*/
|
||||
public LaunchOptions setChannel(String channel) {
|
||||
this.channel = channel;
|
||||
@@ -399,7 +397,7 @@ public interface BrowserType {
|
||||
/**
|
||||
* Browser distribution channel. Supported values are "chrome", "chrome-beta", "chrome-dev", "chrome-canary", "msedge",
|
||||
* "msedge-beta", "msedge-dev", "msedge-canary". Read more about using <a
|
||||
* href="https://playwright.dev/java/docs/browsers/#google-chrome--microsoft-edge">Google Chrome and Microsoft Edge</a>.
|
||||
* href="https://playwright.dev/java/docs/browsers#google-chrome--microsoft-edge">Google Chrome and Microsoft Edge</a>.
|
||||
*/
|
||||
public Object channel;
|
||||
/**
|
||||
@@ -625,7 +623,7 @@ public interface BrowserType {
|
||||
/**
|
||||
* Browser distribution channel. Supported values are "chrome", "chrome-beta", "chrome-dev", "chrome-canary", "msedge",
|
||||
* "msedge-beta", "msedge-dev", "msedge-canary". Read more about using <a
|
||||
* href="https://playwright.dev/java/docs/browsers/#google-chrome--microsoft-edge">Google Chrome and Microsoft Edge</a>.
|
||||
* href="https://playwright.dev/java/docs/browsers#google-chrome--microsoft-edge">Google Chrome and Microsoft Edge</a>.
|
||||
*/
|
||||
public LaunchPersistentContextOptions setChannel(BrowserChannel channel) {
|
||||
this.channel = channel;
|
||||
@@ -634,7 +632,7 @@ public interface BrowserType {
|
||||
/**
|
||||
* Browser distribution channel. Supported values are "chrome", "chrome-beta", "chrome-dev", "chrome-canary", "msedge",
|
||||
* "msedge-beta", "msedge-dev", "msedge-canary". Read more about using <a
|
||||
* href="https://playwright.dev/java/docs/browsers/#google-chrome--microsoft-edge">Google Chrome and Microsoft Edge</a>.
|
||||
* href="https://playwright.dev/java/docs/browsers#google-chrome--microsoft-edge">Google Chrome and Microsoft Edge</a>.
|
||||
*/
|
||||
public LaunchPersistentContextOptions setChannel(String channel) {
|
||||
this.channel = channel;
|
||||
@@ -971,7 +969,7 @@ public interface BrowserType {
|
||||
}
|
||||
}
|
||||
/**
|
||||
* This methods attaches Playwright to an existing browser instance.
|
||||
* This method attaches Playwright to an existing browser instance.
|
||||
*
|
||||
* @param wsEndpoint A browser websocket endpoint to connect to.
|
||||
*/
|
||||
@@ -979,13 +977,13 @@ public interface BrowserType {
|
||||
return connect(wsEndpoint, null);
|
||||
}
|
||||
/**
|
||||
* This methods attaches Playwright to an existing browser instance.
|
||||
* This method attaches Playwright to an existing browser instance.
|
||||
*
|
||||
* @param wsEndpoint A browser websocket endpoint to connect to.
|
||||
*/
|
||||
Browser connect(String wsEndpoint, ConnectOptions options);
|
||||
/**
|
||||
* This methods attaches Playwright to an existing browser instance using the Chrome DevTools Protocol.
|
||||
* This method attaches Playwright to an existing browser instance using the Chrome DevTools Protocol.
|
||||
*
|
||||
* <p> The default browser context is accessible via {@link Browser#contexts Browser.contexts()}.
|
||||
*
|
||||
@@ -998,7 +996,7 @@ public interface BrowserType {
|
||||
return connectOverCDP(endpointURL, null);
|
||||
}
|
||||
/**
|
||||
* This methods attaches Playwright to an existing browser instance using the Chrome DevTools Protocol.
|
||||
* This method attaches Playwright to an existing browser instance using the Chrome DevTools Protocol.
|
||||
*
|
||||
* <p> The default browser context is accessible via {@link Browser#contexts Browser.contexts()}.
|
||||
*
|
||||
|
||||
@@ -29,7 +29,7 @@ import static java.util.Arrays.asList;
|
||||
*/
|
||||
public class CLI {
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
Path driver = Driver.ensureDriverInstalled(Collections.emptyMap());
|
||||
Path driver = Driver.ensureDriverInstalled(Collections.emptyMap(), false);
|
||||
ProcessBuilder pb = new ProcessBuilder(driver.toString());
|
||||
pb.command().addAll(asList(args));
|
||||
Driver.setRequiredEnvironmentVariables(pb);
|
||||
|
||||
@@ -19,7 +19,28 @@ package com.microsoft.playwright;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* {@code ConsoleMessage} objects are dispatched by page via the {@link Page#onConsoleMessage Page.onConsoleMessage()} event.
|
||||
* {@code ConsoleMessage} objects are dispatched by page via the {@link Page#onConsoleMessage Page.onConsoleMessage()} event. For
|
||||
* each console messages logged in the page there will be corresponding event in the Playwright context.
|
||||
* <pre>{@code
|
||||
* // Listen for all System.out.printlns
|
||||
* page.onConsoleMessage(msg -> System.out.println(msg.text()));
|
||||
*
|
||||
* // Listen for all console events and handle errors
|
||||
* page.onConsoleMessage(msg -> {
|
||||
* if ("error".equals(msg.type()))
|
||||
* System.out.println("Error text: " + msg.text());
|
||||
* });
|
||||
*
|
||||
* // Get the next System.out.println
|
||||
* ConsoleMessage msg = page.waitForConsoleMessage(() -> {
|
||||
* // Issue console.log inside the page
|
||||
* page.evaluate("console.log('hello', 42, { foo: 'bar' });");
|
||||
* });
|
||||
*
|
||||
* // Deconstruct console.log arguments
|
||||
* msg.args().get(0).jsonValue() // hello
|
||||
* msg.args().get(1).jsonValue() // 42
|
||||
* }</pre>
|
||||
*/
|
||||
public interface ConsoleMessage {
|
||||
/**
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -74,50 +74,50 @@ public interface FileChooser {
|
||||
Page page();
|
||||
/**
|
||||
* Sets the value of the file input this chooser is associated with. If some of the {@code filePaths} are relative paths, then
|
||||
* they are resolved relative to the the current working directory. For empty array, clears the selected files.
|
||||
* they are resolved relative to the current working directory. For empty array, clears the selected files.
|
||||
*/
|
||||
default void setFiles(Path files) {
|
||||
setFiles(files, null);
|
||||
}
|
||||
/**
|
||||
* Sets the value of the file input this chooser is associated with. If some of the {@code filePaths} are relative paths, then
|
||||
* they are resolved relative to the the current working directory. For empty array, clears the selected files.
|
||||
* they are resolved relative to the current working directory. For empty array, clears the selected files.
|
||||
*/
|
||||
void setFiles(Path files, SetFilesOptions options);
|
||||
/**
|
||||
* Sets the value of the file input this chooser is associated with. If some of the {@code filePaths} are relative paths, then
|
||||
* they are resolved relative to the the current working directory. For empty array, clears the selected files.
|
||||
* they are resolved relative to the current working directory. For empty array, clears the selected files.
|
||||
*/
|
||||
default void setFiles(Path[] files) {
|
||||
setFiles(files, null);
|
||||
}
|
||||
/**
|
||||
* Sets the value of the file input this chooser is associated with. If some of the {@code filePaths} are relative paths, then
|
||||
* they are resolved relative to the the current working directory. For empty array, clears the selected files.
|
||||
* they are resolved relative to the current working directory. For empty array, clears the selected files.
|
||||
*/
|
||||
void setFiles(Path[] files, SetFilesOptions options);
|
||||
/**
|
||||
* Sets the value of the file input this chooser is associated with. If some of the {@code filePaths} are relative paths, then
|
||||
* they are resolved relative to the the current working directory. For empty array, clears the selected files.
|
||||
* they are resolved relative to the current working directory. For empty array, clears the selected files.
|
||||
*/
|
||||
default void setFiles(FilePayload files) {
|
||||
setFiles(files, null);
|
||||
}
|
||||
/**
|
||||
* Sets the value of the file input this chooser is associated with. If some of the {@code filePaths} are relative paths, then
|
||||
* they are resolved relative to the the current working directory. For empty array, clears the selected files.
|
||||
* they are resolved relative to the current working directory. For empty array, clears the selected files.
|
||||
*/
|
||||
void setFiles(FilePayload files, SetFilesOptions options);
|
||||
/**
|
||||
* Sets the value of the file input this chooser is associated with. If some of the {@code filePaths} are relative paths, then
|
||||
* they are resolved relative to the the current working directory. For empty array, clears the selected files.
|
||||
* they are resolved relative to the current working directory. For empty array, clears the selected files.
|
||||
*/
|
||||
default void setFiles(FilePayload[] files) {
|
||||
setFiles(files, null);
|
||||
}
|
||||
/**
|
||||
* Sets the value of the file input this chooser is associated with. If some of the {@code filePaths} are relative paths, then
|
||||
* they are resolved relative to the the current working directory. For empty array, clears the selected files.
|
||||
* they are resolved relative to the current working directory. For empty array, clears the selected files.
|
||||
*/
|
||||
void setFiles(FilePayload[] files, SetFilesOptions options);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -50,22 +50,42 @@ import java.util.regex.Pattern;
|
||||
public interface FrameLocator {
|
||||
class LocatorOptions {
|
||||
/**
|
||||
* Matches elements containing specified text somewhere inside, possibly in a child or a descendant element. For example,
|
||||
* {@code "Playwright"} matches {@code <article><div>Playwright</div></article>}.
|
||||
* Matches elements containing an element that matches an inner locator. Inner locator is queried against the outer one.
|
||||
* For example, {@code article} that has {@code text=Playwright} matches {@code <article><div>Playwright</div></article>}.
|
||||
*
|
||||
* <p> Note that outer and inner locators must belong to the same frame. Inner locator must not contain {@code FrameLocator}s.
|
||||
*/
|
||||
public Locator has;
|
||||
/**
|
||||
* Matches elements containing specified text somewhere inside, possibly in a child or a descendant element. When passed a
|
||||
* [string], matching is case-insensitive and searches for a substring. For example, {@code "Playwright"} matches
|
||||
* {@code <article><div>Playwright</div></article>}.
|
||||
*/
|
||||
public Object hasText;
|
||||
|
||||
/**
|
||||
* Matches elements containing specified text somewhere inside, possibly in a child or a descendant element. For example,
|
||||
* {@code "Playwright"} matches {@code <article><div>Playwright</div></article>}.
|
||||
* Matches elements containing an element that matches an inner locator. Inner locator is queried against the outer one.
|
||||
* For example, {@code article} that has {@code text=Playwright} matches {@code <article><div>Playwright</div></article>}.
|
||||
*
|
||||
* <p> Note that outer and inner locators must belong to the same frame. Inner locator must not contain {@code FrameLocator}s.
|
||||
*/
|
||||
public LocatorOptions setHas(Locator has) {
|
||||
this.has = has;
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Matches elements containing specified text somewhere inside, possibly in a child or a descendant element. When passed a
|
||||
* [string], matching is case-insensitive and searches for a substring. For example, {@code "Playwright"} matches
|
||||
* {@code <article><div>Playwright</div></article>}.
|
||||
*/
|
||||
public LocatorOptions setHasText(String hasText) {
|
||||
this.hasText = hasText;
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Matches elements containing specified text somewhere inside, possibly in a child or a descendant element. For example,
|
||||
* {@code "Playwright"} matches {@code <article><div>Playwright</div></article>}.
|
||||
* Matches elements containing specified text somewhere inside, possibly in a child or a descendant element. When passed a
|
||||
* [string], matching is case-insensitive and searches for a substring. For example, {@code "Playwright"} matches
|
||||
* {@code <article><div>Playwright</div></article>}.
|
||||
*/
|
||||
public LocatorOptions setHasText(Pattern hasText) {
|
||||
this.hasText = hasText;
|
||||
@@ -80,7 +100,7 @@ public interface FrameLocator {
|
||||
* When working with iframes, you can create a frame locator that will enter the iframe and allow selecting elements in
|
||||
* that iframe.
|
||||
*
|
||||
* @param selector A selector to use when resolving DOM element. See <a href="https://playwright.dev/java/docs/selectors/">working with
|
||||
* @param selector A selector to use when resolving DOM element. See <a href="https://playwright.dev/java/docs/selectors">working with
|
||||
* selectors</a> for more details.
|
||||
*/
|
||||
FrameLocator frameLocator(String selector);
|
||||
@@ -91,7 +111,7 @@ public interface FrameLocator {
|
||||
/**
|
||||
* The method finds an element matching the specified selector in the FrameLocator's subtree.
|
||||
*
|
||||
* @param selector A selector to use when resolving DOM element. See <a href="https://playwright.dev/java/docs/selectors/">working with
|
||||
* @param selector A selector to use when resolving DOM element. See <a href="https://playwright.dev/java/docs/selectors">working with
|
||||
* selectors</a> for more details.
|
||||
*/
|
||||
default Locator locator(String selector) {
|
||||
@@ -100,12 +120,12 @@ public interface FrameLocator {
|
||||
/**
|
||||
* The method finds an element matching the specified selector in the FrameLocator's subtree.
|
||||
*
|
||||
* @param selector A selector to use when resolving DOM element. See <a href="https://playwright.dev/java/docs/selectors/">working with
|
||||
* @param selector A selector to use when resolving DOM element. See <a href="https://playwright.dev/java/docs/selectors">working with
|
||||
* selectors</a> for more details.
|
||||
*/
|
||||
Locator locator(String selector, LocatorOptions options);
|
||||
/**
|
||||
* Returns locator to the n-th matching frame.
|
||||
* Returns locator to the n-th matching frame. It's zero based, {@code nth(0)} selects the first frame.
|
||||
*/
|
||||
FrameLocator nth(int index);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -122,12 +122,12 @@ public interface Mouse {
|
||||
}
|
||||
class MoveOptions {
|
||||
/**
|
||||
* defaults to 1. Sends intermediate {@code mousemove} events.
|
||||
* Defaults to 1. Sends intermediate {@code mousemove} events.
|
||||
*/
|
||||
public Integer steps;
|
||||
|
||||
/**
|
||||
* defaults to 1. Sends intermediate {@code mousemove} events.
|
||||
* Defaults to 1. Sends intermediate {@code mousemove} events.
|
||||
*/
|
||||
public MoveOptions setSteps(int steps) {
|
||||
this.steps = steps;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -70,7 +70,7 @@ public interface Playwright extends AutoCloseable {
|
||||
APIRequest request();
|
||||
/**
|
||||
* Selectors can be used to install custom selector engines. See <a
|
||||
* href="https://playwright.dev/java/docs/selectors/">Working with selectors</a> for more information.
|
||||
* href="https://playwright.dev/java/docs/selectors">Working with selectors</a> for more information.
|
||||
*/
|
||||
Selectors selectors();
|
||||
/**
|
||||
|
||||
@@ -22,6 +22,8 @@ import java.util.*;
|
||||
/**
|
||||
* Whenever a network route is set up with {@link Page#route Page.route()} or {@link BrowserContext#route
|
||||
* BrowserContext.route()}, the {@code Route} object allows to handle the route.
|
||||
*
|
||||
* <p> Learn more about <a href="https://playwright.dev/java/docs/network">networking</a>.
|
||||
*/
|
||||
public interface Route {
|
||||
class ResumeOptions {
|
||||
@@ -100,6 +102,11 @@ public interface Route {
|
||||
* is resolved relative to the current working directory.
|
||||
*/
|
||||
public Path path;
|
||||
/**
|
||||
* {@code APIResponse} to fulfill route's request with. Individual fields of the response (such as headers) can be overridden
|
||||
* using fulfill options.
|
||||
*/
|
||||
public APIResponse response;
|
||||
/**
|
||||
* Response status code, defaults to {@code 200}.
|
||||
*/
|
||||
@@ -141,6 +148,14 @@ public interface Route {
|
||||
this.path = path;
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* {@code APIResponse} to fulfill route's request with. Individual fields of the response (such as headers) can be overridden
|
||||
* using fulfill options.
|
||||
*/
|
||||
public FulfillOptions setResponse(APIResponse response) {
|
||||
this.response = response;
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Response status code, defaults to {@code 200}.
|
||||
*/
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* Selectors can be used to install custom selector engines. See <a
|
||||
* href="https://playwright.dev/java/docs/selectors/">Working with selectors</a> for more information.
|
||||
* href="https://playwright.dev/java/docs/selectors">Working with selectors</a> for more information.
|
||||
*/
|
||||
public interface Selectors {
|
||||
class RegisterOptions {
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* API for collecting and saving Playwright traces. Playwright traces can be opened in <a
|
||||
* href="https://playwright.dev/java/docs/trace-viewer/">Trace Viewer</a> after Playwright script runs.
|
||||
* href="https://playwright.dev/java/docs/trace-viewer">Trace Viewer</a> after Playwright script runs.
|
||||
*
|
||||
* <p> Start recording a trace before performing actions. At the end, stop tracing and save it to a file.
|
||||
* <pre>{@code
|
||||
@@ -47,12 +47,17 @@ public interface Tracing {
|
||||
*/
|
||||
public Boolean screenshots;
|
||||
/**
|
||||
* Whether to capture DOM snapshot on every action.
|
||||
* If this option is true tracing will
|
||||
* <ul>
|
||||
* <li> capture DOM snapshot on every action</li>
|
||||
* <li> record network activity</li>
|
||||
* </ul>
|
||||
*/
|
||||
public Boolean snapshots;
|
||||
/**
|
||||
* Whether to include source files for trace actions. List of the directories with source code for the application must be
|
||||
* provided via {@code PLAYWRIGHT_JAVA_SRC} environment variable.
|
||||
* provided via {@code PLAYWRIGHT_JAVA_SRC} environment variable (the paths should be separated by ';' on Windows and by ':' on
|
||||
* other platforms).
|
||||
*/
|
||||
public Boolean sources;
|
||||
/**
|
||||
@@ -76,7 +81,11 @@ public interface Tracing {
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Whether to capture DOM snapshot on every action.
|
||||
* If this option is true tracing will
|
||||
* <ul>
|
||||
* <li> capture DOM snapshot on every action</li>
|
||||
* <li> record network activity</li>
|
||||
* </ul>
|
||||
*/
|
||||
public StartOptions setSnapshots(boolean snapshots) {
|
||||
this.snapshots = snapshots;
|
||||
@@ -84,7 +93,8 @@ public interface Tracing {
|
||||
}
|
||||
/**
|
||||
* Whether to include source files for trace actions. List of the directories with source code for the application must be
|
||||
* provided via {@code PLAYWRIGHT_JAVA_SRC} environment variable.
|
||||
* provided via {@code PLAYWRIGHT_JAVA_SRC} environment variable (the paths should be separated by ';' on Windows and by ':' on
|
||||
* other platforms).
|
||||
*/
|
||||
public StartOptions setSources(boolean sources) {
|
||||
this.sources = sources;
|
||||
|
||||
+1
-1
@@ -39,7 +39,7 @@ package com.microsoft.playwright.assertions;
|
||||
public interface APIResponseAssertions {
|
||||
/**
|
||||
* Makes the assertion check for the opposite condition. For example, this code tests that the response status is not
|
||||
* successfull:
|
||||
* successful:
|
||||
* <pre>{@code
|
||||
* assertThat(response).not().isOK();
|
||||
* }</pre>
|
||||
|
||||
+16
-8
@@ -328,7 +328,11 @@ public interface LocatorAssertions {
|
||||
*/
|
||||
void isChecked(IsCheckedOptions options);
|
||||
/**
|
||||
* Ensures the {@code Locator} points to a disabled element.
|
||||
* Ensures the {@code Locator} points to a disabled element. Element is disabled if it has "disabled" attribute or is disabled
|
||||
* via <a
|
||||
* href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-disabled">'aria-disabled'</a>.
|
||||
* Note that only native control elements such as HTML {@code button}, {@code input}, {@code select}, {@code textarea}, {@code option}, {@code optgroup} can be
|
||||
* disabled by setting "disabled" attribute. "disabled" attribute on other elements is ignored by the browser.
|
||||
* <pre>{@code
|
||||
* assertThat(page.locator("button.submit")).isDisabled();
|
||||
* }</pre>
|
||||
@@ -337,7 +341,11 @@ public interface LocatorAssertions {
|
||||
isDisabled(null);
|
||||
}
|
||||
/**
|
||||
* Ensures the {@code Locator} points to a disabled element.
|
||||
* Ensures the {@code Locator} points to a disabled element. Element is disabled if it has "disabled" attribute or is disabled
|
||||
* via <a
|
||||
* href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-disabled">'aria-disabled'</a>.
|
||||
* Note that only native control elements such as HTML {@code button}, {@code input}, {@code select}, {@code textarea}, {@code option}, {@code optgroup} can be
|
||||
* disabled by setting "disabled" attribute. "disabled" attribute on other elements is ignored by the browser.
|
||||
* <pre>{@code
|
||||
* assertThat(page.locator("button.submit")).isDisabled();
|
||||
* }</pre>
|
||||
@@ -409,7 +417,7 @@ public interface LocatorAssertions {
|
||||
void isFocused(IsFocusedOptions options);
|
||||
/**
|
||||
* Ensures the {@code Locator} points to a hidden DOM node, which is the opposite of <a
|
||||
* href="https://playwright.dev/java/docs/actionability/#visible">visible</a>.
|
||||
* href="https://playwright.dev/java/docs/api/actionability#visible">visible</a>.
|
||||
* <pre>{@code
|
||||
* assertThat(page.locator(".my-element")).isHidden();
|
||||
* }</pre>
|
||||
@@ -419,27 +427,27 @@ public interface LocatorAssertions {
|
||||
}
|
||||
/**
|
||||
* Ensures the {@code Locator} points to a hidden DOM node, which is the opposite of <a
|
||||
* href="https://playwright.dev/java/docs/actionability/#visible">visible</a>.
|
||||
* href="https://playwright.dev/java/docs/api/actionability#visible">visible</a>.
|
||||
* <pre>{@code
|
||||
* assertThat(page.locator(".my-element")).isHidden();
|
||||
* }</pre>
|
||||
*/
|
||||
void isHidden(IsHiddenOptions options);
|
||||
/**
|
||||
* Ensures the {@code Locator} points to a <a href="https://playwright.dev/java/docs/actionability/#visible">visible</a> DOM
|
||||
* Ensures the {@code Locator} points to a <a href="https://playwright.dev/java/docs/api/actionability#visible">visible</a> DOM
|
||||
* node.
|
||||
* <pre>{@code
|
||||
* assertThat(page.locator(".my-element")).toBeVisible();
|
||||
* assertThat(page.locator(".my-element")).isVisible();
|
||||
* }</pre>
|
||||
*/
|
||||
default void isVisible() {
|
||||
isVisible(null);
|
||||
}
|
||||
/**
|
||||
* Ensures the {@code Locator} points to a <a href="https://playwright.dev/java/docs/actionability/#visible">visible</a> DOM
|
||||
* Ensures the {@code Locator} points to a <a href="https://playwright.dev/java/docs/api/actionability#visible">visible</a> DOM
|
||||
* node.
|
||||
* <pre>{@code
|
||||
* assertThat(page.locator(".my-element")).toBeVisible();
|
||||
* assertThat(page.locator(".my-element")).isVisible();
|
||||
* }</pre>
|
||||
*/
|
||||
void isVisible(IsVisibleOptions options);
|
||||
|
||||
+2
-11
@@ -24,8 +24,8 @@ import com.microsoft.playwright.impl.LocatorAssertionsImpl;
|
||||
import com.microsoft.playwright.impl.PageAssertionsImpl;
|
||||
|
||||
/**
|
||||
* The {@code PlaywrightAssertions} class provides convenience methods for creating assertions that will wait until the expected
|
||||
* condition is met.
|
||||
* Playwright gives you Web-First Assertions with convenience methods for creating assertions that will wait and retry
|
||||
* until the expected condition is met.
|
||||
*
|
||||
* <p> Consider the following example:
|
||||
* <pre>{@code
|
||||
@@ -48,15 +48,6 @@ import com.microsoft.playwright.impl.PageAssertionsImpl;
|
||||
* You can pass this timeout as an option.
|
||||
*
|
||||
* <p> By default, the timeout for assertions is set to 5 seconds.
|
||||
*
|
||||
* <p> To use Playwright assertions add the following dependency into the {@code pom.xml} of your Maven project:
|
||||
* <pre>{@code
|
||||
* <dependency>
|
||||
* <groupId>com.microsoft.playwright</groupId>
|
||||
* <artifactId>assertions</artifactId>
|
||||
* <version>1.17.0</version>
|
||||
* </dependency>
|
||||
* }</pre>
|
||||
*/
|
||||
public interface PlaywrightAssertions {
|
||||
/**
|
||||
|
||||
@@ -21,8 +21,11 @@ import static com.microsoft.playwright.impl.Serialization.*;
|
||||
import static com.microsoft.playwright.impl.Utils.toFilePayload;
|
||||
|
||||
class APIRequestContextImpl extends ChannelOwner implements APIRequestContext {
|
||||
private final TracingImpl tracing;
|
||||
|
||||
APIRequestContextImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
|
||||
super(parent, type, guid, initializer);
|
||||
this.tracing = connection.getExistingObject(initializer.getAsJsonObject("tracing").get("guid").getAsString());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -34,7 +34,7 @@ import static com.microsoft.playwright.impl.Utils.isSafeCloseError;
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
class APIResponseImpl implements APIResponse {
|
||||
private final APIRequestContextImpl context;
|
||||
final APIRequestContextImpl context;
|
||||
private final JsonObject initializer;
|
||||
private final RawHeaders headers;
|
||||
|
||||
@@ -109,7 +109,7 @@ class APIResponseImpl implements APIResponse {
|
||||
return initializer.get("url").getAsString();
|
||||
}
|
||||
|
||||
private String fetchUid() {
|
||||
String fetchUid() {
|
||||
return initializer.get("fetchUid").getAsString();
|
||||
}
|
||||
|
||||
|
||||
@@ -67,7 +67,10 @@ class AssertionsBase {
|
||||
if (expected == null) {
|
||||
throw new AssertionFailedError(message + log);
|
||||
}
|
||||
throw new AssertionFailedError(message + log, formatValue(expected), formatValue(actual));
|
||||
ValueWrapper expectedValue = formatValue(expected);
|
||||
ValueWrapper actualValue = formatValue(actual);
|
||||
message += ": " + expectedValue.getStringRepresentation() + "\nReceived: " + actualValue.getStringRepresentation() + "\n";
|
||||
throw new AssertionFailedError(message + log, expectedValue, actualValue);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -55,7 +55,6 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext {
|
||||
Path videosDir;
|
||||
URL baseUrl;
|
||||
Path recordHarPath;
|
||||
LocalUtils localUtils;
|
||||
|
||||
enum EventType {
|
||||
CLOSE,
|
||||
@@ -73,7 +72,8 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext {
|
||||
} else {
|
||||
browser = null;
|
||||
}
|
||||
this.tracing = new TracingImpl(this);
|
||||
this.tracing = connection.getExistingObject(initializer.getAsJsonObject("tracing").get("guid").getAsString());
|
||||
tracing.isRemote = browser != null && browser.isRemote;
|
||||
this.request = connection.getExistingObject(initializer.getAsJsonObject("APIRequestContext").get("guid").getAsString());
|
||||
}
|
||||
|
||||
@@ -429,7 +429,7 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tracing tracing() {
|
||||
public TracingImpl tracing() {
|
||||
return tracing;
|
||||
}
|
||||
|
||||
@@ -560,4 +560,11 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext {
|
||||
}
|
||||
listeners.notify(EventType.CLOSE, this);
|
||||
}
|
||||
|
||||
WritableStream createTempFile(String name) {
|
||||
JsonObject params = new JsonObject();
|
||||
params.addProperty("name", name);
|
||||
JsonObject json = sendMessage("createTempFile", params).getAsJsonObject();
|
||||
return connection.getExistingObject(json.getAsJsonObject("writableStream").get("guid").getAsString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,7 +172,7 @@ class BrowserImpl extends ChannelOwner implements Browser {
|
||||
context.setBaseUrl(options.baseURL);
|
||||
}
|
||||
context.recordHarPath = options.recordHarPath;
|
||||
context.localUtils = localUtils;
|
||||
context.tracing().localUtils = localUtils;
|
||||
contexts.add(context);
|
||||
return context;
|
||||
}
|
||||
|
||||
@@ -64,6 +64,22 @@ class BrowserTypeImpl extends ChannelOwner implements BrowserType {
|
||||
// We don't use gson() here as the headers map should be serialized to a json object.
|
||||
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
|
||||
params.addProperty("wsEndpoint", wsEndpoint);
|
||||
|
||||
if (!params.has("headers")) {
|
||||
params.add("headers", new JsonObject());
|
||||
}
|
||||
JsonObject headers = params.get("headers").getAsJsonObject();
|
||||
boolean foundBrowserHeader = false;
|
||||
for (String name : headers.keySet()) {
|
||||
if ("x-playwright-browser".equalsIgnoreCase(name)) {
|
||||
foundBrowserHeader = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!foundBrowserHeader) {
|
||||
headers.addProperty("x-playwright-browser", name());
|
||||
}
|
||||
|
||||
JsonObject json = sendMessage("connect", params).getAsJsonObject();
|
||||
JsonPipe pipe = connection.getExistingObject(json.getAsJsonObject("pipe").get("guid").getAsString());
|
||||
Connection connection = new Connection(pipe);
|
||||
@@ -180,7 +196,7 @@ class BrowserTypeImpl extends ChannelOwner implements BrowserType {
|
||||
context.setBaseUrl(options.baseURL);
|
||||
}
|
||||
context.recordHarPath = options.recordHarPath;
|
||||
context.localUtils = localUtils;
|
||||
context.tracing().localUtils = localUtils;
|
||||
return context;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@ import com.microsoft.playwright.PlaywrightException;
|
||||
import com.microsoft.playwright.TimeoutError;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -85,8 +84,7 @@ public class Connection {
|
||||
}
|
||||
this.transport = transport;
|
||||
root = new Root(this);
|
||||
String srcRoot = System.getenv("PLAYWRIGHT_JAVA_SRC");
|
||||
stackTraceCollector = (srcRoot == null) ? null: new StackTraceCollector(Paths.get(srcRoot));
|
||||
stackTraceCollector = StackTraceCollector.createFromEnv();
|
||||
}
|
||||
|
||||
boolean isCollectingStacks() {
|
||||
@@ -121,11 +119,15 @@ public class Connection {
|
||||
message.addProperty("method", method);
|
||||
message.add("params", params);
|
||||
JsonObject metadata = new JsonObject();
|
||||
if (stackTraceCollector != null) {
|
||||
metadata.add("stack", stackTraceCollector.currentStackTrace());
|
||||
}
|
||||
if (apiName != null) {
|
||||
if (apiName == null) {
|
||||
metadata.addProperty("internal", true);
|
||||
} else {
|
||||
metadata.addProperty("apiName", apiName);
|
||||
// All but first message in an API call are considered internal and will be hidden from the inspector.
|
||||
apiName = null;
|
||||
if (stackTraceCollector != null) {
|
||||
metadata.add("stack", stackTraceCollector.currentStackTrace());
|
||||
}
|
||||
}
|
||||
message.add("metadata", metadata);
|
||||
transport.send(message);
|
||||
@@ -291,12 +293,18 @@ public class Connection {
|
||||
case "Selectors":
|
||||
result = new SelectorsImpl(parent, type, guid, initializer);
|
||||
break;
|
||||
case "Tracing":
|
||||
result = new TracingImpl(parent, type, guid, initializer);
|
||||
break;
|
||||
case "WebSocket":
|
||||
result = new WebSocketImpl(parent, type, guid, initializer);
|
||||
break;
|
||||
case "Worker":
|
||||
result = new WorkerImpl(parent, type, guid, initializer);
|
||||
break;
|
||||
case "WritableStream":
|
||||
result = new WritableStream(parent, type, guid, initializer);
|
||||
break;
|
||||
default:
|
||||
throw new PlaywrightException("Unknown type " + type);
|
||||
}
|
||||
|
||||
@@ -33,7 +33,8 @@ import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
import static com.microsoft.playwright.impl.Serialization.*;
|
||||
import static com.microsoft.playwright.impl.Utils.convertType;
|
||||
import static com.microsoft.playwright.impl.Utils.*;
|
||||
import static com.microsoft.playwright.impl.Utils.addLargeFileUploadParams;
|
||||
import static com.microsoft.playwright.options.ScreenshotType.JPEG;
|
||||
import static com.microsoft.playwright.options.ScreenshotType.PNG;
|
||||
|
||||
@@ -299,7 +300,7 @@ public class ElementHandleImpl extends JSHandleImpl implements ElementHandle {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Frame ownerFrame() {
|
||||
public FrameImpl ownerFrame() {
|
||||
return withLogging("ElementHandle.ownerFrame", () -> {
|
||||
JsonObject json = sendMessage("ownerFrame").getAsJsonObject();
|
||||
if (!json.has("frame")) {
|
||||
@@ -455,7 +456,24 @@ public class ElementHandleImpl extends JSHandleImpl implements ElementHandle {
|
||||
|
||||
@Override
|
||||
public void setInputFiles(Path[] files, SetInputFilesOptions options) {
|
||||
setInputFiles(Utils.toFilePayloads(files), options);
|
||||
withLogging("ElementHandle.setInputFiles", () -> setInputFilesImpl(files, options));
|
||||
}
|
||||
|
||||
void setInputFilesImpl(Path[] files, SetInputFilesOptions options) {
|
||||
FrameImpl frame = ownerFrame();
|
||||
if (frame == null) {
|
||||
throw new Error("Cannot set input files to detached element");
|
||||
}
|
||||
if (hasLargeFile(files)) {
|
||||
if (options == null) {
|
||||
options = new SetInputFilesOptions();
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
addLargeFileUploadParams(files, params, frame.page().context());
|
||||
sendMessage("setInputFilePaths", params);
|
||||
} else {
|
||||
setInputFilesImpl(Utils.toFilePayloads(files), options);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -469,6 +487,7 @@ public class ElementHandleImpl extends JSHandleImpl implements ElementHandle {
|
||||
}
|
||||
|
||||
void setInputFilesImpl(FilePayload[] files, SetInputFilesOptions options) {
|
||||
checkFilePayloadSize(files);
|
||||
if (options == null) {
|
||||
options = new SetInputFilesOptions();
|
||||
}
|
||||
|
||||
@@ -58,7 +58,8 @@ class FileChooserImpl implements FileChooser {
|
||||
|
||||
@Override
|
||||
public void setFiles(Path[] files, SetFilesOptions options) {
|
||||
setFiles(Utils.toFilePayloads(files), options);
|
||||
page.withLogging("FileChooser.setInputFiles",
|
||||
() -> element.setInputFilesImpl(files, convertType(options, ElementHandle.SetInputFilesOptions.class)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -69,6 +70,6 @@ class FileChooserImpl implements FileChooser {
|
||||
@Override
|
||||
public void setFiles(FilePayload[] files, SetFilesOptions options) {
|
||||
page.withLogging("FileChooser.setInputFiles",
|
||||
() -> element.setInputFilesImpl(files, convertType(options, ElementHandle.SetInputFilesOptions.class)));
|
||||
() -> element.setInputFilesImpl(files, convertType(options, ElementHandle.SetInputFilesOptions.class)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import com.microsoft.playwright.*;
|
||||
import com.microsoft.playwright.options.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -31,7 +32,7 @@ import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static com.microsoft.playwright.impl.Utils.convertType;
|
||||
import static com.microsoft.playwright.impl.Utils.*;
|
||||
import static com.microsoft.playwright.options.WaitUntilState.*;
|
||||
import static com.microsoft.playwright.impl.Serialization.*;
|
||||
|
||||
@@ -586,7 +587,7 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page page() {
|
||||
public PageImpl page() {
|
||||
return page;
|
||||
}
|
||||
|
||||
@@ -686,21 +687,32 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
withLogging("Frame.setInputFiles", () -> setInputFilesImpl(selector, files, options));
|
||||
}
|
||||
|
||||
void setInputFilesImpl(String selector, Path[] files, SetInputFilesOptions options) {
|
||||
if (hasLargeFile(files)) {
|
||||
if (options == null) {
|
||||
options = new SetInputFilesOptions();
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
addLargeFileUploadParams(files, params, page.context());
|
||||
params.addProperty("selector", selector);
|
||||
sendMessage("setInputFilePaths", params);
|
||||
} else {
|
||||
setInputFilesImpl(selector, Utils.toFilePayloads(files), options);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInputFiles(String selector, FilePayload files, SetInputFilesOptions options) {
|
||||
setInputFiles(selector, new FilePayload[]{files}, options);
|
||||
}
|
||||
|
||||
void setInputFilesImpl(String selector, Path[] files, SetInputFilesOptions options) {
|
||||
setInputFiles(selector, Utils.toFilePayloads(files), options);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInputFiles(String selector, FilePayload[] files, SetInputFilesOptions options) {
|
||||
withLogging("Frame.setInputFiles", () -> setInputFilesImpl(selector, files, options));
|
||||
}
|
||||
|
||||
void setInputFilesImpl(String selector, FilePayload[] files, SetInputFilesOptions options) {
|
||||
checkFilePayloadSize(files);
|
||||
if (options == null) {
|
||||
options = new SetInputFilesOptions();
|
||||
}
|
||||
@@ -1024,6 +1036,12 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
return result.get("value").getAsInt();
|
||||
}
|
||||
|
||||
void highlightImpl(String selector) {
|
||||
JsonObject params = new JsonObject();
|
||||
params.addProperty("selector", selector);
|
||||
sendMessage("highlight", params);
|
||||
}
|
||||
|
||||
protected void handleEvent(String event, JsonObject params) {
|
||||
if ("loadstate".equals(event)) {
|
||||
JsonElement add = params.get("add");
|
||||
|
||||
@@ -257,7 +257,8 @@ public class LocatorAssertionsImpl extends AssertionsBase implements LocatorAsse
|
||||
|
||||
@Override
|
||||
public void isChecked(IsCheckedOptions options) {
|
||||
expectTrue("to.be.checked", "Locator expected to be checked", convertType(options, FrameExpectOptions.class));
|
||||
String expression = (options != null && options.checked != null && !options.checked) ? "to.be.unchecked" : "to.be.checked";
|
||||
expectTrue(expression, "Locator expected to be checked", convertType(options, FrameExpectOptions.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.microsoft.playwright.impl;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.microsoft.playwright.*;
|
||||
@@ -9,8 +8,11 @@ import com.microsoft.playwright.options.FilePayload;
|
||||
import com.microsoft.playwright.options.SelectOption;
|
||||
import com.microsoft.playwright.options.WaitForSelectorState;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.file.Path;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -22,16 +24,56 @@ class LocatorImpl implements Locator {
|
||||
private final FrameImpl frame;
|
||||
private final String selector;
|
||||
|
||||
private static class Filters {
|
||||
private final Map<Field, String> filterFieldToEngine = new LinkedHashMap<>();
|
||||
private void addFilter(String name, String engine) throws NoSuchFieldException {
|
||||
filterFieldToEngine.put(LocatorOptions.class.getField(name), engine);
|
||||
}
|
||||
{
|
||||
try {
|
||||
addFilter("has", "has");
|
||||
// addFilter("leftOf", "left-of");
|
||||
// addFilter("rightOf", "right-of");
|
||||
// addFilter("above", "above");
|
||||
// addFilter("below", "below");
|
||||
// addFilter("near", "near");
|
||||
} catch (NoSuchFieldException e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
}
|
||||
String addFiltersToSelector(String selector, LocatorOptions options, Frame frame) {
|
||||
try {
|
||||
for (Map.Entry<Field, String> p : filterFieldToEngine.entrySet()) {
|
||||
LocatorImpl filter = (LocatorImpl) p.getKey().get(options);
|
||||
if (filter == null) {
|
||||
continue;
|
||||
}
|
||||
if (filter.frame != frame) {
|
||||
throw new PlaywrightException("Inner '" + p.getKey().getName() + "' locator must belong to the same frame.");
|
||||
}
|
||||
selector += " >> " + p.getValue() + "=" + gson().toJson(filter.selector);
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new PlaywrightException("Unexpected options", e);
|
||||
}
|
||||
return selector;
|
||||
}
|
||||
}
|
||||
private static final Filters filters = new Filters();
|
||||
|
||||
public LocatorImpl(FrameImpl frame, String selector, LocatorOptions options) {
|
||||
this.frame = frame;
|
||||
if (options != null && options.hasText != null) {
|
||||
if (options.hasText instanceof Pattern) {
|
||||
Pattern pattern = (Pattern) options.hasText;
|
||||
selector += " >> :scope:text-matches(" + escapeWithQuotes(pattern.pattern()) + ", \"" + toJsRegexFlags(pattern) + "\")";
|
||||
} else if (options.hasText instanceof String) {
|
||||
String text = (String) options.hasText;
|
||||
selector += " >> :scope:has-text(" + escapeWithQuotes(text)+ ")";
|
||||
if (options != null) {
|
||||
if (options.hasText != null) {
|
||||
if (options.hasText instanceof Pattern) {
|
||||
Pattern pattern = (Pattern) options.hasText;
|
||||
selector += " >> :scope:text-matches(" + escapeWithQuotes(pattern.pattern()) + ", \"" + toJsRegexFlags(pattern) + "\")";
|
||||
} else if (options.hasText instanceof String) {
|
||||
String text = (String) options.hasText;
|
||||
selector += " >> :scope:has-text(" + escapeWithQuotes(text) + ")";
|
||||
}
|
||||
}
|
||||
selector = filters.addFiltersToSelector(selector, options, frame);
|
||||
}
|
||||
this.selector = selector;
|
||||
}
|
||||
@@ -160,6 +202,11 @@ class LocatorImpl implements Locator {
|
||||
frame.fill(selector, value, convertType(options, Frame.FillOptions.class).setStrict(true));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Locator filter(FilterOptions options) {
|
||||
return new LocatorImpl(frame, selector, convertType(options,LocatorOptions.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Locator first() {
|
||||
return new LocatorImpl(frame, selector + " >> nth=0", null);
|
||||
@@ -186,6 +233,11 @@ class LocatorImpl implements Locator {
|
||||
return frame.getAttribute(selector, name, convertType(options, Frame.GetAttributeOptions.class).setStrict(true));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void highlight() {
|
||||
frame.highlightImpl(selector);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hover(HoverOptions options) {
|
||||
if (options == null) {
|
||||
@@ -281,6 +333,11 @@ class LocatorImpl implements Locator {
|
||||
return new LocatorImpl(frame, selector + " >> nth=" + index, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page page() {
|
||||
return frame.page();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void press(String key, PressOptions options) {
|
||||
if (options == null) {
|
||||
@@ -451,6 +508,15 @@ class LocatorImpl implements Locator {
|
||||
return frame.withLogging("Locator.expect", () -> expectImpl(expression, options));
|
||||
}
|
||||
|
||||
JsonObject toProtocol() {
|
||||
JsonObject result = new JsonObject();
|
||||
JsonObject frameJson = new JsonObject();
|
||||
frameJson.addProperty("guid", frame.guid);
|
||||
result.add("frame", frameJson);
|
||||
result.addProperty("selector", selector);
|
||||
return result;
|
||||
}
|
||||
|
||||
private FrameExpectResult expectImpl(String expression, FrameExpectOptions options) {
|
||||
if (options == null) {
|
||||
options = new FrameExpectOptions();
|
||||
|
||||
@@ -40,6 +40,7 @@ public class PageAssertionsImpl extends AssertionsBase implements PageAssertions
|
||||
public void hasTitle(String title, HasTitleOptions options) {
|
||||
ExpectedTextValue expected = new ExpectedTextValue();
|
||||
expected.string = title;
|
||||
expected.normalizeWhiteSpace = true;
|
||||
expectImpl("to.have.title", expected, title, "Page title expected to be", convertType(options, FrameExpectOptions.class));
|
||||
}
|
||||
|
||||
|
||||
@@ -28,11 +28,11 @@ import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static com.microsoft.playwright.impl.Serialization.gson;
|
||||
import static com.microsoft.playwright.impl.Utils.convertType;
|
||||
import static com.microsoft.playwright.impl.Utils.isSafeCloseError;
|
||||
import static com.microsoft.playwright.options.ScreenshotType.JPEG;
|
||||
import static com.microsoft.playwright.options.ScreenshotType.PNG;
|
||||
import static com.microsoft.playwright.impl.Serialization.gson;
|
||||
import static com.microsoft.playwright.impl.Utils.isSafeCloseError;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static java.nio.file.Files.readAllBytes;
|
||||
import static java.util.Arrays.asList;
|
||||
@@ -1034,8 +1034,18 @@ public class PageImpl extends ChannelOwner implements Page {
|
||||
}
|
||||
}
|
||||
}
|
||||
List<Locator> mask = options.mask;
|
||||
options.mask = null;
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
options.mask = mask;
|
||||
params.remove("path");
|
||||
if (mask != null) {
|
||||
JsonArray maskArray = new JsonArray();
|
||||
for (Locator locator: mask) {
|
||||
maskArray.add(((LocatorImpl) locator).toProtocol());
|
||||
}
|
||||
params.add("mask", maskArray);
|
||||
}
|
||||
JsonObject json = sendMessage("screenshot", params).getAsJsonObject();
|
||||
|
||||
byte[] buffer = Base64.getDecoder().decode(json.get("binary").getAsString());
|
||||
|
||||
@@ -37,7 +37,7 @@ public class PlaywrightImpl extends ChannelOwner implements Playwright {
|
||||
if (options != null && options.env != null) {
|
||||
env = options.env;
|
||||
}
|
||||
Path driver = Driver.ensureDriverInstalled(env);
|
||||
Path driver = Driver.ensureDriverInstalled(env, true);
|
||||
ProcessBuilder pb = new ProcessBuilder(driver.toString(), "run-driver");
|
||||
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
|
||||
pb.environment().putAll(env);
|
||||
|
||||
@@ -43,6 +43,8 @@ class SerializedValue{
|
||||
}
|
||||
O[] o;
|
||||
Number h;
|
||||
Integer id;
|
||||
Integer ref;
|
||||
}
|
||||
|
||||
class SerializedArgument{
|
||||
|
||||
@@ -18,7 +18,6 @@ package com.microsoft.playwright.impl;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.microsoft.playwright.PlaywrightException;
|
||||
import com.microsoft.playwright.Request;
|
||||
import com.microsoft.playwright.Route;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -29,12 +28,15 @@ import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class RouteImpl extends ChannelOwner implements Route {
|
||||
private boolean handled;
|
||||
|
||||
public RouteImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
|
||||
super(parent, type, guid, initializer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void abort(String errorCode) {
|
||||
startHandling();
|
||||
withLogging("Route.abort", () -> {
|
||||
JsonObject params = new JsonObject();
|
||||
params.addProperty("errorCode", errorCode);
|
||||
@@ -44,6 +46,7 @@ public class RouteImpl extends ChannelOwner implements Route {
|
||||
|
||||
@Override
|
||||
public void resume(ResumeOptions options) {
|
||||
startHandling();
|
||||
withLogging("Route.resume", () -> resumeImpl(options));
|
||||
}
|
||||
|
||||
@@ -78,6 +81,7 @@ public class RouteImpl extends ChannelOwner implements Route {
|
||||
|
||||
@Override
|
||||
public void fulfill(FulfillOptions options) {
|
||||
startHandling();
|
||||
withLogging("Route.fulfill", () -> fulfillImpl(options));
|
||||
}
|
||||
|
||||
@@ -86,16 +90,30 @@ public class RouteImpl extends ChannelOwner implements Route {
|
||||
options = new FulfillOptions();
|
||||
}
|
||||
|
||||
int status = options.status == null ? 200 : options.status;
|
||||
String body = "";
|
||||
Integer status = options.status;
|
||||
Map<String, String> headersOption = options.headers;
|
||||
String fetchResponseUid = null;
|
||||
|
||||
if (options.response != null) {
|
||||
if (status == null) {
|
||||
status = options.response.status();
|
||||
}
|
||||
if (headersOption == null) {
|
||||
headersOption = options.response.headers();
|
||||
}
|
||||
}
|
||||
if (status == null) {
|
||||
status = 200;
|
||||
}
|
||||
String body = null;
|
||||
boolean isBase64 = false;
|
||||
int length = 0;
|
||||
if (options.path != null) {
|
||||
try {
|
||||
byte[] buffer = Files.readAllBytes(options.path);
|
||||
body = Base64.getEncoder().encodeToString(buffer);
|
||||
isBase64 = true;
|
||||
length = buffer.length;
|
||||
byte[] buffer = Files.readAllBytes(options.path);
|
||||
body = Base64.getEncoder().encodeToString(buffer);
|
||||
isBase64 = true;
|
||||
length = buffer.length;
|
||||
} catch (IOException e) {
|
||||
throw new PlaywrightException("Failed to read from file: " + options.path, e);
|
||||
}
|
||||
@@ -107,11 +125,22 @@ public class RouteImpl extends ChannelOwner implements Route {
|
||||
body = Base64.getEncoder().encodeToString(options.bodyBytes);
|
||||
isBase64 = true;
|
||||
length = options.bodyBytes.length;
|
||||
} else if (options.response != null) {
|
||||
APIResponseImpl response = (APIResponseImpl) options.response;
|
||||
if (response.context.connection == connection) {
|
||||
fetchResponseUid = response.fetchUid();
|
||||
} else {
|
||||
byte[] bodyBytes = response.body();
|
||||
body = Base64.getEncoder().encodeToString(bodyBytes);
|
||||
isBase64 = true;
|
||||
length = bodyBytes.length;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Map<String, String> headers = new LinkedHashMap<>();
|
||||
if (options.headers != null) {
|
||||
for (Map.Entry<String, String> h : options.headers.entrySet()) {
|
||||
if (headersOption != null) {
|
||||
for (Map.Entry<String, String> h : headersOption.entrySet()) {
|
||||
headers.put(h.getKey().toLowerCase(), h.getValue());
|
||||
}
|
||||
}
|
||||
@@ -128,11 +157,21 @@ public class RouteImpl extends ChannelOwner implements Route {
|
||||
params.add("headers", Serialization.toProtocol(headers));
|
||||
params.addProperty("isBase64", isBase64);
|
||||
params.addProperty("body", body);
|
||||
if (fetchResponseUid != null) {
|
||||
params.addProperty("fetchResponseUid", fetchResponseUid);
|
||||
}
|
||||
sendMessageAsync("fulfill", params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Request request() {
|
||||
public RequestImpl request() {
|
||||
return connection.getExistingObject(initializer.getAsJsonObject("request").get("guid").getAsString());
|
||||
}
|
||||
|
||||
private void startHandling() {
|
||||
if (handled) {
|
||||
throw new PlaywrightException("Route is already handled!");
|
||||
}
|
||||
handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,10 @@ class Serialization {
|
||||
.registerTypeAdapter(Media.class, new ToLowerCaseSerializer<Media>())
|
||||
.registerTypeAdapter(ForcedColors.class, new ToLowerCaseSerializer<ForcedColors>())
|
||||
.registerTypeAdapter(ReducedMotion.class, new ToLowerCaseAndDashSerializer<ReducedMotion>())
|
||||
.registerTypeAdapter(ScreenshotAnimations.class, new ToLowerCaseSerializer<ScreenshotAnimations>())
|
||||
.registerTypeAdapter(ScreenshotType.class, new ToLowerCaseSerializer<ScreenshotType>())
|
||||
.registerTypeAdapter(ScreenshotScale.class, new ToLowerCaseSerializer<ScreenshotScale>())
|
||||
.registerTypeAdapter(ScreenshotCaret.class, new ToLowerCaseSerializer<ScreenshotCaret>())
|
||||
.registerTypeAdapter(MouseButton.class, new ToLowerCaseSerializer<MouseButton>())
|
||||
.registerTypeAdapter(LoadState.class, new ToLowerCaseSerializer<LoadState>())
|
||||
.registerTypeAdapter(WaitUntilState.class, new ToLowerCaseSerializer<WaitUntilState>())
|
||||
@@ -68,82 +71,128 @@ class Serialization {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static SerializedValue serializeValue(Object value, List<JSHandleImpl> handles, int depth) {
|
||||
if (depth > 100) {
|
||||
throw new PlaywrightException("Maximum argument depth exceeded");
|
||||
private static class ValueSerializer {
|
||||
// hashCode() of a map containing itself as a key will throw stackoverflow exception,
|
||||
// so we user wrappers.
|
||||
private static class HashableValue {
|
||||
final Object value;
|
||||
HashableValue(Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return value == ((HashableValue) o).value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return System.identityHashCode(value);
|
||||
}
|
||||
}
|
||||
SerializedValue result = new SerializedValue();
|
||||
if (value instanceof JSHandleImpl) {
|
||||
result.h = handles.size();
|
||||
handles.add((JSHandleImpl) value);
|
||||
private final Map<HashableValue, Integer> valueToId = new HashMap<>();
|
||||
private int lastId = 0;
|
||||
private final List<JSHandleImpl> handles = new ArrayList<>();
|
||||
private final SerializedValue serializedValue;
|
||||
|
||||
ValueSerializer(Object value) {
|
||||
serializedValue = serializeValue(value);
|
||||
}
|
||||
|
||||
SerializedArgument toSerializedArgument() {
|
||||
SerializedArgument result = new SerializedArgument();
|
||||
result.value = serializedValue;
|
||||
result.handles = new Channel[handles.size()];
|
||||
int i = 0;
|
||||
for (JSHandleImpl handle : handles) {
|
||||
result.handles[i] = new Channel();
|
||||
result.handles[i].guid = handle.guid;
|
||||
++i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
if (value == null) {
|
||||
result.v = "undefined";
|
||||
} else if (value instanceof Double) {
|
||||
double d = ((Double) value);
|
||||
if (d == Double.POSITIVE_INFINITY) {
|
||||
result.v = "Infinity";
|
||||
} else if (d == Double.NEGATIVE_INFINITY) {
|
||||
result.v = "-Infinity";
|
||||
} else if (d == -0) {
|
||||
result.v = "-0";
|
||||
} else if (Double.isNaN(d)) {
|
||||
result.v = "NaN";
|
||||
|
||||
private SerializedValue serializeValue(Object value) {
|
||||
SerializedValue result = new SerializedValue();
|
||||
if (value instanceof JSHandleImpl) {
|
||||
result.h = handles.size();
|
||||
handles.add((JSHandleImpl) value);
|
||||
return result;
|
||||
}
|
||||
if (value == null) {
|
||||
result.v = "undefined";
|
||||
} else if (value instanceof Double) {
|
||||
double d = ((Double) value);
|
||||
if (d == Double.POSITIVE_INFINITY) {
|
||||
result.v = "Infinity";
|
||||
} else if (d == Double.NEGATIVE_INFINITY) {
|
||||
result.v = "-Infinity";
|
||||
} else if (d == -0) {
|
||||
result.v = "-0";
|
||||
} else if (Double.isNaN(d)) {
|
||||
result.v = "NaN";
|
||||
} else {
|
||||
result.n = d;
|
||||
}
|
||||
} else if (value instanceof Boolean) {
|
||||
result.b = (Boolean) value;
|
||||
} else if (value instanceof Integer) {
|
||||
result.n = (Integer) value;
|
||||
} else if (value instanceof String) {
|
||||
result.s = (String) value;
|
||||
} else {
|
||||
result.n = d;
|
||||
HashableValue mapKey = new HashableValue(value);
|
||||
Integer id = valueToId.get(mapKey);
|
||||
if (id != null) {
|
||||
result.ref = id;
|
||||
} else {
|
||||
result.id = ++lastId;
|
||||
valueToId.put(mapKey, lastId);
|
||||
if (value instanceof List) {
|
||||
List<SerializedValue> list = new ArrayList<>();
|
||||
for (Object o : (List<?>) value) {
|
||||
list.add(serializeValue(o));
|
||||
}
|
||||
result.a = list.toArray(new SerializedValue[0]);
|
||||
} else if (value instanceof Map) {
|
||||
List<SerializedValue.O> list = new ArrayList<>();
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, ?> map = (Map<String, ?>) value;
|
||||
for (Map.Entry<String, ?> e : map.entrySet()) {
|
||||
SerializedValue.O o = new SerializedValue.O();
|
||||
o.k = e.getKey();
|
||||
o.v = serializeValue(e.getValue());
|
||||
list.add(o);
|
||||
}
|
||||
result.o = list.toArray(new SerializedValue.O[0]);
|
||||
} else if (value instanceof Object[]) {
|
||||
List<SerializedValue> list = new ArrayList<>();
|
||||
for (Object o : (Object[]) value) {
|
||||
list.add(serializeValue(o));
|
||||
}
|
||||
result.a = list.toArray(new SerializedValue[0]);
|
||||
} else {
|
||||
throw new PlaywrightException("Unsupported type of argument: " + value);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (value instanceof Boolean) {
|
||||
result.b = (Boolean) value;
|
||||
} else if (value instanceof Integer) {
|
||||
result.n = (Integer) value;
|
||||
} else if (value instanceof String) {
|
||||
result.s = (String) value;
|
||||
} else if (value instanceof List) {
|
||||
List<SerializedValue> list = new ArrayList<>();
|
||||
for (Object o : (List<?>) value) {
|
||||
list.add(serializeValue(o, handles, depth + 1));
|
||||
}
|
||||
result.a = list.toArray(new SerializedValue[0]);
|
||||
} else if (value instanceof Map) {
|
||||
List<SerializedValue.O> list = new ArrayList<>();
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, ?> map = (Map<String, ?>) value;
|
||||
for (Map.Entry<String, ?> e : map.entrySet()) {
|
||||
SerializedValue.O o = new SerializedValue.O();
|
||||
o.k = e.getKey();
|
||||
o.v = serializeValue(e.getValue(), handles, depth + 1);
|
||||
list.add(o);
|
||||
}
|
||||
result.o = list.toArray(new SerializedValue.O[0]);
|
||||
} else if (value instanceof Object[]) {
|
||||
List<SerializedValue> list = new ArrayList<>();
|
||||
for (Object o : (Object[]) value) {
|
||||
list.add(serializeValue(o, handles, depth + 1));
|
||||
}
|
||||
result.a = list.toArray(new SerializedValue[0]);
|
||||
} else {
|
||||
throw new PlaywrightException("Unsupported type of argument: " + value);
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static SerializedArgument serializeArgument(Object arg) {
|
||||
SerializedArgument result = new SerializedArgument();
|
||||
List<JSHandleImpl> handles = new ArrayList<>();
|
||||
result.value = serializeValue(arg, handles, 0);
|
||||
result.handles = new Channel[handles.size()];
|
||||
int i = 0;
|
||||
for (JSHandleImpl handle : handles) {
|
||||
result.handles[i] = new Channel();
|
||||
result.handles[i].guid = handle.guid;
|
||||
++i;
|
||||
}
|
||||
return result;
|
||||
return new ValueSerializer(arg).toSerializedArgument();
|
||||
}
|
||||
|
||||
static <T> T deserialize(SerializedValue value) {
|
||||
return deserialize(value, new HashMap<>());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static <T> T deserialize(SerializedValue value) {
|
||||
private static <T> T deserialize(SerializedValue value, Map<Integer, Object> idToValue) {
|
||||
if (value.ref != null) {
|
||||
return (T) idToValue.get(value.ref);
|
||||
}
|
||||
if (value.n != null) {
|
||||
if (value.n.doubleValue() == (double) value.n.intValue()) {
|
||||
return (T) Integer.valueOf(value.n.intValue());
|
||||
@@ -174,15 +223,17 @@ class Serialization {
|
||||
}
|
||||
if (value.a != null) {
|
||||
List<Object> list = new ArrayList<>();
|
||||
idToValue.put(value.id, list);
|
||||
for (SerializedValue v : value.a) {
|
||||
list.add(deserialize(v));
|
||||
list.add(deserialize(v, idToValue));
|
||||
}
|
||||
return (T) list;
|
||||
}
|
||||
if (value.o != null) {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
idToValue.put(value.id, map);
|
||||
for (SerializedValue.O o : value.o) {
|
||||
map.put(o.k, deserialize(o.v));
|
||||
map.put(o.k, deserialize(o.v, idToValue));
|
||||
}
|
||||
return (T) map;
|
||||
}
|
||||
@@ -209,6 +260,14 @@ class Serialization {
|
||||
}
|
||||
}
|
||||
|
||||
static JsonArray toJsonArray(Path[] files) {
|
||||
JsonArray jsonFiles = new JsonArray();
|
||||
for (Path p : files) {
|
||||
jsonFiles.add(p.toAbsolutePath().toString());
|
||||
}
|
||||
return jsonFiles;
|
||||
}
|
||||
|
||||
static JsonArray toJsonArray(FilePayload[] files) {
|
||||
JsonArray jsonFiles = new JsonArray();
|
||||
for (FilePayload p : files) {
|
||||
|
||||
@@ -23,15 +23,30 @@ import com.microsoft.playwright.PlaywrightException;
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
class StackTraceCollector {
|
||||
private final Path srcDir;
|
||||
private final List<Path> srcDirs;
|
||||
private final Map<Path, String> classToSourceCache = new HashMap<>();
|
||||
|
||||
StackTraceCollector(Path srcDir) {
|
||||
if (!Files.exists(srcDir.toAbsolutePath())) {
|
||||
throw new PlaywrightException("Source location doesn't exist: '" + srcDir.toAbsolutePath() + "'");
|
||||
static StackTraceCollector createFromEnv() {
|
||||
String srcRoots = System.getenv("PLAYWRIGHT_JAVA_SRC");
|
||||
if (srcRoots == null) {
|
||||
return null;
|
||||
}
|
||||
this.srcDir = srcDir;
|
||||
List<Path> srcDirs = Arrays.stream(srcRoots.split(File.pathSeparator)).map(p -> Paths.get(p)).collect(Collectors.toList());
|
||||
for (Path srcDir: srcDirs) {
|
||||
if (!Files.exists(srcDir.toAbsolutePath())) {
|
||||
throw new PlaywrightException("Source location specified in PLAYWRIGHT_JAVA_SRC doesn't exist: '" + srcDir.toAbsolutePath() + "'");
|
||||
}
|
||||
}
|
||||
return new StackTraceCollector(srcDirs);
|
||||
}
|
||||
|
||||
private StackTraceCollector(List<Path> srcDirs) {
|
||||
this.srcDirs = srcDirs;
|
||||
}
|
||||
|
||||
private String sourceFile(StackTraceElement frame) {
|
||||
@@ -47,7 +62,26 @@ class StackTraceCollector {
|
||||
if (file == null) {
|
||||
return "";
|
||||
}
|
||||
return srcDir.resolve(pkg).resolve(file).toString();
|
||||
return resolveSourcePath(Paths.get(pkg).resolve(file));
|
||||
}
|
||||
|
||||
private String resolveSourcePath(Path relativePath) {
|
||||
String path = classToSourceCache.get(relativePath);
|
||||
if (path == null) {
|
||||
for (Path dir : srcDirs) {
|
||||
Path absolutePath = dir.resolve(relativePath);
|
||||
if (Files.exists(absolutePath)) {
|
||||
path = absolutePath.toString();
|
||||
classToSourceCache.put(relativePath, path);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (path == null) {
|
||||
path = "";
|
||||
classToSourceCache.put(relativePath, path);
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
JsonArray currentStackTrace() {
|
||||
|
||||
@@ -25,16 +25,15 @@ import java.nio.file.Path;
|
||||
|
||||
import static com.microsoft.playwright.impl.Serialization.gson;
|
||||
|
||||
class TracingImpl implements Tracing {
|
||||
private final BrowserContextImpl context;
|
||||
private boolean includeSources;
|
||||
class TracingImpl extends ChannelOwner implements Tracing {
|
||||
LocalUtils localUtils;
|
||||
boolean isRemote;
|
||||
|
||||
TracingImpl(BrowserContextImpl context) {
|
||||
this.context = context;
|
||||
TracingImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
|
||||
super(parent, type, guid, initializer);
|
||||
}
|
||||
|
||||
private void stopChunkImpl(Path path) {
|
||||
boolean isRemote = context.browser() != null && context.browser().isRemote;
|
||||
JsonObject params = new JsonObject();
|
||||
String mode = "doNotSave";
|
||||
if (path != null) {
|
||||
@@ -45,11 +44,11 @@ class TracingImpl implements Tracing {
|
||||
}
|
||||
}
|
||||
params.addProperty("mode", mode);
|
||||
JsonObject json = context.sendMessage("tracingStopChunk", params).getAsJsonObject();
|
||||
JsonObject json = sendMessage("tracingStopChunk", params).getAsJsonObject();
|
||||
if (!json.has("artifact")) {
|
||||
return;
|
||||
}
|
||||
ArtifactImpl artifact = context.connection.getExistingObject(json.getAsJsonObject("artifact").get("guid").getAsString());
|
||||
ArtifactImpl artifact = connection.getExistingObject(json.getAsJsonObject("artifact").get("guid").getAsString());
|
||||
// In case of CDP connection browser is null but since the connection is established by
|
||||
// the driver it is safe to consider the artifact local.
|
||||
if (isRemote) {
|
||||
@@ -61,18 +60,18 @@ class TracingImpl implements Tracing {
|
||||
// Add local sources to the remote trace if necessary.
|
||||
if (isRemote && json.has("sourceEntries")) {
|
||||
JsonArray entries = json.getAsJsonArray("sourceEntries");
|
||||
context.localUtils.zip(path, entries);
|
||||
localUtils.zip(path, entries);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(StartOptions options) {
|
||||
context.withLogging("Tracing.start", () -> startImpl(options));
|
||||
withLogging("Tracing.start", () -> startImpl(options));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startChunk(StartChunkOptions options) {
|
||||
context.withLogging("Tracing.startChunk", () -> {
|
||||
withLogging("Tracing.startChunk", () -> {
|
||||
startChunkImpl(options);
|
||||
});
|
||||
}
|
||||
@@ -82,7 +81,7 @@ class TracingImpl implements Tracing {
|
||||
options = new StartChunkOptions();
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
context.sendMessage("tracingStartChunk", params);
|
||||
sendMessage("tracingStartChunk", params);
|
||||
}
|
||||
|
||||
private void startImpl(StartOptions options) {
|
||||
@@ -90,28 +89,28 @@ class TracingImpl implements Tracing {
|
||||
options = new StartOptions();
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
includeSources = options.sources != null;
|
||||
boolean includeSources = options.sources != null && options.sources;
|
||||
if (includeSources) {
|
||||
if (!context.connection.isCollectingStacks()) {
|
||||
throw new PlaywrightException("Source root directories must be provided to enable source collection");
|
||||
if (!connection.isCollectingStacks()) {
|
||||
throw new PlaywrightException("Source root directory must be specified via PLAYWRIGHT_JAVA_SRC environment variable when source collection is enabled");
|
||||
}
|
||||
params.addProperty("sources", true);
|
||||
}
|
||||
context.sendMessage("tracingStart", params);
|
||||
context.sendMessage("tracingStartChunk");
|
||||
sendMessage("tracingStart", params);
|
||||
sendMessage("tracingStartChunk");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop(StopOptions options) {
|
||||
context.withLogging("Tracing.stop", () -> {
|
||||
withLogging("Tracing.stop", () -> {
|
||||
stopChunkImpl(options == null ? null : options.path);
|
||||
context.sendMessage("tracingStop");
|
||||
sendMessage("tracingStop");
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopChunk(StopChunkOptions options) {
|
||||
context.withLogging("Tracing.stopChunk", () -> {
|
||||
withLogging("Tracing.stopChunk", () -> {
|
||||
stopChunkImpl(options == null ? null : options.path);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package com.microsoft.playwright.impl;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.microsoft.playwright.PlaywrightException;
|
||||
import com.microsoft.playwright.options.FilePayload;
|
||||
import com.microsoft.playwright.options.HttpHeader;
|
||||
@@ -23,6 +25,7 @@ import com.microsoft.playwright.options.HttpHeader;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.nio.file.Files;
|
||||
@@ -30,6 +33,8 @@ import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static com.microsoft.playwright.impl.Serialization.toJsonArray;
|
||||
|
||||
class Utils {
|
||||
static <F, T> T convertType(F f, Class<T> t) {
|
||||
if (f == null) {
|
||||
@@ -51,6 +56,10 @@ class Utils {
|
||||
try {
|
||||
T result = t.getDeclaredConstructor().newInstance();
|
||||
for (Field toField : t.getDeclaredFields()) {
|
||||
// Skip fields added by test coverage tools, see https://github.com/microsoft/playwright-java/issues/802
|
||||
if (toField.isSynthetic()) {
|
||||
continue;
|
||||
}
|
||||
if (Modifier.isStatic(toField.getModifiers())) {
|
||||
throw new RuntimeException("Unexpected field modifiers: " + t.getCanonicalName() + "." + toField.getName() + ", modifiers: " + toField.getModifiers());
|
||||
}
|
||||
@@ -140,6 +149,49 @@ class Utils {
|
||||
return mimeType;
|
||||
}
|
||||
|
||||
static final int maxUplodBufferSize = 50 * 1024 * 1024;
|
||||
|
||||
static boolean hasLargeFile(Path[] files) {
|
||||
for (Path file: files) {
|
||||
try {
|
||||
if (Files.size(file)> maxUplodBufferSize) {
|
||||
return true;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new PlaywrightException("Cannot get file size.", e);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void addLargeFileUploadParams(Path[] files, JsonObject params, BrowserContextImpl context) {
|
||||
if (context.browser().isRemote) {
|
||||
List<WritableStream> streams = new ArrayList<>();
|
||||
JsonArray jsonStreams = new JsonArray();
|
||||
for (Path path : files) {
|
||||
WritableStream temp = context.createTempFile(path.getFileName().toString());
|
||||
streams.add(temp);
|
||||
try (OutputStream out = temp.stream()) {
|
||||
Files.copy(path, out);
|
||||
} catch (IOException e) {
|
||||
throw new PlaywrightException("Failed to copy file to remote server.", e);
|
||||
}
|
||||
jsonStreams.add(temp.toProtocol());
|
||||
}
|
||||
params.add("streams", jsonStreams);
|
||||
} else {
|
||||
params.add("localPaths", toJsonArray(files));
|
||||
}
|
||||
}
|
||||
|
||||
static void checkFilePayloadSize(FilePayload[] files) {
|
||||
for (FilePayload file: files) {
|
||||
if (file.buffer.length > maxUplodBufferSize) {
|
||||
throw new PlaywrightException("Cannot set buffer larger than 50Mb, please write it to a file and pass its path instead.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static FilePayload[] toFilePayloads(Path[] files) {
|
||||
List<FilePayload> payloads = new ArrayList<>();
|
||||
for (Path file : files) {
|
||||
|
||||
@@ -22,7 +22,4 @@ interface Waitable<T> {
|
||||
boolean isDone();
|
||||
T get();
|
||||
void dispose();
|
||||
default <U> Waitable<U> apply(Function<T, U> transform) {
|
||||
return new WaitableAdapter<T, U>(this, transform);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.microsoft.playwright.impl;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
class WritableStream extends ChannelOwner {
|
||||
WritableStream(ChannelOwner parent, String type, String guid, JsonObject initializer) {
|
||||
super(parent, type, guid, initializer);
|
||||
}
|
||||
|
||||
OutputStream stream() {
|
||||
return new OutputStream() {
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
write(new byte[] { (byte) b });
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
JsonObject params = new JsonObject();
|
||||
ByteBuffer buffer = ByteBuffer.wrap(b, off, len);
|
||||
ByteBuffer encoded = Base64.getEncoder().encode(buffer);
|
||||
params.addProperty("binary", new String(encoded.array(), StandardCharsets.UTF_8));
|
||||
sendMessage("write", params);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
JsonObject toProtocol() {
|
||||
JsonObject json = new JsonObject();
|
||||
json.addProperty("guid", guid);
|
||||
return json;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.options;
|
||||
|
||||
public enum ScreenshotAnimations {
|
||||
DISABLED,
|
||||
ALLOW
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.options;
|
||||
|
||||
public enum ScreenshotCaret {
|
||||
HIDE,
|
||||
INITIAL
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.options;
|
||||
|
||||
public enum ScreenshotScale {
|
||||
CSS,
|
||||
DEVICE
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class MultipartFormData {
|
||||
static MultipartFormData parseRequest(HttpExchange exchange) throws IOException {
|
||||
ByteArrayOutputStream bodyBytes = new ByteArrayOutputStream();
|
||||
try (OutputStream output = bodyBytes) {
|
||||
Utils.copy(exchange.getRequestBody(), output);
|
||||
}
|
||||
String body = new String(bodyBytes.toByteArray(), StandardCharsets.UTF_8);
|
||||
String contentType = exchange.getRequestHeaders().get("content-type").get(0);
|
||||
Matcher matcher = Pattern.compile("boundary=(.*)$").matcher(contentType);
|
||||
if (!matcher.find()) {
|
||||
throw new RuntimeException("Boundary not found!");
|
||||
}
|
||||
String boundary = matcher.group(1);
|
||||
return new MultipartFormData(body, boundary);
|
||||
}
|
||||
|
||||
static class Field {
|
||||
final String filename;
|
||||
final String content;
|
||||
|
||||
Field(String filename, String content) {
|
||||
this.filename = filename;
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
|
||||
final List<Field> fields = new ArrayList<>();
|
||||
|
||||
MultipartFormData(String body, String boundary) {
|
||||
String[] parts = Pattern.compile("--" + boundary + "(--)?\r\n", Pattern.MULTILINE).split(body);
|
||||
for (String part : parts) {
|
||||
if (part.trim().length() == 0) {
|
||||
continue;
|
||||
}
|
||||
String[] headersAndContent = Pattern.compile("\r\n\r\n", Pattern.MULTILINE).split(part);
|
||||
if (headersAndContent.length != 2) {
|
||||
throw new RuntimeException("Unexpected format: " + part);
|
||||
}
|
||||
String headers = headersAndContent[0];
|
||||
String filename = null;
|
||||
for (String header: Pattern.compile("\r\n", Pattern.MULTILINE).split(headers)) {
|
||||
Matcher matcher = Pattern.compile("content-disposition: .*filename=\"([^\"]+)\"", Pattern.CASE_INSENSITIVE).matcher(header);
|
||||
if (!matcher.find()) {
|
||||
continue;
|
||||
}
|
||||
filename = matcher.group(1);
|
||||
}
|
||||
String content = headersAndContent[1];
|
||||
content = content.substring(0, content.length() - "\r\n".length());
|
||||
fields.add(new Field(filename, content));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,7 @@ public class TestBrowserContextAddCookies extends TestBase {
|
||||
.setDomain(cookies.get(0).domain)
|
||||
.setPath(cookies.get(0).path)
|
||||
.setExpires(cookies.get(0).expires)
|
||||
.setSameSite(cookies.get(0).sameSite)
|
||||
));
|
||||
assertJsonEquals(new Gson().toJson(cookies), context.cookies());
|
||||
}
|
||||
|
||||
+33
-9
@@ -16,6 +16,7 @@
|
||||
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import com.sun.net.httpserver.Filter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -33,10 +34,19 @@ public class TestBrowserContextNetworkEvents extends TestBase {
|
||||
page.setContent("<a target=_blank rel=noopener href='/one-style.html'>yo</a>");
|
||||
Page page1 = context.waitForPage(() -> page.click("a"));
|
||||
page1.waitForLoadState();
|
||||
assertEquals(asList(
|
||||
server.EMPTY_PAGE,
|
||||
server.PREFIX + "/one-style.html",
|
||||
server.PREFIX + "/one-style.css"), requests);
|
||||
// In firefox one-style.css is requested multiple times.
|
||||
if (isFirefox()) {
|
||||
assertEquals(asList(
|
||||
server.EMPTY_PAGE,
|
||||
server.PREFIX + "/one-style.html",
|
||||
server.PREFIX + "/one-style.css",
|
||||
server.PREFIX + "/one-style.css"), requests);
|
||||
} else {
|
||||
assertEquals(asList(
|
||||
server.EMPTY_PAGE,
|
||||
server.PREFIX + "/one-style.html",
|
||||
server.PREFIX + "/one-style.css"), requests);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -47,10 +57,19 @@ public class TestBrowserContextNetworkEvents extends TestBase {
|
||||
page.setContent("<a target=_blank rel=noopener href='/one-style.html'>yo</a>");
|
||||
Page page1 = context.waitForPage(() -> page.click("a"));
|
||||
page1.waitForLoadState();
|
||||
assertEquals(asList(
|
||||
server.EMPTY_PAGE,
|
||||
server.PREFIX + "/one-style.html",
|
||||
server.PREFIX + "/one-style.css"), responses);
|
||||
// In firefox one-style.css is requested multiple times.
|
||||
if (isFirefox()) {
|
||||
assertEquals(asList(
|
||||
server.EMPTY_PAGE,
|
||||
server.PREFIX + "/one-style.html",
|
||||
server.PREFIX + "/one-style.css",
|
||||
server.PREFIX + "/one-style.css"), responses);
|
||||
} else {
|
||||
assertEquals(asList(
|
||||
server.EMPTY_PAGE,
|
||||
server.PREFIX + "/one-style.html",
|
||||
server.PREFIX + "/one-style.css"), responses);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -59,7 +78,12 @@ public class TestBrowserContextNetworkEvents extends TestBase {
|
||||
List<Request> failedRequests = new ArrayList<>();
|
||||
context.onRequestFailed(request -> failedRequests.add(request));
|
||||
page.navigate(server.PREFIX + "/one-style.html");
|
||||
assertEquals(1, failedRequests.size());
|
||||
// In firefox one-style.css is requested multiple times.
|
||||
if (isFirefox()) {
|
||||
assertTrue(failedRequests.size() > 0);
|
||||
} else {
|
||||
assertEquals(1, failedRequests.size());
|
||||
}
|
||||
assertTrue(failedRequests.get(0).url().contains("one-style.css"));
|
||||
assertNull(failedRequests.get(0).response());
|
||||
assertEquals("stylesheet", failedRequests.get(0).resourceType());
|
||||
|
||||
@@ -16,10 +16,13 @@
|
||||
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
@@ -134,4 +137,74 @@ public class TestBrowserContextRoute extends TestBase {
|
||||
page.navigate(server.EMPTY_PAGE);
|
||||
assertEquals(2, intercepted[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldOverwritePostBodyWithEmptyString() throws ExecutionException, InterruptedException {
|
||||
boolean[] routeHandled = {false};
|
||||
context.route("**/empty.html", route -> {
|
||||
route.resume(new Route.ResumeOptions().setPostData(""));
|
||||
routeHandled[0] = true;
|
||||
});
|
||||
|
||||
Future<Server.Request> req = server.futureRequest("/empty.html");
|
||||
page.setContent("<script>\n" +
|
||||
" (async () => {\n" +
|
||||
" await fetch('" + server.EMPTY_PAGE + "', {\n" +
|
||||
" method: 'POST',\n" +
|
||||
" body: 'original',\n" +
|
||||
" });\n" +
|
||||
" })()\n" +
|
||||
" </script>");
|
||||
while (!routeHandled[0]) {
|
||||
page.waitForTimeout(100);
|
||||
}
|
||||
byte[] body = req.get().postBody;
|
||||
assertEquals(0, body.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotSwallowExceptionsInRoute() throws ExecutionException, InterruptedException {
|
||||
context.route("**/empty.html", route -> {
|
||||
throw new RuntimeException("My Exception");
|
||||
});
|
||||
|
||||
try {
|
||||
page.navigate(server.EMPTY_PAGE);
|
||||
fail("did not throw");
|
||||
} catch (RuntimeException e) {
|
||||
assertTrue(e.getMessage().contains("My Exception"), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("Conflicts with https://github.com/microsoft/playwright-java/pull/680")
|
||||
void shouldNotSwallowExceptionsInFulfill() throws ExecutionException, InterruptedException {
|
||||
APIRequestContext request = playwright.request().newContext();
|
||||
APIResponse response = request.get(server.EMPTY_PAGE);
|
||||
response.dispose();
|
||||
page.route("**/*", route -> {
|
||||
// Fulfilling with dsiposed response will lead to a server-side exception.
|
||||
route.fulfill(new Route.FulfillOptions().setResponse(response));
|
||||
});
|
||||
try {
|
||||
page.navigate(server.EMPTY_PAGE);
|
||||
fail("did not throw");
|
||||
} catch (RuntimeException e) {
|
||||
assertTrue(e.getMessage().contains("Fetch response has been disposed"), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("Conflicts with https://github.com/microsoft/playwright-java/pull/680")
|
||||
void shouldNotSwallowExceptionsInResume() throws ExecutionException, InterruptedException {
|
||||
page.route("**/*", route -> {
|
||||
route.resume(new Route.ResumeOptions().setUrl("file:///tmp"));
|
||||
});
|
||||
try {
|
||||
page.navigate(server.EMPTY_PAGE);
|
||||
fail("did not throw");
|
||||
} catch (RuntimeException e) {
|
||||
assertTrue(e.getMessage().contains("New URL must have same protocol as overridden URL"), e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,10 +30,11 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.microsoft.playwright.Utils.mapOf;
|
||||
import static com.microsoft.playwright.Utils.parseTrace;
|
||||
import static com.microsoft.playwright.Utils.*;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
@@ -60,13 +61,13 @@ public class TestBrowserTypeConnect extends TestBase {
|
||||
|
||||
private static BrowserServer launchBrowserServer(BrowserType browserType) {
|
||||
try {
|
||||
Path driver = Driver.ensureDriverInstalled(Collections.emptyMap());
|
||||
Path driver = Driver.ensureDriverInstalled(Collections.emptyMap(), false);
|
||||
Path dir = driver.getParent();
|
||||
String node = dir.resolve(isWindows ? "node.exe" : "node").toString();
|
||||
String cliJs = dir.resolve("package/lib/cli/cli.js").toString();
|
||||
// We launch node process directly instead of using playwright.sh script as killing the script
|
||||
// process will leave node process running and killing it would be more hassle.
|
||||
ProcessBuilder pb = new ProcessBuilder(node, cliJs, "launch-server", browserType.name());
|
||||
ProcessBuilder pb = new ProcessBuilder(node, cliJs, "launch-server", "--browser", browserType.name());
|
||||
pb.directory(dir.toFile());
|
||||
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
|
||||
BrowserServer result = new BrowserServer();
|
||||
@@ -166,6 +167,7 @@ public class TestBrowserTypeConnect extends TestBase {
|
||||
}
|
||||
assertNotNull(webSocketServer.lastClientHandshake);
|
||||
assertEquals("Playwright", webSocketServer.lastClientHandshake.getFieldValue("User-Agent"));
|
||||
assertEquals(browserType.name(), webSocketServer.lastClientHandshake.getFieldValue("x-playwright-browser"));
|
||||
assertEquals("bar", webSocketServer.lastClientHandshake.getFieldValue("foo"));
|
||||
}
|
||||
}
|
||||
@@ -507,4 +509,55 @@ public class TestBrowserTypeConnect extends TestBase {
|
||||
assertEquals(new String(thisFile, UTF_8), new String(sources.values().iterator().next(), UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFulfillWithGlobalFetchResult() {
|
||||
page.route("**/*", route -> {
|
||||
APIRequestContext request = playwright.request().newContext();
|
||||
APIResponse response = request.get(server.PREFIX + "/simple.json");
|
||||
route.fulfill(new Route.FulfillOptions().setResponse(response));
|
||||
});
|
||||
Response response = page.navigate(server.EMPTY_PAGE);
|
||||
assertEquals(200, response.status());
|
||||
assertEquals("{\"foo\": \"bar\"}\n", response.text());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldUploadLargeFile(@TempDir Path tmpDir) throws IOException, ExecutionException, InterruptedException {
|
||||
Assumptions.assumeTrue(3 <= (Runtime.getRuntime().maxMemory() >> 30), "Fails if max heap size is < 3Gb");
|
||||
page.navigate(server.PREFIX + "/input/fileupload.html");
|
||||
Path uploadFile = tmpDir.resolve("200MB.zip");
|
||||
String str = String.join("", Collections.nCopies(4 * 1024, "A"));
|
||||
|
||||
try (Writer stream = new OutputStreamWriter(Files.newOutputStream(uploadFile))) {
|
||||
for (int i = 0; i < 50 * 1024; i++) {
|
||||
stream.write(str);
|
||||
}
|
||||
}
|
||||
Locator input = page.locator("input[type='file']");
|
||||
JSHandle events = input.evaluateHandle("e => {\n" +
|
||||
" const events = [];\n" +
|
||||
" e.addEventListener('input', () => events.push('input'));\n" +
|
||||
" e.addEventListener('change', () => events.push('change'));\n" +
|
||||
" return events;\n" +
|
||||
" }");
|
||||
input.setInputFiles(uploadFile);
|
||||
assertEquals("200MB.zip", input.evaluate("e => e.files[0].name"));
|
||||
assertEquals(asList("input", "change"), events.evaluate("e => e"));
|
||||
CompletableFuture<MultipartFormData> formData = new CompletableFuture<>();
|
||||
server.setRoute("/upload", exchange -> {
|
||||
try {
|
||||
MultipartFormData multipartFormData = MultipartFormData.parseRequest(exchange);
|
||||
formData.complete(multipartFormData);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
formData.completeExceptionally(e);
|
||||
}
|
||||
exchange.sendResponseHeaders(200, -1);
|
||||
});
|
||||
page.click("input[type=submit]", new Page.ClickOptions().setTimeout(90_000));
|
||||
List<MultipartFormData.Field> fields = formData.get().fields;
|
||||
assertEquals(1, fields.size());
|
||||
assertEquals("200MB.zip", fields.get(0).filename);
|
||||
assertEquals(200 * 1024 * 1024, fields.get(0).content.length());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,6 @@ public class TestElementHandlePress extends TestBase {
|
||||
void shouldWorkWithNumberInput() {
|
||||
page.setContent("<input type='number' value=2 />");
|
||||
page.press("input", "1");
|
||||
assertEquals("12", page.evalOnSelector("input", "input => input.value"));
|
||||
assertEquals(isWebKit() ? "1" : "12", page.evalOnSelector("input", "input => input.value"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,6 @@ public class TestElementHandleType extends TestBase {
|
||||
void shouldWorkWithNumberInput() {
|
||||
page.setContent("<input type='number' value=2 />");
|
||||
page.type("input", "13");
|
||||
assertEquals("132", page.evalOnSelector("input", "input => input.value"));
|
||||
assertEquals(isWebKit() ? "13" : "132", page.evalOnSelector("input", "input => input.value"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,6 +118,13 @@ public class TestLocatorAssertions extends TestBase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasTextWTextInnerTextPass() {
|
||||
page.setContent("<div id=node>Text <span hidden>garbage</span> content</div>");
|
||||
Locator locator = page.locator("#node");
|
||||
assertThat(locator).hasText("Text content", new LocatorAssertions.HasTextOptions().setUseInnerText(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasTextWTextArrayPass() {
|
||||
page.setContent("<div>Text \n1</div><div>Text 2a</div>");
|
||||
@@ -190,7 +197,8 @@ public class TestLocatorAssertions extends TestBase {
|
||||
} catch (AssertionFailedError e) {
|
||||
assertEquals("[Text 1, Text 3, Extra]", e.getExpected().getStringRepresentation());
|
||||
assertEquals("[Text 1, Text 3]", e.getActual().getStringRepresentation());
|
||||
assertTrue(e.getMessage().contains("Locator expected to have text"), e.getMessage());
|
||||
assertTrue(e.getMessage().contains("Locator expected to have text: [Text 1, Text 3, Extra]"), e.getMessage());
|
||||
assertTrue(e.getMessage().contains("Received: [Text 1, Text 3]"), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,7 +242,7 @@ public class TestLocatorAssertions extends TestBase {
|
||||
} catch (AssertionFailedError e) {
|
||||
assertEquals("foo", e.getExpected().getStringRepresentation());
|
||||
assertEquals("node", e.getActual().getStringRepresentation());
|
||||
assertTrue(e.getMessage().contains("Locator expected to have attribute 'id'"), e.getMessage());
|
||||
assertTrue(e.getMessage().contains("Locator expected to have attribute 'id': foo\nReceived: node"), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -255,7 +263,7 @@ public class TestLocatorAssertions extends TestBase {
|
||||
} catch (AssertionFailedError e) {
|
||||
assertEquals(".Nod..", e.getExpected().getStringRepresentation());
|
||||
assertEquals("node", e.getActual().getStringRepresentation());
|
||||
assertTrue(e.getMessage().contains("Locator expected to have attribute 'id' matching regex"), e.getMessage());
|
||||
assertTrue(e.getMessage().contains("Locator expected to have attribute 'id' matching regex: .Nod..\nReceived: node"), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -576,6 +584,13 @@ public class TestLocatorAssertions extends TestBase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void isCheckedFalsePass() {
|
||||
page.setContent("<input type=checkbox></input>");
|
||||
Locator locator = page.locator("input");
|
||||
assertThat(locator).isChecked(new LocatorAssertions.IsCheckedOptions().setChecked(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isDisabledPass() {
|
||||
page.setContent("<button disabled>Text</button>");
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.microsoft.playwright.options.BoundingBox;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestLocatorHighlight extends TestBase {
|
||||
@Disabled("Requires isUnderTest to be true https://github.com/microsoft/playwright/pull/12420")
|
||||
@Test
|
||||
void shouldHighlightLocator() {
|
||||
page.setContent("<input type='text' />");
|
||||
page.locator("input").highlight();
|
||||
assertThat(page.locator("x-pw-tooltip")).hasText("input");
|
||||
assertThat(page.locator("x-pw-highlight")).isVisible();
|
||||
BoundingBox box1 = page.locator("input").boundingBox();
|
||||
BoundingBox box2 = page.locator("x-pw-highlight").boundingBox();
|
||||
assertEquals(new Gson().toJson(box2), new Gson().toJson(box1));
|
||||
}
|
||||
}
|
||||
@@ -91,6 +91,12 @@ public class TestPageAssertions extends TestBase {
|
||||
assertThat(page).hasTitle("Woof-Woof", new PageAssertions.HasTitleOptions().setTimeout(1_000));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasTitleTextNormalizeWhitespaces() {
|
||||
page.setContent("<title> Foo Bar </title>");
|
||||
assertThat(page).hasTitle(" Foo Bar", new PageAssertions.HasTitleOptions().setTimeout(1_000));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasTitleTextFail() {
|
||||
page.navigate(server.PREFIX + "/title.html");
|
||||
@@ -100,7 +106,7 @@ public class TestPageAssertions extends TestBase {
|
||||
} catch (AssertionFailedError e) {
|
||||
assertEquals("foo", e.getExpected().getValue());
|
||||
assertEquals("Woof-Woof", e.getActual().getValue());
|
||||
assertTrue(e.getMessage().contains("Page title expected to be"), e.getMessage());
|
||||
assertTrue(e.getMessage().contains("Page title expected to be: foo\nReceived: Woof-Woof"), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +125,7 @@ public class TestPageAssertions extends TestBase {
|
||||
} catch (AssertionFailedError e) {
|
||||
assertEquals("^foo[AB]", e.getExpected().getStringRepresentation());
|
||||
assertEquals("Woof-Woof", e.getActual().getValue());
|
||||
assertTrue(e.getMessage().contains("Page title expected to match regex"), e.getMessage());
|
||||
assertTrue(e.getMessage().contains("Page title expected to match regex: ^foo[AB]\nReceived: Woof-Woof"), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -345,31 +345,13 @@ public class TestPageEvaluate extends TestBase {
|
||||
|
||||
@Test
|
||||
void shouldReturnUndefinedForNonSerializableObjects() {
|
||||
assertEquals(null, page.evaluate("() => window"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFailForCircularObject() {
|
||||
Object result = page.evaluate("() => {\n" +
|
||||
" const a = {};\n" +
|
||||
" const b = { a };\n" +
|
||||
" a.b = b;\n" +
|
||||
" return a;\n" +
|
||||
"}");
|
||||
assertNull(result);
|
||||
assertEquals(null, page.evaluate("() => () => {}"));
|
||||
assertEquals("ref: <Window>", page.evaluate("() => window"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldBeAbleToThrowATrickyError() {
|
||||
JSHandle windowHandle = page.evaluateHandle("() => window");
|
||||
String errorText = null;
|
||||
try {
|
||||
windowHandle.jsonValue();
|
||||
fail("did not throw");
|
||||
} catch (PlaywrightException e) {
|
||||
errorText = e.getMessage();
|
||||
}
|
||||
assertNotNull(errorText);
|
||||
String errorText = "My error";
|
||||
try {
|
||||
page.evaluate("errorText => {\n" +
|
||||
" throw new Error(errorText);\n" +
|
||||
@@ -619,4 +601,23 @@ public class TestPageEvaluate extends TestBase {
|
||||
JSHandle resultHandle = page.evaluateHandle("() => ({ toJSON: () => 'string', data: 'data' })");
|
||||
assertEquals(mapOf("data", "data", "toJSON", emptyMap()), resultHandle.jsonValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAliasWindowDocumentAndNode() {
|
||||
Object object = page.evaluate("[window, document, document.body]");
|
||||
assertEquals(asList("ref: <Window>", "ref: <Document>", "ref: <Node>"), object);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldWorkForCircularObject() {
|
||||
Object result = page.evaluate("() => {\n" +
|
||||
" const a = {};\n" +
|
||||
" a.b = a;\n" +
|
||||
" return a;\n" +
|
||||
" }");
|
||||
|
||||
Map<String, Object> map = (Map<String, Object>) result;
|
||||
assertEquals(1, map.size());
|
||||
assertTrue(map == map.get("b"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,12 @@ public class TestPageEventNetwork extends TestBase {
|
||||
List<Request> failedRequests = new ArrayList<>();
|
||||
page.onRequestFailed(request -> failedRequests.add(request));
|
||||
page.navigate(server.PREFIX + "/one-style.html");
|
||||
assertEquals(1, failedRequests.size());
|
||||
// In firefox one-style.css is requested multiple times.
|
||||
if (isFirefox()) {
|
||||
assertTrue(failedRequests.size() > 0);
|
||||
} else {
|
||||
assertEquals(1, failedRequests.size());
|
||||
}
|
||||
assertTrue(failedRequests.get(0).url().contains("one-style.css"));
|
||||
assertNull(failedRequests.get(0).response());
|
||||
assertEquals("stylesheet", failedRequests.get(0).resourceType());
|
||||
|
||||
@@ -227,5 +227,15 @@ public class TestPageExposeFunction extends TestBase {
|
||||
assertTrue(e.getMessage().contains("exposeBindingHandle supports a single argument, 2 received"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSerializeCycles() {
|
||||
Object[] object = { null };
|
||||
page.exposeBinding("log", (source, obj) -> object[0] = obj[0]);
|
||||
page.evaluate("const a = {}; a.b = a; window.log(a)");
|
||||
Map<String, Object> map = (Map<String, Object>) object[0];
|
||||
assertEquals(1, map.size());
|
||||
assertTrue(map == map.get("b"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+13
-22
@@ -14,30 +14,21 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.microsoft.playwright.impl;
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.function.Function;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class WaitableAdapter<F, T> implements Waitable<T> {
|
||||
private final Waitable<F> waitable;
|
||||
private final Function<F, T> transformation;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
WaitableAdapter(Waitable<F> waitable, Function<F, T> transformation) {
|
||||
this.waitable = waitable;
|
||||
this.transformation = transformation;
|
||||
}
|
||||
@Override
|
||||
public boolean isDone() {
|
||||
return waitable.isDone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get() {
|
||||
return transformation.apply(waitable.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
waitable.dispose();
|
||||
public class TestPageLocatorConvenience extends TestBase {
|
||||
@Test
|
||||
void shouldReturnPage() {
|
||||
page.navigate(server.PREFIX + "/frames/two-frames.html");
|
||||
Locator outer = page.locator("#outer");
|
||||
assertEquals(page, outer.page());
|
||||
Locator inner = outer.locator("#inner");
|
||||
assertEquals(page, inner.page());
|
||||
Locator inFrame = page.frames().get(1).locator("div");
|
||||
assertEquals(page, inFrame.page());
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestPageLocatorQuery extends TestBase {
|
||||
@@ -118,4 +119,34 @@ public class TestPageLocatorQuery extends TestBase {
|
||||
assertEquals("Hello \"world\"", page.locator("div", new Page.LocatorOptions().setHasText(pattern)).textContent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSupportHasLocator() {
|
||||
page.setContent("<div><span>hello</span></div><div><span>world</span></div>");
|
||||
assertThat(page.locator("div", new Page.LocatorOptions().setHas(page.locator("text=world")))).hasCount(1);
|
||||
assertEquals("<div><span>world</span></div>", page.locator("div", new Page.LocatorOptions().setHas(page.locator("text=world"))).evaluate("e => e.outerHTML"));
|
||||
assertThat(page.locator("div", new Page.LocatorOptions().setHas(page.locator("text='hello'")))).hasCount(1);
|
||||
assertEquals("<div><span>hello</span></div>", page.locator("div", new Page.LocatorOptions().setHas(page.locator("text='hello'"))).evaluate("e => e.outerHTML"));
|
||||
assertThat(page.locator("div", new Page.LocatorOptions().setHas(page.locator("xpath=./span")))).hasCount(2);
|
||||
assertThat(page.locator("div", new Page.LocatorOptions().setHas(page.locator("span")))).hasCount(2);
|
||||
assertThat(page.locator("div", new Page.LocatorOptions().setHas(page.locator("span", new Page.LocatorOptions().setHasText("wor"))))).hasCount(1);
|
||||
assertEquals("<div><span>world</span></div>", page.locator("div", new Page.LocatorOptions().setHas(
|
||||
page.locator("span", new Page.LocatorOptions().setHasText("wor")))).evaluate("e => e.outerHTML"));
|
||||
assertThat(page.locator("div", new Page.LocatorOptions()
|
||||
.setHas(page.locator("span")).setHasText("wor"))).hasCount(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSupportLocatorFilter() {
|
||||
page.setContent("<section><div><span>hello</span></div><div><span>world</span></div></section>");
|
||||
assertThat(page.locator("div").filter(new Locator.FilterOptions().setHasText("hello"))).hasCount(1);
|
||||
assertThat(page.locator("div", new Page.LocatorOptions().setHasText("hello")).filter(new Locator.FilterOptions().setHasText("hello"))).hasCount(1);
|
||||
assertThat(page.locator("div", new Page.LocatorOptions().setHasText("hello")).filter(new Locator.FilterOptions().setHasText("world"))).hasCount(0);
|
||||
assertThat(page.locator("section", new Page.LocatorOptions().setHasText("hello")).filter(new Locator.FilterOptions().setHasText("world"))).hasCount(1);
|
||||
assertThat(page.locator("div").filter(new Locator.FilterOptions().setHasText("hello")).locator("span")).hasCount(1);
|
||||
assertThat(page.locator("div").filter(new Locator.FilterOptions().setHas(page.locator("span", new Page.LocatorOptions().setHasText("world"))))).hasCount(1);
|
||||
assertThat(page.locator("div").filter(new Locator.FilterOptions().setHas(page.locator("span")))).hasCount(2);
|
||||
assertThat(page.locator("div").filter(new Locator.FilterOptions()
|
||||
.setHas(page.locator("span"))
|
||||
.setHasText("world"))).hasCount(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import java.util.*;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestPageNetworkRequest extends TestBase {
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
public class TestPageRequestContinue extends TestBase {
|
||||
@Test
|
||||
void shouldNotThrowWhenContinuingAfterPageIsClosed() {
|
||||
boolean[] done = {false};
|
||||
page.route("**/*", route -> {
|
||||
page.close();
|
||||
route.resume();
|
||||
done[0] = true;
|
||||
});
|
||||
try {
|
||||
page.navigate(server.EMPTY_PAGE);
|
||||
fail("did not throw");
|
||||
} catch (PlaywrightException e) {
|
||||
assertTrue(e.getMessage().contains("Navigation failed because page was closed") ||
|
||||
e.getMessage().contains("frame was detached"), e.getMessage());
|
||||
}
|
||||
assertTrue(done[0]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static com.microsoft.playwright.Utils.mapOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestPageRequestFulfill extends TestBase {
|
||||
@Test
|
||||
void shouldFulfillWithGlobalFetchResult() {
|
||||
page.route("**/*", route -> {
|
||||
APIRequestContext request = playwright.request().newContext();
|
||||
APIResponse response = request.get(server.PREFIX + "/simple.json");
|
||||
route.fulfill(new Route.FulfillOptions().setResponse(response));
|
||||
});
|
||||
Response response = page.navigate(server.EMPTY_PAGE);
|
||||
assertEquals("application/json", response.headers().get("content-type"));
|
||||
assertEquals(200, response.status());
|
||||
assertEquals("{\"foo\": \"bar\"}\n", response.text());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFulfillWithFetchResult() {
|
||||
page.route("**/*", route -> {
|
||||
APIResponse response = page.request().get(server.PREFIX + "/simple.json");
|
||||
route.fulfill(new Route.FulfillOptions().setResponse(response));
|
||||
});
|
||||
Response response = page.navigate(server.EMPTY_PAGE);
|
||||
assertEquals("application/json", response.headers().get("content-type"));
|
||||
assertEquals(200, response.status());
|
||||
assertEquals("{\"foo\": \"bar\"}\n", response.text());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFulfillWithFetchResultAndOverrides() {
|
||||
page.route("**/*", route -> {
|
||||
APIResponse response = page.request().get(server.PREFIX + "/simple.json");
|
||||
route.fulfill(new Route.FulfillOptions().setResponse(response)
|
||||
.setStatus(201).setHeaders(mapOf("Content-Type", "application/json", "foo", "bar")));
|
||||
});
|
||||
Response response = page.navigate(server.EMPTY_PAGE);
|
||||
assertEquals("application/json", response.headers().get("content-type"));
|
||||
assertEquals(201, response.status());
|
||||
assertEquals("bar", response.allHeaders().get("foo"));
|
||||
assertEquals("{\"foo\": \"bar\"}\n", response.text());
|
||||
}
|
||||
}
|
||||
@@ -256,7 +256,7 @@ public class TestPageRoute extends TestBase {
|
||||
}
|
||||
assertNotNull(failedRequest[0]);
|
||||
if (isWebKit()) {
|
||||
assertEquals("Request intercepted", failedRequest[0].failure());
|
||||
assertEquals("Blocked by Web Inspector", failedRequest[0].failure());
|
||||
} else if (isFirefox()) {
|
||||
assertEquals("NS_ERROR_OFFLINE", failedRequest[0].failure());
|
||||
} else {
|
||||
@@ -281,7 +281,7 @@ public class TestPageRoute extends TestBase {
|
||||
fail("did not throw");
|
||||
} catch (PlaywrightException e) {
|
||||
if (isWebKit())
|
||||
assertTrue(e.getMessage().contains("Request intercepted"));
|
||||
assertTrue(e.getMessage().contains("Blocked by Web Inspector"), e.getMessage());
|
||||
else if (isFirefox())
|
||||
assertTrue(e.getMessage().contains("NS_ERROR_FAILURE"));
|
||||
else
|
||||
@@ -347,7 +347,12 @@ public class TestPageRoute extends TestBase {
|
||||
assertEquals(200, response.status());
|
||||
assertTrue(response.url().contains("one-style.html"));
|
||||
|
||||
assertEquals(2, intercepted.size());
|
||||
// In firefox one-style.css is requested multiple times.
|
||||
if (isFirefox()) {
|
||||
assertTrue(intercepted.size() > 1);
|
||||
} else {
|
||||
assertEquals(2, intercepted.size());
|
||||
}
|
||||
assertEquals("document", intercepted.get(0).resourceType());
|
||||
assertTrue(intercepted.get(0).url().contains("one-style.html"));
|
||||
|
||||
@@ -460,7 +465,12 @@ public class TestPageRoute extends TestBase {
|
||||
});
|
||||
Response response = page.navigate("data:text/html,<link rel='stylesheet' href='" + server.PREFIX + "/fonts?helvetica|arial'/>");
|
||||
assertNull(response);
|
||||
assertEquals(1, requests.size());
|
||||
// In firefox linked resource is requested multiple times.
|
||||
if (isFirefox()) {
|
||||
assertTrue(requests.size() > 0);
|
||||
} else {
|
||||
assertEquals(1, requests.size());
|
||||
}
|
||||
assertEquals(400, (requests.get(0).response()).status());
|
||||
}
|
||||
|
||||
@@ -524,9 +534,7 @@ public class TestPageRoute extends TestBase {
|
||||
page.navigate(server.EMPTY_PAGE);
|
||||
page.route("**/cars*", route -> {
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
if (route.request().url().endsWith("allow")) {
|
||||
headers.put("access-control-allow-origin", "*");
|
||||
}
|
||||
headers.put("access-control-allow-origin", route.request().url().endsWith("allow") ? "*" : "none");
|
||||
route.fulfill(new Route.FulfillOptions()
|
||||
.setStatus(200)
|
||||
.setContentType("application/json")
|
||||
@@ -690,4 +698,29 @@ public class TestPageRoute extends TestBase {
|
||||
page.navigate(server.EMPTY_PAGE);
|
||||
assertEquals(1, intercepted[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAddAccessControlAllowOriginByDefaultWhenFulfill() {
|
||||
page.navigate(server.EMPTY_PAGE);
|
||||
page.route("**/cars", route -> {
|
||||
route.fulfill(new Route.FulfillOptions()
|
||||
.setContentType("application/json")
|
||||
.setStatus(200)
|
||||
.setBody("[\"electric\",\"gas\"]"));
|
||||
});
|
||||
|
||||
Response response = page.waitForResponse("https://example.com/cars", () -> {
|
||||
Object result = page.evaluate("async () => {\n" +
|
||||
" const response = await fetch('https://example.com/cars', {\n" +
|
||||
" method: 'POST',\n" +
|
||||
" headers: { 'Content-Type': 'application/json' },\n" +
|
||||
" mode: 'cors',\n" +
|
||||
" body: JSON.stringify({ 'number': 1 })\n" +
|
||||
" });\n" +
|
||||
" return response.text();\n" +
|
||||
" }");
|
||||
assertEquals("[\"electric\",\"gas\"]", result);
|
||||
});
|
||||
assertEquals(server.PREFIX, response.headerValue("Access-Control-Allow-Origin"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,14 +17,23 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import com.microsoft.playwright.options.Clip;
|
||||
import com.microsoft.playwright.options.ScreenshotAnimations;
|
||||
import com.microsoft.playwright.options.ScreenshotCaret;
|
||||
import com.microsoft.playwright.options.ScreenshotScale;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.opentest4j.AssertionFailedError;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static com.microsoft.playwright.options.ScreenshotAnimations.DISABLED;
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
// TODO: suite.skip(browserName === "firefox" && headful");
|
||||
public class TestPageScreenshot extends TestBase {
|
||||
@@ -50,4 +59,191 @@ public class TestPageScreenshot extends TestBase {
|
||||
assertEquals(100, image.getHeight());
|
||||
// expect(screenshot).toMatchSnapshot("screenshot-clip-rect.png");
|
||||
}
|
||||
|
||||
static private void rafraf(Page page) {
|
||||
// Do a double raf since single raf does not
|
||||
// actually guarantee a new animation frame.
|
||||
page.evaluate("() => new Promise(x => {\n" +
|
||||
" requestAnimationFrame(() => requestAnimationFrame(x));\n" +
|
||||
" })");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotCaptureInfiniteCssAnimation() {
|
||||
page.navigate(server.PREFIX + "/rotate-z.html");
|
||||
Locator div = page.locator("div");
|
||||
byte[] screenshot = div.screenshot(new Locator.ScreenshotOptions().setAnimations(DISABLED));
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
rafraf(page);
|
||||
byte[] newScreenshot = div.screenshot(new Locator.ScreenshotOptions().setAnimations(DISABLED));
|
||||
assertArrayEquals(screenshot, newScreenshot);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotCapturePseudoElementCssAnimation() {
|
||||
page.navigate(server.PREFIX + "/rotate-pseudo.html");
|
||||
Locator div = page.locator("div");
|
||||
byte[] screenshot = div.screenshot(new Locator.ScreenshotOptions().setAnimations(DISABLED));
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
rafraf(page);
|
||||
byte[] newScreenshot = div.screenshot(new Locator.ScreenshotOptions().setAnimations(DISABLED));
|
||||
assertArrayEquals(screenshot, newScreenshot);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotCaptureCssAnimationsInShadowDOM() {
|
||||
page.navigate(server.PREFIX + "/rotate-z-shadow-dom.html");
|
||||
byte[] screenshot = page.screenshot(new Page.ScreenshotOptions().setAnimations(DISABLED));
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
rafraf(page);
|
||||
byte[] newScreenshot = page.screenshot(new Page.ScreenshotOptions().setAnimations(DISABLED));
|
||||
assertArrayEquals(screenshot, newScreenshot);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldResumeInfiniteAnimations() {
|
||||
page.navigate(server.PREFIX + "/rotate-z.html");
|
||||
page.screenshot(new Page.ScreenshotOptions().setAnimations(DISABLED));
|
||||
byte[] buffer1 = page.screenshot();
|
||||
rafraf(page);
|
||||
byte[] buffer2 = page.screenshot();
|
||||
try {
|
||||
assertArrayEquals(buffer1, buffer2);
|
||||
} catch (AssertionFailedError e) {
|
||||
return;
|
||||
}
|
||||
fail("Screenshots are equal");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotCaptureInfiniteWebAnimations() {
|
||||
page.navigate(server.PREFIX + "/web-animation.html");
|
||||
Locator div = page.locator("div");
|
||||
byte[] screenshot = div.screenshot(new Locator.ScreenshotOptions().setAnimations(DISABLED));
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
rafraf(page);
|
||||
byte[] newScreenshot = div.screenshot(new Locator.ScreenshotOptions().setAnimations(DISABLED));
|
||||
assertArrayEquals(screenshot, newScreenshot);
|
||||
}
|
||||
// Should resume infinite web animation.
|
||||
byte[] buffer1 = page.screenshot();
|
||||
rafraf(page);
|
||||
byte[] buffer2 = page.screenshot();
|
||||
try {
|
||||
assertArrayEquals(buffer1, buffer2);
|
||||
} catch (AssertionFailedError e) {
|
||||
return;
|
||||
}
|
||||
fail("Screenshots are equal");
|
||||
}
|
||||
|
||||
@Test
|
||||
void maskShouldWork() {
|
||||
page.setViewportSize(500, 500);
|
||||
page.navigate(server.PREFIX + "/grid.html");
|
||||
byte[] screenshot = page.screenshot(new Page.ScreenshotOptions()
|
||||
.setMask(asList(page.locator("div").nth(5))));
|
||||
// TODO: toMatchSnapshot is not present in java, so we only checks that masked screenshot is different.
|
||||
byte[] originalScreenshot = page.screenshot();
|
||||
try {
|
||||
assertArrayEquals(screenshot, originalScreenshot);
|
||||
} catch (AssertionFailedError e) {
|
||||
return;
|
||||
}
|
||||
fail("Screenshots are equal");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldWorkWithDeviceScaleFactorAndClip() {
|
||||
try (BrowserContext context = browser.newContext(new Browser.NewContextOptions()
|
||||
.setViewportSize(500, 500).setDeviceScaleFactor(3))) {
|
||||
Page page = context.newPage();
|
||||
page.navigate(server.PREFIX + "/grid.html");
|
||||
byte[] screenshot = page.screenshot(new Page.ScreenshotOptions().setClip(50, 100, 150, 100));
|
||||
assertNotNull(screenshot);
|
||||
// TODO:
|
||||
// expect(screenshot).toMatchSnapshot("screenshot-device-scale-factor-clip.png");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldWorkWithDeviceScaleFactorAndScaleCss() {
|
||||
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
|
||||
.setViewportSize(320, 480).setDeviceScaleFactor(2));
|
||||
Page page = context.newPage();
|
||||
page.navigate(server.PREFIX + "/grid.html");
|
||||
byte[] screenshot = page.screenshot(new Page.ScreenshotOptions().setScale(ScreenshotScale.CSS));
|
||||
assertNotNull(screenshot);
|
||||
// TODO:
|
||||
// expect(screenshot).toMatchSnapshot("screenshot-device-scale-factor-css-size.png");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldWorkWithDeviceScaleFactorAndScaleDevice() {
|
||||
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
|
||||
.setViewportSize(320, 480).setDeviceScaleFactor(2));
|
||||
Page page = context.newPage();
|
||||
page.navigate(server.PREFIX + "/grid.html");
|
||||
byte[] screenshot = page.screenshot(new Page.ScreenshotOptions().setScale(ScreenshotScale.DEVICE));
|
||||
assertNotNull(screenshot);
|
||||
// TODO:
|
||||
// expect(screenshot).toMatchSnapshot("screenshot-device-scale-factor-device-size.png");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotCaptureBlinkingCaretByDefault() {
|
||||
page.setContent("<!-- Refer to stylesheet from other origin. Accessing this\n" +
|
||||
" stylesheet rules will throw.\n" +
|
||||
" -->\n" +
|
||||
" <link rel=stylesheet href=\"" + server.CROSS_PROCESS_PREFIX + "/injectedstyle.css\">\n" +
|
||||
" <!-- make life harder: define caret color in stylesheet -->\n" +
|
||||
" <style>\n" +
|
||||
" div {\n" +
|
||||
" caret-color: #000 !important;\n" +
|
||||
" }\n" +
|
||||
" </style>\n" +
|
||||
" <div contenteditable=\"true\"></div>\n");
|
||||
Locator div = page.locator("div");
|
||||
div.type("foo bar");
|
||||
byte[] screenshot = div.screenshot();
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
// Caret blinking time is set to 500ms.
|
||||
// Try to capture variety of screenshots to make
|
||||
// sure we don"t capture blinking caret.
|
||||
page.waitForTimeout(150);
|
||||
byte[] newScreenshot = div.screenshot();
|
||||
assertArrayEquals(screenshot, newScreenshot);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCaptureBlinkingCaretIfExplicitlyAskedFor() {
|
||||
page.setContent(" <!-- Refer to stylesheet from other origin. Accessing this\n" +
|
||||
" stylesheet rules will throw.\n" +
|
||||
" -->\n" +
|
||||
" <link rel=stylesheet href=\"" + server.CROSS_PROCESS_PREFIX + "/injectedstyle.css'}\">\n" +
|
||||
" <!-- make life harder: define caret color in stylesheet -->\n" +
|
||||
" <style>\n" +
|
||||
" div {\n" +
|
||||
" caret-color: #000 !important;\n" +
|
||||
" }\n" +
|
||||
" </style>\n" +
|
||||
" <div contenteditable=\"true\"></div>\n");
|
||||
Locator div = page.locator("div");
|
||||
div.type("foo bar");
|
||||
byte[] screenshot = div.screenshot();
|
||||
boolean hasDifferentScreenshots = false;
|
||||
for (int i = 0; !hasDifferentScreenshots && i < 10; ++i) {
|
||||
// Caret blinking time is set to 500ms.
|
||||
// Try to capture variety of screenshots to make
|
||||
// sure we capture blinking caret.
|
||||
page.waitForTimeout(150);
|
||||
byte[] newScreenshot = div.screenshot(new Locator.ScreenshotOptions().setCaret(ScreenshotCaret.INITIAL));
|
||||
hasDifferentScreenshots = !Arrays.equals(newScreenshot, screenshot);
|
||||
}
|
||||
assertTrue(hasDifferentScreenshots);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,15 +17,22 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import com.microsoft.playwright.options.FilePayload;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
@@ -49,6 +56,46 @@ public class TestPageSetInputFiles extends TestBase {
|
||||
"}", input));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldUploadLargeFile(@TempDir Path tmpDir) throws IOException, ExecutionException, InterruptedException {
|
||||
Assumptions.assumeTrue(3 <= (Runtime.getRuntime().maxMemory() >> 30), "Fails if max heap size is < 3Gb");
|
||||
page.navigate(server.PREFIX + "/input/fileupload.html");
|
||||
Path uploadFile = tmpDir.resolve("200MB.zip");
|
||||
String str = String.join("", Collections.nCopies(4 * 1024, "A"));
|
||||
|
||||
try (Writer stream = new OutputStreamWriter(Files.newOutputStream(uploadFile))) {
|
||||
for (int i = 0; i < 50 * 1024; i++) {
|
||||
stream.write(str);
|
||||
}
|
||||
}
|
||||
Locator input = page.locator("input[type='file']");
|
||||
JSHandle events = input.evaluateHandle("e => {\n" +
|
||||
" const events = [];\n" +
|
||||
" e.addEventListener('input', () => events.push('input'));\n" +
|
||||
" e.addEventListener('change', () => events.push('change'));\n" +
|
||||
" return events;\n" +
|
||||
" }");
|
||||
input.setInputFiles(uploadFile);
|
||||
assertEquals("200MB.zip", input.evaluate("e => e.files[0].name"));
|
||||
assertEquals(asList("input", "change"), events.evaluate("e => e"));
|
||||
CompletableFuture<MultipartFormData> formData = new CompletableFuture<>();
|
||||
server.setRoute("/upload", exchange -> {
|
||||
try {
|
||||
MultipartFormData multipartFormData = MultipartFormData.parseRequest(exchange);
|
||||
formData.complete(multipartFormData);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
formData.completeExceptionally(e);
|
||||
}
|
||||
exchange.sendResponseHeaders(200, -1);
|
||||
});
|
||||
page.click("input[type=submit]", new Page.ClickOptions().setTimeout(90_000));
|
||||
List<MultipartFormData.Field> fields = formData.get().fields;
|
||||
assertEquals(1, fields.size());
|
||||
assertEquals("200MB.zip", fields.get(0).filename);
|
||||
assertEquals(200 * 1024 * 1024, fields.get(0).content.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldWork() {
|
||||
page.setContent("<input type=file>");
|
||||
|
||||
@@ -23,6 +23,8 @@ import java.io.OutputStreamWriter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static com.microsoft.playwright.Utils.expectedSSLError;
|
||||
@@ -239,15 +241,18 @@ public class TestPageWaitForNavigation extends TestBase {
|
||||
server.setRoute("/empty.html", exchange -> {});
|
||||
try {
|
||||
frame.waitForNavigation(() -> {
|
||||
page.evaluate("() => {\n" +
|
||||
" frames[0].location.href = '/empty.html';\n" +
|
||||
" setTimeout(() => document.querySelector('iframe').remove());\n" +
|
||||
"}\n");
|
||||
Future<Server.Request> req = server.futureRequest("/empty.html");
|
||||
page.evalOnSelector("iframe", "frame => { frame.contentWindow.location.href = '/empty.html'; }");
|
||||
try {
|
||||
req.get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
page.evaluate("setTimeout(() => document.querySelector('iframe').remove());");
|
||||
});
|
||||
fail("did not throw");
|
||||
} catch (PlaywrightException e) {
|
||||
// assertTrue(e.getMessage().contains("waiting for navigation until \"load\""));
|
||||
assertTrue(e.getMessage().contains("frame was detached"));
|
||||
assertTrue(e.getMessage().contains("frame was detached"), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,8 @@ public class TestPlaywrightCreate {
|
||||
getBrowserTypeFromEnv(playwright).launch();
|
||||
fail("Did not throw");
|
||||
} catch (PlaywrightException e) {
|
||||
assertTrue(e.getMessage().contains("Looks like Playwright Test or Playwright was just installed or updated"), e.getMessage());
|
||||
assertTrue(e.getMessage().contains("Looks like Playwright Test or Playwright was just installed or updated") ||
|
||||
e.getMessage().contains("Looks like Playwright was just installed or updated."), e.getMessage());
|
||||
}
|
||||
|
||||
try (DirectoryStream<Path> ds = Files.newDirectoryStream(browsersDir)) {
|
||||
|
||||
@@ -24,7 +24,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static com.microsoft.playwright.Utils.mapOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestRequestFulfill extends TestBase {
|
||||
@Test
|
||||
@@ -104,4 +104,45 @@ public class TestRequestFulfill extends TestBase {
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void fulfillShouldThrowIfHandledTwice() {
|
||||
try {
|
||||
page.route("**/*", route -> {
|
||||
route.fulfill();
|
||||
route.fulfill();
|
||||
});
|
||||
page.navigate(server.EMPTY_PAGE);
|
||||
fail("didn't throw");
|
||||
} catch (PlaywrightException e) {
|
||||
assertTrue(e.getMessage().contains("Route is already handled!"), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void abortShouldThrowIfHandledTwice() {
|
||||
try {
|
||||
page.route("**/*", route -> {
|
||||
route.abort();
|
||||
route.abort();
|
||||
});
|
||||
page.navigate(server.EMPTY_PAGE);
|
||||
fail("didn't throw");
|
||||
} catch (PlaywrightException e) {
|
||||
assertTrue(e.getMessage().contains("Route is already handled!"), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void resumeShouldThrowIfHandledTwice() {
|
||||
try {
|
||||
page.route("**/*", route -> {
|
||||
route.resume();
|
||||
route.resume();
|
||||
});
|
||||
page.navigate(server.EMPTY_PAGE);
|
||||
fail("didn't throw");
|
||||
} catch (PlaywrightException e) {
|
||||
assertTrue(e.getMessage().contains("Route is already handled!"), e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,221 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestSelectorsMisc extends TestBase {
|
||||
|
||||
@Test
|
||||
void shouldWorkWithLayoutSelectors() {
|
||||
/*
|
||||
|
||||
+--+ +--+
|
||||
| 1| | 2|
|
||||
+--+ ++-++
|
||||
| 3| | 4|
|
||||
+-------+ ++-++
|
||||
| 0 | | 5|
|
||||
| +--+ +--+--+
|
||||
| | 6| | 7|
|
||||
| +--+ +--+
|
||||
| |
|
||||
O-------+
|
||||
+--+
|
||||
| 8|
|
||||
+--++--+
|
||||
| 9|
|
||||
+--+
|
||||
|
||||
*/
|
||||
Object[][] boxes = {
|
||||
// x, y, width, height
|
||||
{0, 0, 150, 150},
|
||||
{100, 200, 50, 50},
|
||||
{200, 200, 50, 50},
|
||||
{100, 150, 50, 50},
|
||||
{201, 150, 50, 50},
|
||||
{200, 100, 50, 50},
|
||||
{50, 50, 50, 50},
|
||||
{150, 50, 50, 50},
|
||||
{150, -51, 50, 50},
|
||||
{201, -101, 50, 50},
|
||||
};
|
||||
page.setContent("<container style='width: 500px; height: 500px; position: relative;'></container>");
|
||||
page.evalOnSelector("container", "(container, boxes) => {\n" +
|
||||
" for (let i = 0; i < boxes.length; i++) {\n" +
|
||||
" const div = document.createElement('div');\n" +
|
||||
" div.style.position = 'absolute';\n" +
|
||||
" div.style.overflow = 'hidden';\n" +
|
||||
" div.style.boxSizing = 'border-box';\n" +
|
||||
" div.style.border = '1px solid black';\n" +
|
||||
" div.id = 'id' + i;\n" +
|
||||
" div.textContent = 'id' + i;\n" +
|
||||
" const box = boxes[i];\n" +
|
||||
" div.style.left = box[0] + 'px';\n" +
|
||||
" // Note that top is a flipped y coordinate.\n" +
|
||||
" div.style.top = (250 - box[1] - box[3]) + 'px';\n" +
|
||||
" div.style.width = box[2] + 'px';\n" +
|
||||
" div.style.height = box[3] + 'px';\n" +
|
||||
" container.appendChild(div);\n" +
|
||||
" const span = document.createElement('span');\n" +
|
||||
" span.textContent = '' + i;\n" +
|
||||
" div.appendChild(span);\n" +
|
||||
" }\n" +
|
||||
" }", boxes);
|
||||
|
||||
assertEquals("id7", page.evalOnSelector("div:right-of(#id6)", "e => e.id"));
|
||||
// assertEquals("id7", page.evalOnSelector("div >> right-of=\"#id6\"", "e => e.id"));
|
||||
// assertEquals("id7", page.locator("div", new Page.LocatorOptions().setRightOf(page.locator("#id6")))
|
||||
// .first().evaluate("e => e.id"));
|
||||
assertEquals("id2", page.evalOnSelector("div:right-of(#id1)", "e => e.id"));
|
||||
// assertEquals("id2", page.evalOnSelector("div >> right-of=\"#id1\"", "e => e.id"));
|
||||
assertEquals("id4", page.evalOnSelector("div:right-of(#id3)", "e => e.id"));
|
||||
// assertEquals("id4", page.evalOnSelector("div >> right-of=\"#id3\"", "e => e.id"));
|
||||
assertNull(page.querySelector("div:right-of(#id4)"));
|
||||
// assertNull(page.querySelector("div >> right-of=\"#id4\""));
|
||||
assertEquals("id7", page.evalOnSelector("div:right-of(#id0)", "e => e.id"));
|
||||
// assertEquals("id7", page.evalOnSelector("div >> right-of=\"#id0\"", "e => e.id"));
|
||||
assertEquals("id9", page.evalOnSelector("div:right-of(#id8)", "e => e.id"));
|
||||
// assertEquals("id9", page.evalOnSelector("div >> right-of=\"#id8\"", "e => e.id"));
|
||||
assertEquals("id4,id2,id5,id7,id8,id9", page.evalOnSelectorAll("div:right-of(#id3)", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("id4,id2,id5,id7,id8,id9", page.evalOnSelectorAll("div >> right-of=\"#id3\"", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("4,2,5,7,8,9", page.locator("div",
|
||||
// new Page.LocatorOptions().setRightOf(page.locator("#id3"))).locator("span")
|
||||
// .evaluateAll("els => els.map(e => e.textContent).join(',')"));
|
||||
assertEquals("id2,id5,id7,id8", page.evalOnSelectorAll("div:right-of(#id3, 50)", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("id2,id5,id7,id8", page.evalOnSelectorAll("div >> right-of=\"#id3\",50", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("2,5,7,8", page.evalOnSelectorAll("div >> right-of=\"#id3\",50 >> span", "els => els.map(e => e.textContent).join(',')"));
|
||||
// assertEquals("4,2,5,7,8,9", page.locator("div", new Page.LocatorOptions().setRightOf(page.locator("#id3")))
|
||||
// .locator("span").evaluateAll("els => els.map(e => e.textContent).join(',')"));
|
||||
assertEquals("id7,id8", page.evalOnSelectorAll("div:right-of(#id3, 49)", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("id7,id8", page.evalOnSelectorAll("div >> right-of=\"#id3\",49", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("7,8", page.evalOnSelectorAll("div >> right-of=\"#id3\",49 >> span", "els => els.map(e => e.textContent).join(',')"));
|
||||
// assertEquals("4,2,5,7,8,9", page.locator("div", new Page.LocatorOptions().setRightOf(page.locator("#id3")))
|
||||
// .locator("span").evaluateAll("els => els.map(e => e.textContent).join(',')"));
|
||||
|
||||
assertEquals("id1", page.evalOnSelector("div:left-of(#id2)", "e => e.id"));
|
||||
// assertEquals("id1", page.evalOnSelector("div >> left-of=\"#id2\"", "e => e.id"));
|
||||
// assertEquals("id1", page.locator("div", new Page.LocatorOptions().setLeftOf(page.locator("#id2"))).first().evaluate("e => e.id"));
|
||||
assertNull(page.querySelector("div:left-of(#id0)"));
|
||||
// assertNull(page.querySelector("div >> left-of=\"#id0\""));
|
||||
assertEquals("id0", page.evalOnSelector("div:left-of(#id5)", "e => e.id"));
|
||||
// assertEquals("id0", page.evalOnSelector("div >> left-of=\"#id5\"", "e => e.id"));
|
||||
assertEquals("id8", page.evalOnSelector("div:left-of(#id9)", "e => e.id"));
|
||||
// assertEquals("id8", page.evalOnSelector("div >> left-of=\"#id9\"", "e => e.id"));
|
||||
assertEquals("id3", page.evalOnSelector("div:left-of(#id4)", "e => e.id"));
|
||||
// assertEquals("id3", page.evalOnSelector("div >> left-of=\"#id4\"", "e => e.id"));
|
||||
assertEquals("id0,id7,id3,id1,id6,id8", page.evalOnSelectorAll("div:left-of(#id5)", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("id0,id7,id3,id1,id6,id8", page.evalOnSelectorAll("div >> left-of=\"#id5\"", "els => els.map(e => e.id).join(',')"));
|
||||
assertEquals("id7,id8", page.evalOnSelectorAll("div:left-of(#id5, 3)", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("id7,id8", page.evalOnSelectorAll("div >> left-of=\"#id5\",3", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("7,8", page.evalOnSelectorAll("div >> left-of=\"#id5\",3 >> span", "els => els.map(e => e.textContent).join(',')"));
|
||||
|
||||
assertEquals("id3", page.evalOnSelector("div:above(#id0)", "e => e.id"));
|
||||
// assertEquals("id3", page.evalOnSelector("div >> above=\"#id0\"", "e => e.id"));
|
||||
// assertEquals("id3", page.locator("div", new Page.LocatorOptions().setAbove(page.locator("#id0")))
|
||||
// .first().evaluate("e => e.id"));
|
||||
assertEquals("id4", page.evalOnSelector("div:above(#id5)", "e => e.id"));
|
||||
// assertEquals("id4", page.evalOnSelector("div >> above=\"#id5\"", "e => e.id"));
|
||||
assertEquals("id5", page.evalOnSelector("div:above(#id7)", "e => e.id"));
|
||||
// assertEquals("id5", page.evalOnSelector("div >> above=\"#id7\"", "e => e.id"));
|
||||
assertEquals("id0", page.evalOnSelector("div:above(#id8)", "e => e.id"));
|
||||
// assertEquals("id0", page.evalOnSelector("div >> above=\"#id8\"", "e => e.id"));
|
||||
assertEquals("id8", page.evalOnSelector("div:above(#id9)", "e => e.id"));
|
||||
// assertEquals("id8", page.evalOnSelector("div >> above=\"#id9\"", "e => e.id"));
|
||||
assertNull(page.querySelector("div:above(#id2)"));
|
||||
// assertNull(page.querySelector("div >> above=\"#id2\""));
|
||||
assertEquals("id4,id2,id3,id1", page.evalOnSelectorAll("div:above(#id5)", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("id4,id2,id3,id1", page.evalOnSelectorAll("div >> above=\"#id5\"", "els => els.map(e => e.id).join(',')"));
|
||||
assertEquals("id4,id3", page.evalOnSelectorAll("div:above(#id5, 20)", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("id4,id3", page.evalOnSelectorAll("div >> above=\"#id5\",20", "els => els.map(e => e.id).join(',')"));
|
||||
|
||||
assertEquals("id5", page.evalOnSelector("div:below(#id4)", "e => e.id"));
|
||||
// assertEquals("id5", page.evalOnSelector("div >> below=\"#id4\"", "e => e.id"));
|
||||
// assertEquals("id5", page.locator("div", new Page.LocatorOptions().setBelow(page.locator("#id4")))
|
||||
// .first().evaluate("e => e.id"));
|
||||
assertEquals("id0", page.evalOnSelector("div:below(#id3)", "e => e.id"));
|
||||
// assertEquals("id0", page.evalOnSelector("div >> below=\"#id3\"", "e => e.id"));
|
||||
assertEquals("id4", page.evalOnSelector("div:below(#id2)", "e => e.id"));
|
||||
// assertEquals("id4", page.evalOnSelector("div >> below=\"#id2\"", "e => e.id"));
|
||||
assertEquals("id8", page.evalOnSelector("div:below(#id6)", "e => e.id"));
|
||||
// assertEquals("id8", page.evalOnSelector("div >> below=\"#id6\"", "e => e.id"));
|
||||
assertEquals("id8", page.evalOnSelector("div:below(#id7)", "e => e.id"));
|
||||
// assertEquals("id8", page.evalOnSelector("div >> below=\"#id7\"", "e => e.id"));
|
||||
assertEquals("id9", page.evalOnSelector("div:below(#id8)", "e => e.id"));
|
||||
// assertEquals("id9", page.evalOnSelector("div >> below=\"#id8\"", "e => e.id"));
|
||||
assertNull(page.querySelector("div:below(#id9)"));
|
||||
// assertNull(page.querySelector("div >> below=\"#id9\""));
|
||||
assertEquals("id0,id5,id6,id7,id8,id9", page.evalOnSelectorAll("div:below(#id3)", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("id0,id5,id6,id7,id8,id9", page.evalOnSelectorAll("div >> below=\"#id3\"", "els => els.map(e => e.id).join(',')"));
|
||||
assertEquals("id0,id5,id6,id7", page.evalOnSelectorAll("div:below(#id3, 105)", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("id0,id5,id6,id7", page.evalOnSelectorAll("div >> below=\"#id3\" , 105", "els => els.map(e => e.id).join(',')"));
|
||||
|
||||
assertEquals("id3", page.evalOnSelector("div:near(#id0)", "e => e.id"));
|
||||
// assertEquals("id3", page.evalOnSelector("div >> near=\"#id0\"", "e => e.id"));
|
||||
// assertEquals("id3", page.locator("div", new Page.LocatorOptions().setNear(page.locator("#id0")))
|
||||
// .first().evaluate("e => e.id"));
|
||||
assertEquals("id0,id5,id3,id6", page.evalOnSelectorAll("div:near(#id7)", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("id0,id5,id3,id6", page.evalOnSelectorAll("div >> near=\"#id7\"", "els => els.map(e => e.id).join(',')"));
|
||||
assertEquals("id3,id6,id7,id8,id1,id5", page.evalOnSelectorAll("div:near(#id0)", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("id3,id6,id7,id8,id1,id5", page.evalOnSelectorAll("div >> near=\"#id0\"", "els => els.map(e => e.id).join(',')"));
|
||||
assertEquals("id0,id3,id7", page.evalOnSelectorAll("div:near(#id6)", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("id0,id3,id7", page.evalOnSelectorAll("div >> near=\"#id6\"", "els => els.map(e => e.id).join(',')"));
|
||||
assertEquals("id0", page.evalOnSelectorAll("div:near(#id6, 10)", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("id0", page.evalOnSelectorAll("div >> near=\"#id6\",10", "els => els.map(e => e.id).join(',')"));
|
||||
assertEquals("id3,id6,id7,id8,id1,id5,id4,id2", page.evalOnSelectorAll("div:near(#id0, 100)", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("id3,id6,id7,id8,id1,id5,id4,id2", page.evalOnSelectorAll("div >> near=\"#id0\",100", "els => els.map(e => e.id).join(',')"));
|
||||
|
||||
assertEquals("id7,id6", page.evalOnSelectorAll("div:below(#id5):above(#id8)", "els => els.map(e => e.id).join(',')"));
|
||||
// assertEquals("id7,id6", page.evalOnSelectorAll("div >> below=\"#id5\" >> above=\"#id8\"", "els => els.map(e => e.id).join(',')"));
|
||||
assertEquals("id7", page.evalOnSelector("div:below(#id5):above(#id8)", "e => e.id"));
|
||||
// assertEquals("id7", page.evalOnSelector("div >> below=\"#id5\" >> above=\"#id8\"", "e => e.id"));
|
||||
// assertEquals("id7", page.locator("div", new Page.LocatorOptions()
|
||||
// .setBelow(page.locator("#id5"))
|
||||
// .setAbove(page.locator("#id8"))).first().evaluate("e => e.id"));
|
||||
|
||||
assertEquals("id5,id6,id3", page.evalOnSelectorAll("div:right-of(#id0) + div:above(#id8)", "els => els.map(e => e.id).join(',')"));
|
||||
|
||||
try {
|
||||
ElementHandle error = page.querySelector(":near(50)");
|
||||
fail("did not throw");
|
||||
} catch (PlaywrightException e) {
|
||||
assertTrue(e.getMessage().contains("\"near\" engine expects a selector list and optional maximum distance in pixels"), e.getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
ElementHandle error1 = page.querySelector("div >> left-of=abc");
|
||||
fail("did not throw");
|
||||
} catch (PlaywrightException e) {
|
||||
assertTrue(e.getMessage().contains("Malformed selector: left-of=abc"));
|
||||
}
|
||||
|
||||
try {
|
||||
ElementHandle error2 = page.querySelector("left-of=\"div\"");
|
||||
fail("did not throw");
|
||||
} catch (PlaywrightException e) {
|
||||
assertTrue(e.getMessage().contains("\"left-of\" selector cannot be first"), e.getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
ElementHandle error3 = page.querySelector("div >> left-of=33");
|
||||
fail("did not throw");
|
||||
} catch (PlaywrightException e) {
|
||||
assertTrue(e.getMessage().contains("Malformed selector: left-of=33"));
|
||||
}
|
||||
|
||||
try {
|
||||
ElementHandle error4 = page.querySelector("div >> left-of='span','foo'");
|
||||
fail("did not throw");
|
||||
} catch (PlaywrightException e) {
|
||||
assertTrue(e.getMessage().contains("Malformed selector: left-of='span','foo'"));
|
||||
}
|
||||
|
||||
try {
|
||||
ElementHandle error5 = page.querySelector("div >> left-of='span',3,4");
|
||||
fail("did not throw");
|
||||
} catch (PlaywrightException e) {
|
||||
assertTrue(e.getMessage().contains("Malformed selector: left-of='span',3,4"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,6 @@ public class TestTracing extends TestBase {
|
||||
|
||||
@BeforeAll
|
||||
void launchBrowser(@TempDir Path tempDir) {
|
||||
System.out.println("new launchBrowser(");
|
||||
BrowserType.LaunchOptions options = createLaunchOptions();
|
||||
options.setTracesDir(tempDir.resolve("trace-dir"));
|
||||
launchBrowser(options);
|
||||
@@ -119,9 +118,17 @@ public class TestTracing extends TestBase {
|
||||
assertEquals(1, sources.size());
|
||||
|
||||
String path = getClass().getName().replace('.', File.separatorChar);
|
||||
Path sourceFile = Paths.get(System.getenv("PLAYWRIGHT_JAVA_SRC"), path + ".java");
|
||||
String[] srcRoots = System.getenv("PLAYWRIGHT_JAVA_SRC").split(File.pathSeparator);
|
||||
// Resolve in the last specified source dir.
|
||||
Path sourceFile = Paths.get(srcRoots[srcRoots.length - 1], path + ".java");
|
||||
byte[] thisFile = Files.readAllBytes(sourceFile);
|
||||
assertEquals(new String(thisFile, UTF_8), new String(sources.values().iterator().next(), UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotFailWhenSourcesSetExplicitlyToFalse() throws IOException {
|
||||
Assumptions.assumeTrue(System.getenv("PLAYWRIGHT_JAVA_SRC") == null, "PLAYWRIGHT_JAVA_SRC must not be set for this test");
|
||||
context.tracing().start(new Tracing.StartOptions().setSources(false));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,6 +24,24 @@ import static com.microsoft.playwright.Utils.mapOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestWheel extends TestBase {
|
||||
private void expectEvent(Map<String, Object> expected) {
|
||||
// Chromium reports deltaX/deltaY scaled by host device scale factor.
|
||||
// https://bugs.chromium.org/p/chromium/issues/detail?id=1324819
|
||||
// https://github.com/microsoft/playwright/issues/7362
|
||||
// Different bots have different scale factors (usually 1 or 2), so we just ignore the values
|
||||
// instead of guessing the host scale factor.
|
||||
// This first appeared in Chromium 102.
|
||||
boolean ignoreDelta = isChromium() && isMac;
|
||||
Map<String, Object> received = (Map<String, Object>) page.evaluate("window.lastEvent");
|
||||
if (ignoreDelta) {
|
||||
expected.remove("deltaX");
|
||||
expected.remove("deltaY");
|
||||
received.remove("deltaX");
|
||||
received.remove("deltaY");
|
||||
}
|
||||
assertEquals(expected, received);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDispatchWheelEvents() {
|
||||
page.setContent("<div style='width: 5000px; height: 5000px;'></div>");
|
||||
@@ -42,7 +60,7 @@ public class TestWheel extends TestBase {
|
||||
"altKey", false,
|
||||
"metaKey", false);
|
||||
page.waitForFunction("window.scrollY === 100");
|
||||
assertEquals(expected, page.evaluate("window.lastEvent"));
|
||||
expectEvent(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -71,7 +89,7 @@ public class TestWheel extends TestBase {
|
||||
"shiftKey", true,
|
||||
"altKey", false,
|
||||
"metaKey", false);
|
||||
assertEquals(expected, page.evaluate("window.lastEvent"));
|
||||
expectEvent(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -90,7 +108,7 @@ public class TestWheel extends TestBase {
|
||||
"shiftKey", false,
|
||||
"altKey", false,
|
||||
"metaKey", false);
|
||||
assertEquals(expected, page.evaluate("window.lastEvent"));
|
||||
expectEvent(expected);
|
||||
page.waitForFunction("window.scrollX === 100");
|
||||
}
|
||||
|
||||
@@ -113,7 +131,7 @@ public class TestWheel extends TestBase {
|
||||
"shiftKey", false,
|
||||
"altKey", false,
|
||||
"metaKey", false);
|
||||
assertEquals(expected, page.evaluate("window.lastEvent"));
|
||||
expectEvent(expected);
|
||||
// give the page a chacne to scroll
|
||||
page.waitForTimeout(100);
|
||||
// ensure that it did not.
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<!DOCTYPE HTML>
|
||||
<style>
|
||||
div {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 200px;
|
||||
height: 100px;
|
||||
background-color: red;
|
||||
transition: all 10s;
|
||||
}
|
||||
.transition {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
</style>
|
||||
<div></div>
|
||||
<script>
|
||||
window.addEventListener('load', () => {
|
||||
document.querySelector('div').classList.add('transition');
|
||||
}, false);
|
||||
</script>
|
||||
@@ -4,8 +4,8 @@
|
||||
<title>File upload test</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="/input/fileupload.html">
|
||||
<input type="file">
|
||||
<form action="/upload" method="post" enctype="multipart/form-data">
|
||||
<input type="file" name="file1">
|
||||
<input type="submit">
|
||||
</form>
|
||||
</body>
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<html>
|
||||
<head>
|
||||
<style type="text/css">
|
||||
div::after {
|
||||
position: absolute;
|
||||
content: " ";
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 200px;
|
||||
height: 100px;
|
||||
background-color: red;
|
||||
animation-name: z-spin;
|
||||
animation-duration: 5s;
|
||||
animation-iteration-count: infinite;
|
||||
animation-timing-function: linear;
|
||||
}
|
||||
div {
|
||||
position: absolute;
|
||||
width: 10px;
|
||||
height: 10px;
|
||||
}
|
||||
|
||||
@keyframes z-spin {
|
||||
0% { transform: rotateZ(0deg); }
|
||||
100% { transform: rotateZ(360deg); }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body >
|
||||
<div class="square"></div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,31 @@
|
||||
<html>
|
||||
<head></head>
|
||||
<body></body>
|
||||
<script>
|
||||
window.addEventListener('DOMContentLoaded', () => {
|
||||
const shadow = document.body.attachShadow({mode: 'open'});
|
||||
shadow.append(document.createElement('div'));
|
||||
const style = document.createElement('style');
|
||||
style.textContent = `
|
||||
div {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 200px;
|
||||
height: 100px;
|
||||
background-color: red;
|
||||
animation-name: z-spin;
|
||||
animation-duration: 5s;
|
||||
animation-iteration-count: infinite;
|
||||
animation-timing-function: linear;
|
||||
}
|
||||
|
||||
@keyframes z-spin {
|
||||
0% { transform: rotateZ(0deg); }
|
||||
100% { transform: rotateZ(360deg); }
|
||||
}
|
||||
`;
|
||||
shadow.append(style);
|
||||
}, false);
|
||||
</script>
|
||||
</html>
|
||||
@@ -13,7 +13,7 @@
|
||||
animation-iteration-count: infinite;
|
||||
animation-timing-function: linear;
|
||||
}
|
||||
|
||||
|
||||
@keyframes z-spin {
|
||||
0% { transform: rotateZ(0deg); }
|
||||
100% { transform: rotateZ(360deg); }
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
<!DOCTYPE HTML>
|
||||
<style>
|
||||
div {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 200px;
|
||||
height: 100px;
|
||||
background-color: red;
|
||||
}
|
||||
</style>
|
||||
<div></div>
|
||||
<script>
|
||||
document.querySelector('div').animate(
|
||||
[
|
||||
{ transform: 'rotate(0deg)' },
|
||||
{ transform: 'rotate(360deg)' }
|
||||
], {
|
||||
duration: 3000,
|
||||
iterations: Infinity
|
||||
}
|
||||
);
|
||||
</script>
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.microsoft.playwright</groupId>
|
||||
<artifactId>parent-pom</artifactId>
|
||||
<version>1.18.0-SNAPSHOT</version>
|
||||
<version>1.22.0</version>
|
||||
<packaging>pom</packaging>
|
||||
<name>Playwright Parent Project</name>
|
||||
<description>Java library to automate Chromium, Firefox and WebKit with a single API.
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user