mirror of
https://github.com/microsoft/playwright-java.git
synced 2025-09-08 21:01:00 +00:00
fix: package drivers for all platforms (#114)
This commit is contained in:
parent
b8a5f2c227
commit
283fecb30d
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
@ -28,7 +28,7 @@ jobs:
|
||||
restore-keys: ${{ runner.os }}-m2
|
||||
- name: Install driver
|
||||
shell: bash
|
||||
run: scripts/download_driver.sh
|
||||
run: scripts/download_driver_for_all_platforms.sh
|
||||
- name: Build with Maven
|
||||
run: mvn -B package -D skipTests --no-transfer-progress
|
||||
- name: Run tests
|
||||
|
63
driver-bundle/pom.xml
Normal file
63
driver-bundle/pom.xml
Normal file
@ -0,0 +1,63 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.microsoft.playwright</groupId>
|
||||
<artifactId>parent-pom</artifactId>
|
||||
<version>0.162.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>driver-bundle</artifactId>
|
||||
<name>Playwright Java - Drivers For All Platforms</name>
|
||||
<description>
|
||||
This module includes playwright-cli binary and related utilities for all supported platforms.
|
||||
It is intended to be used on the systems where Playwright driver is not preinstalled.
|
||||
</description>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludeResources>true</excludeResources>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<failOnError>false</failOnError>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-javadocs</id>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.0.0-M5</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.playwright</groupId>
|
||||
<artifactId>driver</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -16,8 +16,6 @@
|
||||
|
||||
package com.microsoft.playwright.impl;
|
||||
|
||||
import com.microsoft.playwright.PlaywrightException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Files;
|
||||
@ -32,12 +30,12 @@ public class DriverJar extends Driver {
|
||||
driverTempDir = Files.createTempDirectory("playwright-java-");
|
||||
driverTempDir.toFile().deleteOnExit();
|
||||
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
|
||||
Path path = Paths.get(classloader.getResource("driver").toURI());
|
||||
Path path = Paths.get(classloader.getResource("driver/" + platformDir()).toURI());
|
||||
Files.list(path).forEach(filePath -> {
|
||||
try {
|
||||
extractResource(filePath, driverTempDir);
|
||||
} catch (IOException e) {
|
||||
throw new PlaywrightException("Failed to extract driver from " + path, e);
|
||||
throw new RuntimeException("Failed to extract driver from " + path, e);
|
||||
}
|
||||
});
|
||||
|
||||
@ -52,6 +50,20 @@ public class DriverJar extends Driver {
|
||||
}
|
||||
}
|
||||
|
||||
private static String platformDir() {
|
||||
String name = System.getProperty("os.name").toLowerCase();
|
||||
if (name.contains("windows")) {
|
||||
return "win32_x64";
|
||||
}
|
||||
if (name.contains("linux")) {
|
||||
return "linux";
|
||||
}
|
||||
if (name.contains("mac os x")) {
|
||||
return "mac";
|
||||
}
|
||||
throw new RuntimeException("Unexpected os.name value: " + name);
|
||||
}
|
||||
|
||||
private static Path extractResource(Path from, Path toDir) throws IOException {
|
||||
Path path = toDir.resolve(from.getFileName());
|
||||
Files.copy(from, path);
|
2
driver-bundle/src/main/resources/.gitignore
vendored
Normal file
2
driver-bundle/src/main/resources/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
driver/
|
||||
local-driver/
|
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.microsoft.playwright.impl.Driver;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestInstall {
|
||||
@Test
|
||||
void playwrightCliInstalled() throws Exception {
|
||||
// Clear system property to ensure that the driver is loaded from jar.
|
||||
System.clearProperty("playwright.cli.dir");
|
||||
Path cli = Driver.ensureDriverInstalled();
|
||||
assertTrue(Files.exists(cli));
|
||||
|
||||
ProcessBuilder pb = new ProcessBuilder(cli.toString(), "install");
|
||||
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
|
||||
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
|
||||
Process p = pb.start();
|
||||
boolean result = p.waitFor(1, TimeUnit.MINUTES);
|
||||
assertTrue(result, "Timed out waiting for browsers to install");
|
||||
}
|
||||
}
|
@ -11,12 +11,8 @@
|
||||
|
||||
<artifactId>driver</artifactId>
|
||||
<name>Playwright Java - Driver</name>
|
||||
<description>Java library to automate Chromium, Firefox and WebKit with a single API.
|
||||
Playwright is built to enable cross-browser web automation that is ever-green, capable,
|
||||
reliable and fast.
|
||||
|
||||
This module includes playwright-cli binary and related utilities. It is intended to be
|
||||
used on the systems where Playwright driver is not preinstalled.
|
||||
<description>
|
||||
This module provides API for discovery and launching of playwright-cli binary.
|
||||
</description>
|
||||
|
||||
<build>
|
||||
@ -44,17 +40,10 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.0.0-M5</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.playwright</groupId>
|
||||
<artifactId>playwright</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
|
@ -16,12 +16,15 @@
|
||||
|
||||
package com.microsoft.playwright.impl;
|
||||
|
||||
import com.microsoft.playwright.PlaywrightException;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
abstract class Driver {
|
||||
/**
|
||||
* 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
|
||||
* loaded from the driver-bundle module if that module is in the classpath.
|
||||
*/
|
||||
public abstract class Driver {
|
||||
private static Driver instance;
|
||||
|
||||
private static class PreinstalledDriver extends Driver {
|
||||
@ -35,15 +38,17 @@ abstract class Driver {
|
||||
}
|
||||
}
|
||||
|
||||
static synchronized Path ensureDriverInstalled() {
|
||||
public static synchronized Path ensureDriverInstalled() {
|
||||
if (instance == null) {
|
||||
try {
|
||||
instance = createDriver();
|
||||
} catch (Exception exception) {
|
||||
throw new PlaywrightException("Failed to find playwright-cli", exception);
|
||||
throw new RuntimeException("Failed to find playwright-cli", exception);
|
||||
}
|
||||
}
|
||||
return instance.driverDir().resolve("playwright-cli");
|
||||
String name = System.getProperty("os.name").toLowerCase().contains("windows") ?
|
||||
"playwright-cli.exe" : "playwright-cli";
|
||||
return instance.driverDir().resolve(name);
|
||||
}
|
||||
|
||||
private static Driver createDriver() throws Exception {
|
1
driver/src/main/resources/.gitignore
vendored
1
driver/src/main/resources/.gitignore
vendored
@ -1 +0,0 @@
|
||||
driver/
|
@ -1,51 +0,0 @@
|
||||
package com.microsoft.playwright;/*
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestLaunch {
|
||||
@Test
|
||||
void launchBrowser() throws Exception {
|
||||
// Clear system property to ensure that the driver is loaded from jar.
|
||||
System.clearProperty("playwright.cli.dir");
|
||||
Playwright playwright = Playwright.create();
|
||||
String browserName = System.getenv("BROWSER");
|
||||
if (browserName == null) {
|
||||
browserName = "chromium";
|
||||
}
|
||||
BrowserType browserType;
|
||||
switch (browserName) {
|
||||
case "webkit":
|
||||
browserType = playwright.webkit();
|
||||
break;
|
||||
case "firefox":
|
||||
browserType = playwright.firefox();
|
||||
break;
|
||||
case "chromium":
|
||||
browserType = playwright.chromium();
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown browser: " + browserName);
|
||||
}
|
||||
String headfulEnv = System.getenv("HEADFUL");
|
||||
boolean headful = headfulEnv != null && !"0".equals(headfulEnv) && !"false".equals(headfulEnv);
|
||||
BrowserType.LaunchOptions options = new BrowserType.LaunchOptions();
|
||||
options.headless = !headful;
|
||||
Browser browser = browserType.launch(options);
|
||||
browser.close();
|
||||
playwright.close();
|
||||
}
|
||||
}
|
@ -50,11 +50,6 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<playwright.cli.dir>${project.parent.basedir}/driver/src/main/resources/driver</playwright.cli.dir>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
@ -67,6 +62,15 @@
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.playwright</groupId>
|
||||
<artifactId>driver</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.playwright</groupId>
|
||||
<artifactId>driver-bundle</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.java-websocket</groupId>
|
||||
<artifactId>Java-WebSocket</artifactId>
|
||||
|
50
pom.xml
50
pom.xml
@ -36,9 +36,10 @@
|
||||
</scm>
|
||||
|
||||
<modules>
|
||||
<module>playwright</module>
|
||||
<module>api-generator</module>
|
||||
<module>driver</module>
|
||||
<module>driver-bundle</module>
|
||||
<module>playwright</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
@ -50,6 +51,17 @@
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.playwright</groupId>
|
||||
<artifactId>driver</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.playwright</groupId>
|
||||
<artifactId>driver-bundle</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
@ -117,19 +129,6 @@
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-gpg-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>sign-artifacts</id>
|
||||
<phase>verify</phase>
|
||||
<goals>
|
||||
<goal>sign</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.sonatype.plugins</groupId>
|
||||
<artifactId>nexus-staging-maven-plugin</artifactId>
|
||||
@ -153,4 +152,27 @@
|
||||
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>release</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-gpg-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>sign-artifacts</id>
|
||||
<phase>verify</phase>
|
||||
<goals>
|
||||
<goal>sign</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
</project>
|
||||
|
32
scripts/download_driver_for_all_platforms.sh
Executable file
32
scripts/download_driver_for_all_platforms.sh
Executable file
@ -0,0 +1,32 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
set +x
|
||||
|
||||
FILE_PREFIX=playwright-cli-0.170.0-next.1607022026758
|
||||
|
||||
trap "cd $(pwd -P)" EXIT
|
||||
cd "$(dirname $0)"
|
||||
|
||||
cd ../driver-bundle/src/main/resources
|
||||
mkdir -p driver
|
||||
cd driver
|
||||
|
||||
for PLATFORM in mac linux win32_x64
|
||||
do
|
||||
FILE_NAME=$FILE_PREFIX-$PLATFORM.zip
|
||||
if [[ -d $PLATFORM ]]; then
|
||||
echo "Skipping driver download for $PLATFORM ($(pwd)/$PLATFORM already exists)"
|
||||
continue
|
||||
fi
|
||||
mkdir $PLATFORM
|
||||
cd $PLATFORM
|
||||
echo "Downloading driver for $PLATFORM to $(pwd)"
|
||||
|
||||
curl -O https://playwright.azureedge.net/builds/cli/next/${FILE_NAME}
|
||||
unzip ${FILE_NAME} -d .
|
||||
rm $FILE_NAME
|
||||
|
||||
cd -
|
||||
done
|
||||
|
@ -9,13 +9,13 @@ trap "cd $(pwd -P)" EXIT
|
||||
cd "$(dirname $0)"
|
||||
|
||||
cd ../driver/src/main/resources
|
||||
if [[ -d driver ]]; then
|
||||
if [[ -d local-driver ]]; then
|
||||
echo "$(pwd)/driver already exists, delete it first"
|
||||
exit 1;
|
||||
fi
|
||||
|
||||
mkdir driver
|
||||
cd driver
|
||||
mkdir local-driver
|
||||
cd local-driver
|
||||
echo "Created directory: $(pwd)"
|
||||
|
||||
FILE_NAME="unknown"
|
Loading…
x
Reference in New Issue
Block a user