Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions playwright/src/main/java/com/microsoft/playwright/BrowserType.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ class ConnectOptions {
* Additional HTTP headers to be sent with web socket connect request. Optional.
*/
public Map<String, String> headers;
/**
* Launch options to be passed to the browser being connected to. These options will be automatically
* serialized and sent via the {@code x-playwright-launch-options} header.
*
* <p> This is particularly useful when connecting to a browser with specific requirements, such as using
* a specific browser channel (e.g., "msedge" for Microsoft Edge).
*
* <p> Example:
* <pre>{@code
* BrowserType.LaunchOptions launchOptions = new BrowserType.LaunchOptions()
* .setChannel("msedge");
* BrowserType.ConnectOptions connectOptions = new BrowserType.ConnectOptions()
* .setLaunchOptions(launchOptions);
* Browser browser = browserType.connect(wsEndpoint, connectOptions);
* }</pre>
*/
public LaunchOptions launchOptions;
/**
* Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
* Defaults to 0.
Expand Down Expand Up @@ -107,6 +124,26 @@ public ConnectOptions setHeaders(Map<String, String> headers) {
this.headers = headers;
return this;
}
/**
* Launch options to be passed to the browser being connected to. These options will be automatically
* serialized and sent via the {@code x-playwright-launch-options} header.
*
* <p> This is particularly useful when connecting to a browser with specific requirements, such as using
* a specific browser channel (e.g., "msedge" for Microsoft Edge).
*
* <p> Example:
* <pre>{@code
* BrowserType.LaunchOptions launchOptions = new BrowserType.LaunchOptions()
* .setChannel("msedge");
* BrowserType.ConnectOptions connectOptions = new BrowserType.ConnectOptions()
* .setLaunchOptions(launchOptions);
* Browser browser = browserType.connect(wsEndpoint, connectOptions);
* }</pre>
*/
public ConnectOptions setLaunchOptions(LaunchOptions launchOptions) {
this.launchOptions = launchOptions;
return this;
}
/**
* Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
* Defaults to 0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public Browser connect(String wsEndpoint, ConnectOptions options) {
headers.addProperty("x-playwright-browser", name());
}

if (options.launchOptions != null && !headers.has("x-playwright-launch-options")) {
String launchOptionsJson = new Gson().toJsonTree(options.launchOptions).toString();
headers.addProperty("x-playwright-launch-options", launchOptionsJson);
}

Double timeout = options.timeout;
if (timeout == null) {
timeout = 0.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,22 @@ void shouldSendExtraHeadersWithConnectRequest() throws Exception {
}
}

@Test
void shouldSendLaunchOptionsViaConnectOptions() throws Exception {
try (WebSocketServerImpl webSocketServer = WebSocketServerImpl.create()) {
try {
browserType.connect("ws://localhost:" + webSocketServer.getPort() + "/ws",
new BrowserType.ConnectOptions().setLaunchOptions(
new BrowserType.LaunchOptions().setChannel("msedge")));
} catch (Exception e) {
}
assertNotNull(webSocketServer.lastClientHandshake);
String headerValue = webSocketServer.lastClientHandshake.getFieldValue("x-playwright-launch-options");
assertNotNull(headerValue);
assertTrue(headerValue.contains("\"channel\":\"msedge\""));
}
}

@Test
void disconnectedEventShouldBeEmittedWhenBrowserIsClosedOrServerIsClosed() throws InterruptedException {
// Launch another server to not affect other tests.
Expand Down