chore: move driver into separate jar (#81)

This commit is contained in:
Yury Semikhatsky 2020-12-01 17:38:32 -08:00 committed by GitHub
parent 8c07d241b0
commit 507e72c2cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 224 additions and 47 deletions

1
.gitignore vendored
View File

@ -19,4 +19,3 @@ target/
.idea .idea
*.iml *.iml
/driver/

View File

@ -11,7 +11,7 @@ git clone https://github.com/microsoft/playwright-java
cd playwright-java cd playwright-java
``` ```
2. Run the following script to download playwright-cli binary for your platform into `playwright/src/main/resources/driver/` directory. It will also install Playwright and download browser binaries for Chromium, Firefox and WebKit. 2. Run the following script to download playwright-cli binary for your platform into `driver/src/main/resources/driver/` directory. It will also install Playwright and download browser binaries for Chromium, Firefox and WebKit.
```bash ```bash
scripts/download_driver.sh scripts/download_driver.sh

33
driver/pom.xml Normal file
View File

@ -0,0 +1,33 @@
<?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>playwright-java</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>
<artifactId>driver</artifactId>
<build>
<plugins>
<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>0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,68 @@
/*
* 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 com.microsoft.playwright.PlaywrightException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
class DriverJar extends Driver {
private final Path driverTempDir;
DriverJar() throws IOException, URISyntaxException, InterruptedException {
driverTempDir = Files.createTempDirectory("playwright-java-");
driverTempDir.toFile().deleteOnExit();
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
Path path = Paths.get(classloader.getResource("driver").toURI());
Files.list(path).forEach(filePath -> {
try {
extractResource(filePath, driverTempDir);
} catch (IOException e) {
throw new PlaywrightException("Failed to extract driver from " + path, e);
}
});
Path driver = driverTempDir.resolve("playwright-cli");
ProcessBuilder pb = new ProcessBuilder(driver.toString(), "install");
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
Process p = pb.start();
boolean result = p.waitFor(10, TimeUnit.MINUTES);
if (!result) {
System.err.println("Timed out waiting for browsers to install");
}
}
private static Path extractResource(Path from, Path toDir) throws IOException {
Path path = toDir.resolve(from.getFileName());
Files.copy(from, path);
path.toFile().setExecutable(true);
path.toFile().deleteOnExit();
// System.out.println("extracting: " + from.toString() + " to " + path.toString());
return path;
}
@Override
Path driverDir() {
return driverTempDir;
}
}

View File

@ -0,0 +1,51 @@
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

@ -27,6 +27,11 @@
<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> <version>3.0.0-M5</version>
<configuration>
<systemPropertyVariables>
<playwright.cli.dir>${project.parent.basedir}/driver/src/main/resources/driver</playwright.cli.dir>
</systemPropertyVariables>
</configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>

View File

@ -0,0 +1,60 @@
/*
* 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 com.microsoft.playwright.PlaywrightException;
import java.nio.file.Path;
import java.nio.file.Paths;
abstract class Driver {
private static Driver instance;
private static class PreinstalledDriver extends Driver {
private final Path driverDir;
PreinstalledDriver(Path driverDir) {
this.driverDir = driverDir;
}
@Override
Path driverDir() {
return driverDir;
}
}
static synchronized Path ensureDriverInstalled() {
if (instance == null) {
try {
instance = createDriver();
} catch (Exception exception) {
throw new PlaywrightException("Failed to find playwright-cli", exception);
}
}
return instance.driverDir().resolve("playwright-cli");
}
private static Driver createDriver() throws Exception {
String pathFromProperty = System.getProperty("playwright.cli.dir");
if (pathFromProperty != null) {
return new PreinstalledDriver(Paths.get(pathFromProperty));
}
Class<?> jarDriver = Class.forName("com.microsoft.playwright.impl.DriverJar");
return (Driver) jarDriver.getDeclaredConstructor().newInstance();
}
abstract Path driverDir();
}

View File

@ -25,21 +25,17 @@ import com.microsoft.playwright.PlaywrightException;
import com.microsoft.playwright.Selectors; import com.microsoft.playwright.Selectors;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class PlaywrightImpl extends ChannelOwner implements Playwright { public class PlaywrightImpl extends ChannelOwner implements Playwright {
private static Path driverTempDir;
private Process driverProcess; private Process driverProcess;
public static PlaywrightImpl create() { public static PlaywrightImpl create() {
try { try {
Path driver = ensureDriverInstalled(); Path driver = Driver.ensureDriverInstalled();
ProcessBuilder pb = new ProcessBuilder(driver.toString(), "run-driver"); ProcessBuilder pb = new ProcessBuilder(driver.toString(), "run-driver");
pb.redirectError(ProcessBuilder.Redirect.INHERIT); pb.redirectError(ProcessBuilder.Redirect.INHERIT);
// pb.environment().put("DEBUG", "pw:pro*"); // pb.environment().put("DEBUG", "pw:pro*");
@ -48,47 +44,11 @@ public class PlaywrightImpl extends ChannelOwner implements Playwright {
PlaywrightImpl result = (PlaywrightImpl) connection.waitForObjectWithKnownName("Playwright"); PlaywrightImpl result = (PlaywrightImpl) connection.waitForObjectWithKnownName("Playwright");
result.driverProcess = p; result.driverProcess = p;
return result; return result;
} catch (IOException | InterruptedException | URISyntaxException e) { } catch (IOException e) {
throw new PlaywrightException("Failed to launch driver", e); throw new PlaywrightException("Failed to launch driver", e);
} }
} }
private static synchronized Path ensureDriverInstalled() throws IOException, InterruptedException, URISyntaxException {
if (driverTempDir == null) {
driverTempDir = Files.createTempDirectory("playwright-java-");
driverTempDir.toFile().deleteOnExit();
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
Path path = Paths.get(classloader.getResource("driver").toURI());
Files.list(path).forEach(filePath -> {
try {
extractResource(filePath, driverTempDir);
} catch (IOException e) {
throw new PlaywrightException("Failed to extract driver from " + path, e);
}
});
Path driver = driverTempDir.resolve("playwright-cli");
ProcessBuilder pb = new ProcessBuilder(driver.toString(), "install");
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
Process p = pb.start();
boolean result = p.waitFor(10, TimeUnit.MINUTES);
if (!result) {
System.err.println("Timed out waiting for browsers to install");
}
}
return driverTempDir.resolve("playwright-cli");
}
private static Path extractResource(Path from, Path toDir) throws IOException {
Path path = toDir.resolve(from.getFileName());
Files.copy(from, path);
path.toFile().setExecutable(true);
path.toFile().deleteOnExit();
// System.out.println("extracting: " + from.toString() + " to " + path.toString());
return path;
}
private final BrowserTypeImpl chromium; private final BrowserTypeImpl chromium;
private final BrowserTypeImpl firefox; private final BrowserTypeImpl firefox;
private final BrowserTypeImpl webkit; private final BrowserTypeImpl webkit;

View File

@ -11,8 +11,9 @@
<name>Playwright Java</name> <name>Playwright Java</name>
<modules> <modules>
<module>playwright</module> <module>playwright</module>
<module>api-generator</module> <module>api-generator</module>
<module>driver</module>
</modules> </modules>
<properties> <properties>

View File

@ -8,7 +8,7 @@ FILE_PREFIX=playwright-cli-0.170.0-next.1605573954344
trap "cd $(pwd -P)" EXIT trap "cd $(pwd -P)" EXIT
cd "$(dirname $0)" cd "$(dirname $0)"
cd ../playwright/src/main/resources cd ../driver/src/main/resources
if [[ -d driver ]]; then if [[ -d driver ]]; then
echo "$(pwd)/driver already exists, delete it first" echo "$(pwd)/driver already exists, delete it first"
exit 1; exit 1;