Skip to content

Commit 9999a35

Browse files
committed
Split arguments by space only. Updated group ID to com.aldaviva.playwright. Allow extra arguments to be null to remove them.
1 parent 36eb545 commit 9999a35

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

Readme.md

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
🎭 playwright-extra-install-arguments
22
===
33

4-
[![Maven Central Version](https://img.shields.io/maven-central/v/com.aldaviva/playwright-extra-install-arguments?logo=apachemaven&label=maven&color=success)](https://central.sonatype.com/artifact/com.aldaviva/playwright-extra-install-arguments)
4+
[![Maven Central Version](https://img.shields.io/maven-central/v/com.aldaviva.playwright/playwright-extra-install-arguments?logo=apachemaven&label=maven&color=success)](https://central.sonatype.com/artifact/com.aldaviva.playwright/playwright-extra-install-arguments)
55

66
*Pass extra custom arguments to Playwright's `cli.js` install command, which lets you install one specific browser instead of all supported browsers.*
77

@@ -18,19 +18,19 @@
1818
<!-- /MarkdownTOC -->
1919

2020
## Problem
21-
Unfortunately, the [Java implementation of Playwright](https://playwright.dev/java/docs/intro) has a limitation in its API that prevents you from choosing which browser you want to download. Every time the Playwright is constructed, it will install the latest versions of all available browsers and their dependencies, which can take a lot of time, bandwidth, and disk space (**926 MB** on 2025-04-20). This is especially important in environments with constrained resources, like virtual machines, containers, Function-as-a-Service executions, or any machines with slow or expensive network connections, slow or small storage, or small transfer quotas.
21+
Unfortunately, the [Java implementation of Playwright](https://playwright.dev/java/docs/intro) has a limitation in its API that prevents you from choosing which browser you want to download. Every time Playwright is constructed, it will install the latest versions of all available browsers and their dependencies, which can take a lot of time, bandwidth, and disk space (**926 MB** on 2025-04-20). This is especially harmful in environments with constrained resources, like virtual machines, containers, Function-as-a-Service executions, or any machines with slow or expensive network connections, slow or small storage, or small transfer quotas.
2222

2323
The Node.js API does let you [specify exactly which browsers you want to download](https://playwright.dev/docs/browsers#install-browsers) by passing their names to the `install` command:
2424
```sh
2525
node cli.js install chromium
2626
```
2727

28-
Unfortunately, despite bundling and calling the Node.js library internally, the Java API does not expose this functionality, and always insists on calling `node cli.js install` without any browser names. This leads to all browers always being installed.
28+
Sadly, despite bundling and calling the Node.js library internally, the Java API does not in turn expose this functionality to its own consumers, and always insists on calling `node cli.js install` without any browser names. This leads to all browers always being installed. You may have heard of this phenomenon being referred to as an "API cliff."
2929

30-
This issue [has been raised](https://github.com/microsoft/playwright-java/issues/215) to the Playwright maintainers [multiple times](https://github.com/microsoft/playwright-java/issues/388), but they close it each time and offer an insufficient workaround:
31-
1. install Maven (a development tool) on your production deployment machine
32-
1. manually fork a new process `mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install chromium"`, along with all of the associated process management boilerplate and pitfalls
33-
1. call `Driver.ensureDriverInstalled(env, false)` before calling `PlaywrightImpl.create(CreateOptions)`
30+
This issue [has been raised](https://github.com/microsoft/playwright-java/issues/215) to the Playwright maintainers [multiple times](https://github.com/microsoft/playwright-java/issues/388), but they close it as won't-fix each time and offer an insufficient workaround:
31+
1. Install Maven (a development tool) on your production deployment machine.
32+
1. Manually fork a new process `mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install chromium"`, along with the necessity to deal with all of the associated process and file management boilerplate and pitfalls. How do you know what directory to call that in? What if your application is packaged inside a WAR or EAR?
33+
1. Call `Driver.ensureDriverInstalled(env, false)` before calling `PlaywrightImpl.create(CreateOptions)`.
3434

3535
## Solution
3636
This library offers an alternative which can easily install only your desired browsers, without forking any processes or manual installations.
@@ -46,7 +46,7 @@ Add a dependency on `com.aldaviva:playwright-extra-install-arguments` to your Ma
4646

4747
```xml
4848
<dependency>
49-
<groupId>com.aldaviva</groupId>
49+
<groupId>com.aldaviva.playwright</groupId>
5050
<artifactId>playwright-extra-install-arguments</artifactId>
5151
<version><!-- whatever the latest version is--></version>
5252
</dependency>
@@ -64,10 +64,17 @@ import com.aldaviva.playwright.ExtraInstallArgumentsDriver;
6464
public class Main {
6565

6666
public static void main(String[] args) {
67-
ExtraInstallArgumentsDriver.activate(); // register this Driver class
68-
CreateOptions createOptions = ExtraInstallArgumentsDriver.setExtraInstallArguments("chromium --with-deps --only-shell");
67+
// register this Driver class with Playwright
68+
ExtraInstallArgumentsDriver.activate();
6969

70+
// specify arguments to pass after `node cli.js install`
71+
CreateOptions createOptions =
72+
ExtraInstallArgumentsDriver.setExtraInstallArguments("chromium --with-deps --only-shell");
73+
74+
// create Playwright instance with options
7075
try (Playwright playwright = PlaywrightImpl.create(createOptions)) {
76+
77+
// use Playwright instance
7178
Browser chromium = playwright.chromium().launch(new LaunchOptions().setHeadless(true));
7279
BrowserContext browserContext = chromium.newContext();
7380

@@ -82,11 +89,19 @@ public class Main {
8289
```
8390

8491
- `ExtraInstallArgumentsDriver.activate()` must be called once before any calls to `PlaywrightImpl.create(CreateOptions)`, so that Playwright will use `ExtraInstallArgumentsDriver` instead of the default `DriverJar`.
85-
- Multiple extra arguments can be separated by a space, comma, or vertical pipe.
86-
- If you already have an existing `CreateOptions` instance you want to continue using, you may also manually set the extra arguments string as the `PLAYWRIGHT_EXTRA_INSTALL_ARGUMENTS` environment variable (whose name is exposed as the `ExtraInstallArgumentsDriver.EXTRA_INSTALL_ARGUMENTS` constant).
87-
```java
88-
CreateOptions createOptions = new CreateOptions().setEnv(new HashMap<>());
89-
createOptions.env.put(ExtraInstallArgumentsDriver.EXTRA_INSTALL_ARGUMENTS, "firefox");
90-
91-
try (Playwright playwright = PlaywrightImpl.create(createOptions)) {
92-
```
92+
- Multiple extra arguments can be separated by a space: `chromium --with-deps`
93+
- If you already have an existing `CreateOptions` instance you want to use, you may either
94+
- pass it to `ExtraInstallArgumentsDriver.setExtraInstallArguments(CreateOptions, String)`
95+
```java
96+
CreateOptions upstreamOptions;
97+
CreateOptions createOptions = ExtraInstallArgumentsDriver.setExtraInstallArguments(upstreamOptions, "webkit");
98+
99+
try (Playwright playwright = PlaywrightImpl.create(createOptions)) { /*...*/ }
100+
```
101+
- manually set the extra arguments string as the `PLAYWRIGHT_EXTRA_INSTALL_ARGUMENTS` environment variable (whose name is exposed as the `ExtraInstallArgumentsDriver.EXTRA_INSTALL_ARGUMENTS` constant).
102+
```java
103+
CreateOptions createOptions = new CreateOptions().setEnv(new HashMap<>());
104+
createOptions.env.put(ExtraInstallArgumentsDriver.EXTRA_INSTALL_ARGUMENTS, "firefox");
105+
106+
try (Playwright playwright = PlaywrightImpl.create(createOptions)) { /*...*/ }
107+
```

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
4-
<groupId>com.aldaviva</groupId>
4+
<groupId>com.aldaviva.playwright</groupId>
55
<artifactId>playwright-extra-install-arguments</artifactId>
6-
<version>0.0.1-SNAPSHOT</version>
6+
<version>0.0.1</version>
77
<name>Playwright Extra Install Arguments</name>
88
<description>Pass extra custom arguments to Playwright's cli.js install command, which lets you install one specific browser instead of all supported browsers.</description>
99
<inceptionYear>2025</inceptionYear>

src/main/java/com/aldaviva/playwright/ExtraInstallArgumentsDriver.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* <ul><li>Call {@link ExtraInstallArgumentsDriver#activate()}.</li>
2121
* <li>Set the {@code playwright.driver.impl} environment variable to {@code com.aldaviva.playwright.ExtraInstallArgumentsDriver} (using {@link System#setProperty(String, String)}).</li></ul>
2222
*
23-
* <li>Specify the extra arguments you want to pass to {@code node cli.js install} by doing one of the following. Multiple arguments are delimited by spaces, commas, or vertical pipes.
23+
* <li>Specify the extra arguments you want to pass to {@code node cli.js install} by doing one of the following. Multiple arguments are delimited by spaces.
2424
* <ul><li>{@code CreateOptions createOptions = ExtraInstallArgumentsDriver.setExtraInstallArguments("chromium --with-deps --no-shell");}</li>
2525
* <li><code>CreateOptions createOptions = new CreateOptions().setEnv(new HashMap&lt;&gt;());
2626
* createOptions.env.put(ExtraInstallArgumentsDriver.EXTRA_INSTALL_ARGUMENTS, "chromium --with-deps --no-shell");</code></li></ul></li>
@@ -98,7 +98,12 @@ public static CreateOptions setExtraInstallArguments(CreateOptions createOptions
9898
createOptions.setEnv(env);
9999
}
100100

101-
env.put(EXTRA_INSTALL_ARGUMENTS, args);
101+
if (args != null) {
102+
env.put(EXTRA_INSTALL_ARGUMENTS, args);
103+
} else {
104+
env.remove(EXTRA_INSTALL_ARGUMENTS);
105+
}
106+
102107
return createOptions;
103108
}
104109

@@ -107,7 +112,7 @@ public ProcessBuilder createProcessBuilder() {
107112
final ProcessBuilder processBuilder = super.createProcessBuilder();
108113
final List<String> oldCommands = processBuilder.command();
109114
final String extraArgs = env.get(EXTRA_INSTALL_ARGUMENTS);
110-
final List<String> extraArgList = extraArgs != null ? Arrays.asList(extraArgs.split(",| ")) : null;
115+
final List<String> extraArgList = extraArgs != null ? Arrays.asList(extraArgs.split(" ")) : null;
111116
return processBuilder.command(new ExtraInstallArgumentsList(oldCommands, extraArgList));
112117
}
113118

0 commit comments

Comments
 (0)