1
0
mirror of synced 2026-07-19 14:55:07 +00:00

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
13 changed files with 218 additions and 98 deletions
@@ -0,0 +1,65 @@
/*
* 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.nio.file.Path;
import java.nio.file.Paths;
/**
* 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 {
private final Path driverDir;
PreinstalledDriver(Path driverDir) {
this.driverDir = driverDir;
}
@Override
Path driverDir() {
return driverDir;
}
}
public static synchronized Path ensureDriverInstalled() {
if (instance == null) {
try {
instance = createDriver();
} catch (Exception exception) {
throw new RuntimeException("Failed to find playwright-cli", exception);
}
}
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 {
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();
}
@@ -1,68 +0,0 @@
/*
* 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;
public 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;
}
}
-1
View File
@@ -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();
}
}