fix: package drivers for all platforms (#114)

This commit is contained in:
Yury Semikhatsky 2020-12-11 13:54:59 -08:00 committed by GitHub
parent b8a5f2c227
commit 283fecb30d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 218 additions and 98 deletions

View File

@ -28,7 +28,7 @@ jobs:
restore-keys: ${{ runner.os }}-m2 restore-keys: ${{ runner.os }}-m2
- name: Install driver - name: Install driver
shell: bash shell: bash
run: scripts/download_driver.sh run: scripts/download_driver_for_all_platforms.sh
- name: Build with Maven - name: Build with Maven
run: mvn -B package -D skipTests --no-transfer-progress run: mvn -B package -D skipTests --no-transfer-progress
- name: Run tests - name: Run tests

63
driver-bundle/pom.xml Normal file
View 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>

View File

@ -16,8 +16,6 @@
package com.microsoft.playwright.impl; package com.microsoft.playwright.impl;
import com.microsoft.playwright.PlaywrightException;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.file.Files; import java.nio.file.Files;
@ -32,12 +30,12 @@ public class DriverJar extends Driver {
driverTempDir = Files.createTempDirectory("playwright-java-"); driverTempDir = Files.createTempDirectory("playwright-java-");
driverTempDir.toFile().deleteOnExit(); driverTempDir.toFile().deleteOnExit();
ClassLoader classloader = Thread.currentThread().getContextClassLoader(); 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 -> { Files.list(path).forEach(filePath -> {
try { try {
extractResource(filePath, driverTempDir); extractResource(filePath, driverTempDir);
} catch (IOException e) { } 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 { private static Path extractResource(Path from, Path toDir) throws IOException {
Path path = toDir.resolve(from.getFileName()); Path path = toDir.resolve(from.getFileName());
Files.copy(from, path); Files.copy(from, path);

View File

@ -0,0 +1,2 @@
driver/
local-driver/

View File

@ -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");
}
}

View File

@ -11,12 +11,8 @@
<artifactId>driver</artifactId> <artifactId>driver</artifactId>
<name>Playwright Java - Driver</name> <name>Playwright Java - Driver</name>
<description>Java library to automate Chromium, Firefox and WebKit with a single API. <description>
Playwright is built to enable cross-browser web automation that is ever-green, capable, This module provides API for discovery and launching of playwright-cli binary.
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> </description>
<build> <build>
@ -44,17 +40,10 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<dependencies> <dependencies>
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId> <artifactId>junit-jupiter-engine</artifactId>

View File

@ -16,12 +16,15 @@
package com.microsoft.playwright.impl; package com.microsoft.playwright.impl;
import com.microsoft.playwright.PlaywrightException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; 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 Driver instance;
private static class PreinstalledDriver extends Driver { 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) { if (instance == null) {
try { try {
instance = createDriver(); instance = createDriver();
} catch (Exception exception) { } 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 { private static Driver createDriver() throws Exception {

View File

@ -1 +0,0 @@
driver/

View File

@ -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();
}
}

View File

@ -50,11 +50,6 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<playwright.cli.dir>${project.parent.basedir}/driver/src/main/resources/driver</playwright.cli.dir>
</systemPropertyVariables>
</configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
@ -67,6 +62,15 @@
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId> <artifactId>junit-jupiter-engine</artifactId>
</dependency> </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> <dependency>
<groupId>org.java-websocket</groupId> <groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId> <artifactId>Java-WebSocket</artifactId>

50
pom.xml
View File

@ -36,9 +36,10 @@
</scm> </scm>
<modules> <modules>
<module>playwright</module>
<module>api-generator</module> <module>api-generator</module>
<module>driver</module> <module>driver</module>
<module>driver-bundle</module>
<module>playwright</module>
</modules> </modules>
<properties> <properties>
@ -50,6 +51,17 @@
<dependencyManagement> <dependencyManagement>
<dependencies> <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> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
@ -117,19 +129,6 @@
</plugins> </plugins>
</pluginManagement> </pluginManagement>
<plugins> <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> <plugin>
<groupId>org.sonatype.plugins</groupId> <groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId> <artifactId>nexus-staging-maven-plugin</artifactId>
@ -153,4 +152,27 @@
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository> </repository>
</distributionManagement> </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> </project>

View 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

View File

@ -9,13 +9,13 @@ trap "cd $(pwd -P)" EXIT
cd "$(dirname $0)" cd "$(dirname $0)"
cd ../driver/src/main/resources cd ../driver/src/main/resources
if [[ -d driver ]]; then if [[ -d local-driver ]]; then
echo "$(pwd)/driver already exists, delete it first" echo "$(pwd)/driver already exists, delete it first"
exit 1; exit 1;
fi fi
mkdir driver mkdir local-driver
cd driver cd local-driver
echo "Created directory: $(pwd)" echo "Created directory: $(pwd)"
FILE_NAME="unknown" FILE_NAME="unknown"