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;
|
package com.microsoft.playwright.impl;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonArray;
|
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.microsoft.playwright.*;
|
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.convertViaJson;
|
||||||
import static com.microsoft.playwright.impl.Utils.isSafeCloseError;
|
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 {
|
class BrowserImpl extends ChannelOwner implements Browser {
|
||||||
final Set<BrowserContext> contexts = new HashSet<>();
|
final Set<BrowserContext> contexts = new HashSet<>();
|
||||||
private final ListenerCollection<EventType> listeners = new ListenerCollection<>();
|
private final ListenerCollection<EventType> listeners = new ListenerCollection<>();
|
||||||
|
private boolean isConnected = true;
|
||||||
|
|
||||||
BrowserImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
|
BrowserImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
|
||||||
super(parent, type, guid, initializer);
|
super(parent, type, guid, initializer);
|
||||||
@ -63,19 +66,7 @@ class BrowserImpl extends ChannelOwner implements Browser {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isConnected() {
|
public boolean isConnected() {
|
||||||
return false;
|
return isConnected;
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -86,7 +77,7 @@ class BrowserImpl extends ChannelOwner implements Browser {
|
|||||||
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
|
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
|
||||||
if (options.extraHTTPHeaders != null) {
|
if (options.extraHTTPHeaders != null) {
|
||||||
params.remove("extraHTTPHeaders");
|
params.remove("extraHTTPHeaders");
|
||||||
params.add("extraHTTPHeaders", toProtocol(options.extraHTTPHeaders));
|
params.add("extraHTTPHeaders", Serialization.toProtocol(options.extraHTTPHeaders));
|
||||||
}
|
}
|
||||||
JsonElement result = sendMessage("newContext", params);
|
JsonElement result = sendMessage("newContext", params);
|
||||||
BrowserContextImpl context = connection.getExistingObject(result.getAsJsonObject().getAsJsonObject("context").get("guid").getAsString());
|
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();
|
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();
|
JsonObject params = gson.toJsonTree(options).getAsJsonObject();
|
||||||
if (options.extraHTTPHeaders != null) {
|
if (options.extraHTTPHeaders != null) {
|
||||||
params.remove("extraHTTPHeaders");
|
params.remove("extraHTTPHeaders");
|
||||||
JsonArray jsonArray = new JsonArray();
|
params.add("extraHTTPHeaders", Serialization.toProtocol(options.extraHTTPHeaders));
|
||||||
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.addProperty("userDataDir", userDataDir);
|
params.addProperty("userDataDir", userDataDir);
|
||||||
JsonObject json = sendMessage("launchPersistentContext", params).getAsJsonObject();
|
JsonObject json = sendMessage("launchPersistentContext", params).getAsJsonObject();
|
||||||
|
@ -16,14 +16,11 @@
|
|||||||
|
|
||||||
package com.microsoft.playwright.impl;
|
package com.microsoft.playwright.impl;
|
||||||
|
|
||||||
import com.google.gson.JsonArray;
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.microsoft.playwright.Request;
|
import com.microsoft.playwright.Request;
|
||||||
import com.microsoft.playwright.Route;
|
import com.microsoft.playwright.Route;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class RouteImpl extends ChannelOwner implements Route {
|
public class RouteImpl extends ChannelOwner implements Route {
|
||||||
public RouteImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
|
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);
|
params.addProperty("method", overrides.method);
|
||||||
}
|
}
|
||||||
if (overrides.headers != null) {
|
if (overrides.headers != null) {
|
||||||
JsonArray array = new JsonArray();
|
params.add("headers", Serialization.toProtocol(overrides.headers));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
if (overrides.postData != null) {
|
if (overrides.postData != null) {
|
||||||
String base64 = Base64.getEncoder().encodeToString(overrides.postData);
|
String base64 = Base64.getEncoder().encodeToString(overrides.postData);
|
||||||
|
@ -211,6 +211,17 @@ class Serialization {
|
|||||||
return jsonElements;
|
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) {
|
static List<String> parseStringList(JsonArray array) {
|
||||||
List<String> result = new ArrayList<>();
|
List<String> result = new ArrayList<>();
|
||||||
for (JsonElement e : array) {
|
for (JsonElement e : array) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user