@@ -30,17 +30,11 @@ public class DriverJar extends Driver {
3030 private static final String PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD = "PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD" ;
3131 private static final String SELENIUM_REMOTE_URL = "SELENIUM_REMOTE_URL" ;
3232 private final Path driverTempDir ;
33+ private final boolean deleteOnExit ;
3334 private Path preinstalledNodePath ;
3435
3536 public DriverJar () throws IOException {
36- // Allow specifying custom path for the driver installation
37- // See https://github.com/microsoft/playwright-java/issues/728
38- String alternativeTmpdir = System .getProperty ("playwright.driver.tmpdir" );
39- String prefix = "playwright-java-" ;
40- driverTempDir = alternativeTmpdir == null
41- ? Files .createTempDirectory (prefix )
42- : Files .createTempDirectory (Paths .get (alternativeTmpdir ), prefix );
43- driverTempDir .toFile ().deleteOnExit ();
37+ this (createTempDriverDir (), true );
4438 String nodePath = System .getProperty ("playwright.nodejs.path" );
4539 if (nodePath != null ) {
4640 preinstalledNodePath = Paths .get (nodePath );
@@ -51,6 +45,32 @@ public DriverJar() throws IOException {
5145 logMessage ("created DriverJar: " + driverTempDir );
5246 }
5347
48+ private DriverJar (Path driverDir , boolean deleteOnExit ) {
49+ this .driverTempDir = driverDir ;
50+ this .deleteOnExit = deleteOnExit ;
51+ if (deleteOnExit ) {
52+ driverTempDir .toFile ().deleteOnExit ();
53+ }
54+ }
55+
56+ private static Path createTempDriverDir () throws IOException {
57+ // Allow specifying custom path for the driver installation
58+ // See https://github.com/microsoft/playwright-java/issues/728
59+ String alternativeTmpdir = System .getProperty ("playwright.driver.tmpdir" );
60+ String prefix = "playwright-java-" ;
61+ return alternativeTmpdir == null
62+ ? Files .createTempDirectory (prefix )
63+ : Files .createTempDirectory (Paths .get (alternativeTmpdir ), prefix );
64+ }
65+
66+ // Extracts the driver (playwright-core package and the Node.js binary for the current platform)
67+ // into the given directory, persistently. Point playwright.cli.dir / PLAYWRIGHT_DRIVER_DIR at it
68+ // to run without extracting to a temp directory on every launch. See issue #1268.
69+ public static void installDriverTo (Path driverDir ) throws IOException , URISyntaxException {
70+ Files .createDirectories (driverDir );
71+ new DriverJar (driverDir , false ).extractDriverToTempDir ();
72+ }
73+
5474 @ Override
5575 protected void initialize (Boolean installBrowsers ) throws Exception {
5676 if (preinstalledNodePath == null && env .containsKey (PLAYWRIGHT_NODEJS_PATH )) {
@@ -156,7 +176,9 @@ private void extractResourceToDir(String resourcePath, Path destDir) throws URIS
156176 toPath .toFile ().setExecutable (true , true );
157177 }
158178 }
159- toPath .toFile ().deleteOnExit ();
179+ if (deleteOnExit ) {
180+ toPath .toFile ().deleteOnExit ();
181+ }
160182 } catch (IOException e ) {
161183 throw new RuntimeException ("Failed to extract driver from " + uri + ", full uri: " + originalUri , e );
162184 }
@@ -179,7 +201,9 @@ private URI maybeExtractNestedJar(final URI uri) throws URISyntaxException {
179201 Path fromPath = Paths .get (jarUri );
180202 Path toPath = driverTempDir .resolve (fromPath .getFileName ().toString ());
181203 Files .copy (fromPath , toPath );
182- toPath .toFile ().deleteOnExit ();
204+ if (deleteOnExit ) {
205+ toPath .toFile ().deleteOnExit ();
206+ }
183207 return new URI ("jar:" + toPath .toUri () + JAR_URL_SEPARATOR + parts [2 ]);
184208 } catch (IOException e ) {
185209 throw new RuntimeException ("Failed to extract driver's nested .jar from " + jarUri + "; full uri: " + uri , e );
0 commit comments