Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ba0c4d6
feat(protocol): typed sendMessage and connection-aware Gson adapters
yury-s Nov 1, 2026
9f392b5
chore(protocol): generate protocol channel types from the upstream spec
yury-s Nov 1, 2026
076d9a7
refactor(page): use generated channel types for keyboard, mouse, touc…
yury-s Nov 1, 2026
0b41b3a
refactor(protocol): copy options into protocol params with a Gson rou…
yury-s Nov 1, 2026
db2eaf3
refactor(protocol): give every channel owner a typed channel field
yury-s Nov 1, 2026
ea99a6a
refactor(impl): use generated channel types in ArtifactImpl, Stream, …
yury-s Nov 1, 2026
18f56bf
refactor(network): use generated channel types in RequestImpl, Respon…
yury-s Nov 1, 2026
4f51c41
refactor(network): use generated channel types in RouteImpl, WebSocke…
yury-s Nov 1, 2026
92c2c0c
refactor(page): use generated channel types for close, requests, cons…
yury-s Nov 1, 2026
2c6a335
refactor(page): use generated channel types for locator handlers, ini…
yury-s Nov 1, 2026
181e61c
refactor(page): use generated channel types for navigation, screensho…
yury-s Nov 1, 2026
8587e70
refactor(impl): use generated channel types in ClockImpl, WebStorageI…
yury-s Nov 1, 2026
db65e0c
refactor(protocol): resolve channel references in option copies throu…
yury-s Nov 1, 2026
9e97f83
refactor(frame): use generated channel types for frame commands
yury-s Nov 1, 2026
446b220
refactor(locator): use generated channel types in LocatorImpl
yury-s Nov 1, 2026
a18bba0
refactor(handles): use generated channel types in JSHandleImpl and El…
yury-s Nov 1, 2026
db8639f
refactor(utils): use generated channel types in createTempFiles
yury-s Nov 1, 2026
fdbcfdd
refactor(tracing): use generated channel types in TracingImpl, LocalU…
yury-s Nov 1, 2026
8a5a1ce
refactor(fetch): use generated channel types in APIRequestContextImpl…
yury-s Nov 1, 2026
f1f0f5d
refactor(fetch): keep raw storageState reply to preserve number forma…
yury-s Nov 1, 2026
8f642e9
refactor(browser): use generated channel types in BrowserContextImpl,…
yury-s Nov 1, 2026
92c716e
refactor(browser): keep raw storageState reply and connect options fi…
yury-s Nov 1, 2026
4ce496f
refactor(channel): typed sendMessageAsync and sendMessageNoReply
yury-s Nov 1, 2026
2190898
refactor(upload): fill typed upload params and return typed temp file…
yury-s Nov 1, 2026
83b2a2a
refactor(client): type the inputs of APIResponseImpl and ConsoleMessa…
yury-s Nov 1, 2026
d374c3d
refactor(selectors): keep typed selector engines
yury-s Nov 1, 2026
877c4f0
refactor(client): drop dead JsonObject protocol helpers
yury-s Nov 1, 2026
fd8d862
chore(protocol): regenerate channel types for driver 1.65.0
yury-s Nov 17, 2026
445b4f4
refactor(protocol): send supportsLoneSurrogates like the Node.js client
yury-s Nov 17, 2026
be04f56
refactor(protocol): move the generated channel types into impl.protocol
yury-s Nov 17, 2026
d61b8c5
chore(protocol): exclude impl.protocol from javadoc
yury-s Nov 17, 2026
1dedddc
fix(javadoc): exclude the subpackages of impl from the javadoc jar
yury-s Nov 17, 2026
64d9d9a
fix(assertions): resolve handles in the expected value of hasJSProperty
yury-s Nov 17, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion playwright/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
makes the plugin pass "patch-module com.microsoft.playwright=null" and the javadoc jar ends up empty. -->
<sourcepath>${project.build.sourceDirectory}</sourcepath>
<subpackages>com.microsoft.playwright</subpackages>
<excludePackageNames>com.microsoft.playwright.impl</excludePackageNames>
<excludePackageNames>com.microsoft.playwright.impl:com.microsoft.playwright.impl.*</excludePackageNames>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package com.microsoft.playwright.impl;

import com.microsoft.playwright.impl.protocol.*;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import com.microsoft.playwright.APIRequestContext;
import com.microsoft.playwright.APIResponse;
import com.microsoft.playwright.PlaywrightException;
Expand All @@ -37,14 +39,17 @@

import static com.microsoft.playwright.impl.Serialization.*;
import static com.microsoft.playwright.impl.Utils.toFilePayload;
import static com.microsoft.playwright.impl.Utils.toProtocol;
import static java.util.Arrays.asList;

class APIRequestContextImpl extends ChannelOwner implements APIRequestContext {
public class APIRequestContextImpl extends ChannelOwner implements APIRequestContext {
private final TracingImpl tracing;
private String disposeReason;

protected TimeoutSettings timeoutSettings = new TimeoutSettings();

final APIRequestContextChannel channel = new APIRequestContextChannel(this);

APIRequestContextImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
this.tracing = connection.getExistingObject(initializer.getAsJsonObject("tracing").get("guid").getAsString());
Expand All @@ -62,29 +67,31 @@ public List<Cookie> cookies(String url) {

@Override
public List<Cookie> cookies(List<String> urls) {
JsonObject params = new JsonObject();
APIRequestContextCookiesParams params = new APIRequestContextCookiesParams();
if (urls == null) {
urls = new ArrayList<>();
}
params.add("urls", gson().toJsonTree(urls));
JsonObject json = sendMessage("cookies", params, NO_TIMEOUT).getAsJsonObject();
Cookie[] cookies = gson().fromJson(json.getAsJsonArray("cookies"), Cookie[].class);
params.urls = urls;
APIRequestContextCookiesResult result = channel.cookies(params, NO_TIMEOUT);
Cookie[] cookies = gson().fromJson(gson().toJsonTree(result.cookies), Cookie[].class);
return asList(cookies);
}

@Override
public void addCookies(List<Cookie> cookies) {
JsonObject params = new JsonObject();
params.add("cookies", gson().toJsonTree(cookies));
sendMessage("addCookies", params, NO_TIMEOUT);
APIRequestContextAddCookiesParams params = new APIRequestContextAddCookiesParams();
params.cookies = gson().fromJson(gson().toJsonTree(cookies), new TypeToken<List<SetNetworkCookie>>() {}.getType());
channel.addCookies(params, NO_TIMEOUT);
}

@Override
public void clearCookies(ClearCookiesOptions options) {
if (options == null) {
options = new ClearCookiesOptions();
}
sendMessage("clearCookies", toClearCookiesParams(options.name, options.domain, options.path), NO_TIMEOUT);
APIRequestContextClearCookiesParams params = gson().fromJson(
toClearCookiesParams(options.name, options.domain, options.path), APIRequestContextClearCookiesParams.class);
channel.clearCookies(params, NO_TIMEOUT);
}

@Override
Expand All @@ -98,8 +105,7 @@ public void dispose(DisposeOptions options) {
options = new DisposeOptions();
}
disposeReason = options.reason;
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
sendMessage("dispose", params, NO_TIMEOUT);
channel.dispose(toProtocol(options, APIRequestContextDisposeParams.class), NO_TIMEOUT);
tracing.resetStackCounter();
}

Expand Down Expand Up @@ -134,20 +140,18 @@ private APIResponse fetchImpl(String url, RequestOptionsImpl options) {
options = new RequestOptionsImpl();
}
options.timeout = timeoutSettings.timeout(options.timeout);
JsonObject params = new JsonObject();
params.addProperty("url", url);
APIRequestContextFetchParams params = new APIRequestContextFetchParams();
params.url = url;
if (options.params != null) {
Map<String, String> queryParams = new LinkedHashMap<>();
for (Map.Entry<String, ?> e : options.params.entrySet()) {
queryParams.put(e.getKey(), "" + e.getValue());
}
params.add("params", toNameValueArray(queryParams.entrySet()));
}
if (options.method != null) {
params.addProperty("method", options.method);
params.params = toNameValues(toNameValueArray(queryParams.entrySet()));
}
params.method = options.method;
if (options.headers != null) {
params.add("headers", toProtocol(options.headers));
params.headers = toNameValues(Serialization.toProtocol(options.headers));
}

if (options.data != null) {
Expand All @@ -161,38 +165,45 @@ private APIResponse fetchImpl(String url, RequestOptionsImpl options) {
}
}
if (bytes == null) {
params.addProperty("jsonData", jsonDataSerializer.toJson(options.data));
params.jsonData = jsonDataSerializer.toJson(options.data);
} else {
String base64 = Base64.getEncoder().encodeToString(bytes);
params.addProperty("postData", base64);
params.postData = bytes;
}
}
if (options.form != null) {
params.add("formData", toNameValueArray(options.form.fields));
params.formData = toNameValues(toNameValueArray(options.form.fields));
}
if (options.multipart != null) {
params.add("multipartData", serializeMultipartData(options.multipart.fields));
}
if (options.failOnStatusCode != null) {
params.addProperty("failOnStatusCode", options.failOnStatusCode);
}
if (options.ignoreHTTPSErrors != null) {
params.addProperty("ignoreHTTPSErrors", options.ignoreHTTPSErrors);
params.multipartData = serializeMultipartData(options.multipart.fields);
}
params.failOnStatusCode = options.failOnStatusCode;
params.ignoreHTTPSErrors = options.ignoreHTTPSErrors;
if (options.maxRedirects != null) {
if (options.maxRedirects < 0) {
throw new PlaywrightException("'maxRedirects' should be greater than or equal to '0'");
}
params.addProperty("maxRedirects", options.maxRedirects);
params.maxRedirects = options.maxRedirects;
}
if (options.maxRetries != null) {
if (options.maxRetries < 0) {
throw new PlaywrightException("'maxRetries' must be greater than or equal to '0'");
}
params.addProperty("maxRetries", options.maxRetries);
params.maxRetries = options.maxRetries;
}
JsonObject json = sendMessage("fetch", params, timeoutSettings.timeout(options.timeout)).getAsJsonObject();
return new APIResponseImpl(this, json.getAsJsonObject("response"));
APIRequestContextFetchResult result = channel.fetch(params, timeoutSettings.timeout(options.timeout));
return new APIResponseImpl(this, result.response);
}

private static List<NameValue> toNameValues(JsonArray array) {
List<NameValue> result = new ArrayList<>();
for (JsonElement element : array) {
JsonObject item = element.getAsJsonObject();
NameValue nv = new NameValue();
nv.name = item.get("name").getAsString();
nv.value = item.get("value").getAsString();
result.add(nv);
}
return result;
}

private static boolean isJsonContentType(Map<String, String> headers) {
Expand All @@ -207,8 +218,8 @@ private static boolean isJsonContentType(Map<String, String> headers) {
return false;
}

private static JsonArray serializeMultipartData(List<? extends Map.Entry<String, Object>> data) {
JsonArray result = new JsonArray();
private static List<FormField> serializeMultipartData(List<? extends Map.Entry<String, Object>> data) {
List<FormField> result = new ArrayList<>();
for (Map.Entry<String, ?> e : data) {
FilePayload filePayload = null;
if (e.getValue() instanceof FilePayload) {
Expand All @@ -218,12 +229,15 @@ private static JsonArray serializeMultipartData(List<? extends Map.Entry<String,
} else if (e.getValue() instanceof File) {
filePayload = toFilePayload(((File) e.getValue()).toPath());
}
JsonObject item = new JsonObject();
item.addProperty("name", e.getKey());
FormField item = new FormField();
item.name = e.getKey();
if (filePayload == null) {
item.addProperty("value", "" + e.getValue());
item.value = "" + e.getValue();
} else {
item.add("file", toProtocol(filePayload));
item.file = new FormField.FormFieldFile();
item.file.name = filePayload.name;
item.file.mimeType = filePayload.mimeType;
item.file.buffer = filePayload.buffer;
}
result.add(item);
}
Expand Down Expand Up @@ -260,10 +274,10 @@ public String storageState(StorageStateOptions options) {
if (options == null) {
options = new StorageStateOptions();
}
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
params.remove("path");
JsonElement json = sendMessage("storageState", params, NO_TIMEOUT);
String storageState = json.toString();
APIRequestContextStorageStateParams params = toProtocol(options, APIRequestContextStorageStateParams.class);
// Keep the raw reply: re-serializing the typed result would turn "expires":-1 into -1.0 and change the string
// that is compared with BrowserContext.storageState().
String storageState = sendMessage("storageState", connection.protocolGson().toJsonTree(params).getAsJsonObject(), NO_TIMEOUT).toString();
if (options.path != null) {
Utils.writeToFile(storageState.getBytes(StandardCharsets.UTF_8), options.path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,23 @@

package com.microsoft.playwright.impl;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.microsoft.playwright.impl.protocol.*;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.microsoft.playwright.APIRequest;
import com.microsoft.playwright.PlaywrightException;
import com.microsoft.playwright.options.ClientCertificate;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

import static com.microsoft.playwright.impl.ChannelOwner.NO_TIMEOUT;
import static com.microsoft.playwright.impl.Serialization.gson;
import static com.microsoft.playwright.impl.Utils.addToProtocol;
import static com.microsoft.playwright.impl.Utils.toProtocol;

class APIRequestImpl implements APIRequest {
private final PlaywrightImpl playwright;
Expand All @@ -54,24 +57,52 @@ public APIRequestContextImpl newContext(NewContextOptions options) {
throw new PlaywrightException("Failed to read storage state from file", e);
}
}
JsonObject storageState = null;
PlaywrightNewRequestParams.PlaywrightNewRequestParamsStorageState storageState = null;
if (options.storageState != null) {
storageState = new Gson().fromJson(options.storageState, JsonObject.class);
storageState = gson().fromJson(options.storageState, PlaywrightNewRequestParams.PlaywrightNewRequestParamsStorageState.class);
options.storageState = null;
}
List<ClientCertificate> clientCertificateList = options.clientCertificates;
options.clientCertificates = null;
// The protocol always takes a list (mirrors toHttpCredentialsProtocol() in the JS client).
Object httpCredentials = options.httpCredentials;
options.httpCredentials = null;
Double timeout = options.timeout;
// Timeout is handled on the client.
options.timeout = null;
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
PlaywrightNewRequestParams params = toProtocol(options, PlaywrightNewRequestParams.class);
if (storageState != null) {
params.add("storageState", storageState);
params.storageState = storageState;
}
addToProtocol(params, clientCertificateList);
Utils.addHttpCredentialsToProtocol(params);
JsonObject result = playwright.sendMessage("newRequest", params, NO_TIMEOUT).getAsJsonObject();
APIRequestContextImpl context = playwright.connection.getExistingObject(result.getAsJsonObject("request").get("guid").getAsString());
if (clientCertificateList != null) {
params.clientCertificates = new ArrayList<>();
for (ClientCertificate cert : clientCertificateList) {
PlaywrightNewRequestParams.PlaywrightNewRequestParamsClientCertificates c = new PlaywrightNewRequestParams.PlaywrightNewRequestParamsClientCertificates();
c.origin = cert.origin;
try {
c.cert = cert.certPath != null ? Files.readAllBytes(cert.certPath) : cert.cert;
c.key = cert.keyPath != null ? Files.readAllBytes(cert.keyPath) : cert.key;
c.pfx = cert.pfxPath != null ? Files.readAllBytes(cert.pfxPath) : cert.pfx;
} catch (IOException e) {
throw new PlaywrightException("Failed to read from file", e);
}
c.passphrase = cert.passphrase;
params.clientCertificates.add(c);
}
}
if (httpCredentials != null) {
JsonElement credentials = gson().toJsonTree(httpCredentials);
JsonArray list = new JsonArray();
if (credentials.isJsonArray()) {
list.addAll(credentials.getAsJsonArray());
} else {
list.add(credentials);
}
if (list.size() > 0) {
params.httpCredentials = gson().fromJson(list, new TypeToken<List<HttpCredentials>>() {}.getType());
}
}
APIRequestContextImpl context = playwright.channel.newRequest(params, NO_TIMEOUT).request;
context.timeoutSettings.setDefaultTimeout(timeout);
return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.microsoft.playwright.impl;

import com.microsoft.playwright.impl.protocol.*;
import com.microsoft.playwright.APIResponse;
import com.microsoft.playwright.assertions.APIResponseAssertions;
import org.opentest4j.AssertionFailedError;
Expand Down
Loading
Loading