From b09b9aecfb08ec4d94e65879c669db3c6d2a694f Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Thu, 6 Jan 2022 14:36:39 -0800 Subject: [PATCH] feat: custom temp dir for driver via playwright.driver.tmpdir property (#763) --- .../microsoft/playwright/impl/DriverJar.java | 15 +++++++++----- .../com/microsoft/playwright/TestInstall.java | 20 +++++++++++++++++-- .../com/microsoft/playwright/impl/Driver.java | 8 ++++---- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/driver-bundle/src/main/java/com/microsoft/playwright/impl/DriverJar.java b/driver-bundle/src/main/java/com/microsoft/playwright/impl/DriverJar.java index 1cb24319..ba41fb11 100644 --- a/driver-bundle/src/main/java/com/microsoft/playwright/impl/DriverJar.java +++ b/driver-bundle/src/main/java/com/microsoft/playwright/impl/DriverJar.java @@ -28,8 +28,14 @@ public class DriverJar extends Driver { private static final String PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD = "PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD"; private final Path driverTempDir; - DriverJar() throws IOException { - driverTempDir = Files.createTempDirectory("playwright-java-"); + public DriverJar() throws IOException { + // Allow specifying custom path for the driver installation + // See https://github.com/microsoft/playwright-java/issues/728 + String alternativeTmpdir = System.getProperty("playwright.driver.tmpdir"); + String prefix = "playwright-java-"; + driverTempDir = alternativeTmpdir == null + ? Files.createTempDirectory(prefix) + : Files.createTempDirectory(Paths.get(alternativeTmpdir), prefix); driverTempDir.toFile().deleteOnExit(); } @@ -48,10 +54,9 @@ public class DriverJar extends Driver { System.out.println("Skipping browsers download because `PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD` env variable is set"); return; } - String cliFileName = super.cliFileName(); - Path driver = driverTempDir.resolve(cliFileName); + Path driver = driverPath(); if (!Files.exists(driver)) { - throw new RuntimeException("Failed to find " + cliFileName + " at " + driver); + throw new RuntimeException("Failed to find driver: " + driver); } ProcessBuilder pb = new ProcessBuilder(driver.toString(), "install"); pb.environment().putAll(env); diff --git a/driver-bundle/src/test/java/com/microsoft/playwright/TestInstall.java b/driver-bundle/src/test/java/com/microsoft/playwright/TestInstall.java index 2446ea1c..1cc8d04a 100644 --- a/driver-bundle/src/test/java/com/microsoft/playwright/TestInstall.java +++ b/driver-bundle/src/test/java/com/microsoft/playwright/TestInstall.java @@ -17,7 +17,11 @@ 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.io.TempDir; import java.nio.file.Files; import java.nio.file.Path; @@ -27,10 +31,15 @@ import java.util.concurrent.TimeUnit; import static org.junit.jupiter.api.Assertions.assertTrue; public class TestInstall { - @Test - void playwrightCliInstalled() throws Exception { + @BeforeEach + void clearSystemProperties() { // Clear system property to ensure that the driver is loaded from jar. System.clearProperty("playwright.cli.dir"); + System.clearProperty("playwright.driver.tmpdir"); + } + + @Test + void playwrightCliInstalled() throws Exception { Path cli = Driver.ensureDriverInstalled(Collections.emptyMap()); assertTrue(Files.exists(cli)); @@ -41,4 +50,11 @@ public class TestInstall { boolean result = p.waitFor(1, TimeUnit.MINUTES); assertTrue(result, "Timed out waiting for browsers to install"); } + + @Test + void playwrightDriverInAlternativeTmpdir(@TempDir Path tmpdir) throws Exception { + System.setProperty("playwright.driver.tmpdir", tmpdir.toString()); + DriverJar driver = new DriverJar(); + assertTrue(driver.driverPath().startsWith(tmpdir), "Driver path: " + driver.driverPath() + " tmp: " + tmpdir); + } } diff --git a/driver/src/main/java/com/microsoft/playwright/impl/Driver.java b/driver/src/main/java/com/microsoft/playwright/impl/Driver.java index df0077fe..051d92dc 100644 --- a/driver/src/main/java/com/microsoft/playwright/impl/Driver.java +++ b/driver/src/main/java/com/microsoft/playwright/impl/Driver.java @@ -54,15 +54,15 @@ public abstract class Driver { throw new RuntimeException("Failed to create driver", exception); } } - String name = instance.cliFileName(); - return instance.driverDir().resolve(name); + return instance.driverPath(); } protected abstract void initialize(Map env) throws Exception; - protected String cliFileName() { - return System.getProperty("os.name").toLowerCase().contains("windows") ? + public Path driverPath() { + String cliFileName = System.getProperty("os.name").toLowerCase().contains("windows") ? "playwright.cmd" : "playwright.sh"; + return driverDir().resolve(cliFileName); } private static Driver createDriver() throws Exception {