Skip to content

Commit c15165b

Browse files
authored
feat(driver): read PLAYWRIGHT_DRIVER_DIR from Playwright.CreateOptions.env (#2051)
## Summary - Read `PLAYWRIGHT_DRIVER_DIR` from `Playwright.CreateOptions.env`, per call: such a call uses a `PreinstalledDriver` for that directory instead of the JVM-wide driver. - `playwright.cli.dir` still takes precedence, then the `env` of the call, then the process environment, as for `playwright.nodejs.path` and `PLAYWRIGHT_NODEJS_PATH`. - Like `playwright.cli.dir`, a preinstalled driver does not download browsers: run `install` beforehand. Fixes #2050
1 parent 5fb7f0f commit c15165b

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

  • driver/src/main/java/com/microsoft/playwright/impl/driver
  • playwright/src/test/java/com/microsoft/playwright/impl/driver/jar

‎driver/src/main/java/com/microsoft/playwright/impl/driver/Driver.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public Path driverDir() {
5555
}
5656

5757
public static synchronized Driver ensureDriverInstalled(Map<String, String> env, Boolean installBrowsers) {
58+
if (env.containsKey(PLAYWRIGHT_DRIVER_DIR)) {
59+
return createAndInstall(env, installBrowsers);
60+
}
5861
if (instance == null) {
5962
instance = createAndInstall(env, installBrowsers);
6063
}
@@ -98,7 +101,7 @@ private static String getMajorJavaVersion() {
98101
}
99102
public static Driver createAndInstall(Map<String, String> env, Boolean installBrowsers) {
100103
try {
101-
Driver instance = newInstance();
104+
Driver instance = newInstance(env);
102105
logMessage("initializing driver");
103106
instance.initialize(env, installBrowsers);
104107
logMessage("driver initialized.");
@@ -108,8 +111,11 @@ public static Driver createAndInstall(Map<String, String> env, Boolean installBr
108111
}
109112
}
110113

111-
private static Driver newInstance() throws Exception {
114+
private static Driver newInstance(Map<String, String> env) throws Exception {
112115
String driverDir = System.getProperty("playwright.cli.dir");
116+
if (driverDir == null) {
117+
driverDir = env.get(PLAYWRIGHT_DRIVER_DIR);
118+
}
113119
if (driverDir == null) {
114120
driverDir = System.getenv(PLAYWRIGHT_DRIVER_DIR);
115121
}

‎playwright/src/test/java/com/microsoft/playwright/impl/driver/jar/TestInstall.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.microsoft.playwright.impl.driver.jar;
1818

19+
import com.microsoft.playwright.Playwright;
20+
import com.microsoft.playwright.PlaywrightException;
1921
import com.microsoft.playwright.impl.driver.Driver;
2022
import org.junit.jupiter.api.BeforeEach;
2123
import org.junit.jupiter.api.Test;
@@ -39,6 +41,7 @@
3941
import java.util.concurrent.Future;
4042
import java.util.concurrent.TimeUnit;
4143

44+
import static com.microsoft.playwright.impl.driver.Driver.PLAYWRIGHT_DRIVER_DIR;
4245
import static com.microsoft.playwright.impl.driver.Driver.PLAYWRIGHT_NODEJS_PATH;
4346
import static java.util.Collections.singletonMap;
4447
import static org.junit.jupiter.api.Assertions.*;
@@ -182,6 +185,28 @@ void canInstallDriverToDirectoryAndReuseIt(@TempDir Path tmpDir) throws Exceptio
182185
assertTrue(stdout.contains("Version "), stdout);
183186
}
184187

188+
@Test
189+
void canUseTheDriverDirectoryOfEachCall(@TempDir Path tmpDir) throws Exception {
190+
// https://github.com/microsoft/playwright-java/issues/2050
191+
Path driverDir = tmpDir.resolve("driver");
192+
Path otherDriverDir = tmpDir.resolve("other-driver");
193+
194+
Driver driver = Driver.ensureDriverInstalled(singletonMap(PLAYWRIGHT_DRIVER_DIR, driverDir.toString()), false);
195+
assertEquals(driverDir, driver.driverDir());
196+
Driver otherDriver = Driver.ensureDriverInstalled(singletonMap(PLAYWRIGHT_DRIVER_DIR, otherDriverDir.toString()), false);
197+
assertEquals(otherDriverDir, otherDriver.driverDir());
198+
assertNotEquals(driverDir, Driver.ensureDriverInstalled(Collections.emptyMap(), false).driverDir());
199+
}
200+
201+
@Test
202+
void canUseTheDriverDirectoryOfEachPlaywrightInstance(@TempDir Path driverDir) {
203+
// https://github.com/microsoft/playwright-java/issues/2050
204+
Driver.ensureDriverInstalled(Collections.emptyMap(), false);
205+
Playwright.CreateOptions options = new Playwright.CreateOptions().setEnv(singletonMap(PLAYWRIGHT_DRIVER_DIR, driverDir.toString()));
206+
PlaywrightException e = assertThrows(PlaywrightException.class, () -> Playwright.create(options));
207+
assertTrue(e.getMessage().contains("Failed to launch driver"), e.getMessage());
208+
}
209+
185210
private static String extractNodeJsToTemp() throws URISyntaxException, IOException {
186211
DriverJar auxDriver = new DriverJar();
187212
auxDriver.extractDriverToTempDir();

0 commit comments

Comments
 (0)