mirror of
https://github.com/microsoft/playwright-java.git
synced 2025-09-08 21:01:00 +00:00
feat: Browser.isConnected() (#54)
This commit is contained in:
parent
dca206d28d
commit
775b117277
@ -17,12 +17,14 @@
|
||||
package com.microsoft.playwright.impl;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.microsoft.playwright.*;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static com.microsoft.playwright.impl.Utils.convertViaJson;
|
||||
import static com.microsoft.playwright.impl.Utils.isSafeCloseError;
|
||||
@ -30,6 +32,7 @@ import static com.microsoft.playwright.impl.Utils.isSafeCloseError;
|
||||
class BrowserImpl extends ChannelOwner implements Browser {
|
||||
final Set<BrowserContext> contexts = new HashSet<>();
|
||||
private final ListenerCollection<EventType> listeners = new ListenerCollection<>();
|
||||
private boolean isConnected = true;
|
||||
|
||||
BrowserImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
|
||||
super(parent, type, guid, initializer);
|
||||
@ -63,19 +66,7 @@ class BrowserImpl extends ChannelOwner implements Browser {
|
||||
|
||||
@Override
|
||||
public boolean isConnected() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private static JsonArray toProtocol(Map<String, String> map) {
|
||||
JsonArray array = new JsonArray();
|
||||
for (Map.Entry<String, String> e : map.entrySet()) {
|
||||
JsonObject item = new JsonObject();
|
||||
item.addProperty("name", e.getKey());
|
||||
item.addProperty("value", e.getValue());
|
||||
array.add(item);
|
||||
}
|
||||
return array;
|
||||
return isConnected;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -86,7 +77,7 @@ class BrowserImpl extends ChannelOwner implements Browser {
|
||||
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
|
||||
if (options.extraHTTPHeaders != null) {
|
||||
params.remove("extraHTTPHeaders");
|
||||
params.add("extraHTTPHeaders", toProtocol(options.extraHTTPHeaders));
|
||||
params.add("extraHTTPHeaders", Serialization.toProtocol(options.extraHTTPHeaders));
|
||||
}
|
||||
JsonElement result = sendMessage("newContext", params);
|
||||
BrowserContextImpl context = connection.getExistingObject(result.getAsJsonObject().getAsJsonObject("context").get("guid").getAsString());
|
||||
@ -116,4 +107,11 @@ class BrowserImpl extends ChannelOwner implements Browser {
|
||||
return initializer.get("version").getAsString();
|
||||
}
|
||||
|
||||
@Override
|
||||
void handleEvent(String event, JsonObject parameters) {
|
||||
if ("close".equals(event)) {
|
||||
isConnected = false;
|
||||
listeners.notify(EventType.DISCONNECTED, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -87,14 +87,7 @@ class BrowserTypeImpl extends ChannelOwner implements BrowserType {
|
||||
JsonObject params = gson.toJsonTree(options).getAsJsonObject();
|
||||
if (options.extraHTTPHeaders != null) {
|
||||
params.remove("extraHTTPHeaders");
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
for (Map.Entry<String, String> e : options.extraHTTPHeaders.entrySet()) {
|
||||
JsonObject header = new JsonObject();
|
||||
header.addProperty("name", e.getKey());
|
||||
header.addProperty("value", e.getValue());
|
||||
jsonArray.add(header);
|
||||
}
|
||||
params.add("extraHTTPHeaders", jsonArray);
|
||||
params.add("extraHTTPHeaders", Serialization.toProtocol(options.extraHTTPHeaders));
|
||||
}
|
||||
params.addProperty("userDataDir", userDataDir);
|
||||
JsonObject json = sendMessage("launchPersistentContext", params).getAsJsonObject();
|
||||
|
@ -16,14 +16,11 @@
|
||||
|
||||
package com.microsoft.playwright.impl;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.microsoft.playwright.Request;
|
||||
import com.microsoft.playwright.Route;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.Map;
|
||||
|
||||
public class RouteImpl extends ChannelOwner implements Route {
|
||||
public RouteImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
|
||||
@ -47,14 +44,7 @@ public class RouteImpl extends ChannelOwner implements Route {
|
||||
params.addProperty("method", overrides.method);
|
||||
}
|
||||
if (overrides.headers != null) {
|
||||
JsonArray array = new JsonArray();
|
||||
for (Map.Entry<String, String> header : overrides.headers.entrySet()) {
|
||||
JsonObject item = new JsonObject();
|
||||
item.addProperty("name", header.getKey());
|
||||
item.addProperty("value", header.getValue());
|
||||
array.add(item);
|
||||
}
|
||||
params.add("headers", array);
|
||||
params.add("headers", Serialization.toProtocol(overrides.headers));
|
||||
}
|
||||
if (overrides.postData != null) {
|
||||
String base64 = Base64.getEncoder().encodeToString(overrides.postData);
|
||||
|
@ -211,6 +211,17 @@ class Serialization {
|
||||
return jsonElements;
|
||||
}
|
||||
|
||||
static JsonArray toProtocol(Map<String, String> map) {
|
||||
JsonArray array = new JsonArray();
|
||||
for (Map.Entry<String, String> e : map.entrySet()) {
|
||||
JsonObject item = new JsonObject();
|
||||
item.addProperty("name", e.getKey());
|
||||
item.addProperty("value", e.getValue());
|
||||
array.add(item);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
static List<String> parseStringList(JsonArray array) {
|
||||
List<String> result = new ArrayList<>();
|
||||
for (JsonElement e : array) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user