Skip to content

Commit 3622200

Browse files
Raphael HehlRaphael Hehl
authored andcommitted
feat(api): add launchOptions support to ConnectOptions
Add ability to pass LaunchOptions directly via ConnectOptions instead of manually constructing JSON headers. This provides a type-safe and convenient way to specify launch options like browser channel when connecting to a remote browser. The implementation automatically serializes launchOptions to the x-playwright-launch-options header when provided.
1 parent 98296d9 commit 3622200

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

playwright/src/main/java/com/microsoft/playwright/BrowserType.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,23 @@ class ConnectOptions {
6767
* Additional HTTP headers to be sent with web socket connect request. Optional.
6868
*/
6969
public Map<String, String> headers;
70+
/**
71+
* Launch options to be passed to the browser being connected to. These options will be automatically
72+
* serialized and sent via the {@code x-playwright-launch-options} header.
73+
*
74+
* <p> This is particularly useful when connecting to a browser with specific requirements, such as using
75+
* a specific browser channel (e.g., "msedge" for Microsoft Edge).
76+
*
77+
* <p> Example:
78+
* <pre>{@code
79+
* BrowserType.LaunchOptions launchOptions = new BrowserType.LaunchOptions()
80+
* .setChannel("msedge");
81+
* BrowserType.ConnectOptions connectOptions = new BrowserType.ConnectOptions()
82+
* .setLaunchOptions(launchOptions);
83+
* Browser browser = browserType.connect(wsEndpoint, connectOptions);
84+
* }</pre>
85+
*/
86+
public LaunchOptions launchOptions;
7087
/**
7188
* Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
7289
* Defaults to 0.
@@ -107,6 +124,26 @@ public ConnectOptions setHeaders(Map<String, String> headers) {
107124
this.headers = headers;
108125
return this;
109126
}
127+
/**
128+
* Launch options to be passed to the browser being connected to. These options will be automatically
129+
* serialized and sent via the {@code x-playwright-launch-options} header.
130+
*
131+
* <p> This is particularly useful when connecting to a browser with specific requirements, such as using
132+
* a specific browser channel (e.g., "msedge" for Microsoft Edge).
133+
*
134+
* <p> Example:
135+
* <pre>{@code
136+
* BrowserType.LaunchOptions launchOptions = new BrowserType.LaunchOptions()
137+
* .setChannel("msedge");
138+
* BrowserType.ConnectOptions connectOptions = new BrowserType.ConnectOptions()
139+
* .setLaunchOptions(launchOptions);
140+
* Browser browser = browserType.connect(wsEndpoint, connectOptions);
141+
* }</pre>
142+
*/
143+
public ConnectOptions setLaunchOptions(LaunchOptions launchOptions) {
144+
this.launchOptions = launchOptions;
145+
return this;
146+
}
110147
/**
111148
* Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
112149
* Defaults to 0.

playwright/src/main/java/com/microsoft/playwright/impl/BrowserTypeImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public Browser connect(String wsEndpoint, ConnectOptions options) {
7676
headers.addProperty("x-playwright-browser", name());
7777
}
7878

79+
if (options.launchOptions != null && !headers.has("x-playwright-launch-options")) {
80+
String launchOptionsJson = new Gson().toJsonTree(options.launchOptions).toString();
81+
headers.addProperty("x-playwright-launch-options", launchOptionsJson);
82+
}
83+
7984
Double timeout = options.timeout;
8085
if (timeout == null) {
8186
timeout = 0.0;

playwright/src/test/java/com/microsoft/playwright/TestBrowserTypeConnect.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,22 @@ void shouldSendExtraHeadersWithConnectRequest() throws Exception {
174174
}
175175
}
176176

177+
@Test
178+
void shouldSendLaunchOptionsViaConnectOptions() throws Exception {
179+
try (WebSocketServerImpl webSocketServer = WebSocketServerImpl.create()) {
180+
try {
181+
browserType.connect("ws://localhost:" + webSocketServer.getPort() + "/ws",
182+
new BrowserType.ConnectOptions().setLaunchOptions(
183+
new BrowserType.LaunchOptions().setChannel("msedge")));
184+
} catch (Exception e) {
185+
}
186+
assertNotNull(webSocketServer.lastClientHandshake);
187+
String headerValue = webSocketServer.lastClientHandshake.getFieldValue("x-playwright-launch-options");
188+
assertNotNull(headerValue);
189+
assertTrue(headerValue.contains("\"channel\":\"msedge\""));
190+
}
191+
}
192+
177193
@Test
178194
void disconnectedEventShouldBeEmittedWhenBrowserIsClosedOrServerIsClosed() throws InterruptedException {
179195
// Launch another server to not affect other tests.

0 commit comments

Comments
 (0)