1
0
mirror of synced 2026-08-05 23:16:54 +00:00

feat: fire remaining page events (#15)

This commit is contained in:
Yury Semikhatsky
2020-10-13 16:45:51 -07:00
committed by GitHub
parent bad78223c6
commit d164f4a4e8
9 changed files with 166 additions and 4 deletions
@@ -629,6 +629,12 @@ class Interface extends TypeDefinition {
output.add(offset + " Object call(Source source, Object... args);");
output.add(offset + "}");
output.add("");
output.add(offset + "interface Error {");
output.add(offset + " String message();");
output.add(offset + " String name();");
output.add(offset + " String stack();");
output.add(offset + "}");
output.add("");
break;
}
case "BrowserContext": {
@@ -54,6 +54,12 @@ public interface Page {
Object call(Source source, Object... args);
}
interface Error {
String message();
String name();
String stack();
}
enum EventType {
CLOSE,
@@ -212,6 +212,9 @@ public class Connection {
case "Frame":
result = new FrameImpl(parent, type, guid, initializer);
break;
case "FileChooser":
result = new FileChooserImpl(parent, type, guid, initializer);
break;
case "JSHandle":
result = new JSHandleImpl(parent, type, guid, initializer);
break;
@@ -18,23 +18,50 @@ package com.microsoft.playwright.impl;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.microsoft.playwright.Download;
public class DownloadImpl extends ChannelOwner {
public class DownloadImpl extends ChannelOwner implements Download {
public DownloadImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
}
@Override
public String url() {
return initializer.get("url").getAsString();
}
@Override
public String suggestedFilename() {
return initializer.get("suggestedFilename").getAsString();
}
@Override
public Readable createReadStream() {
return null;
}
@Override
public void delete() {
sendMessage("delete", new JsonObject());
}
@Override
public String failure() {
JsonObject result = sendMessage("failure", new JsonObject()).getAsJsonObject();
if (result.has("error")) {
return result.get("error").getAsString();
}
return null;
}
@Override
public String path() {
JsonObject params = new JsonObject();
JsonElement result = sendMessage("path", params);
return result.getAsJsonObject().get("path").getAsString();
}
@Override
public void saveAs(String path) {
}
}
@@ -0,0 +1,48 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.playwright.impl;
import com.google.gson.JsonObject;
import com.microsoft.playwright.ElementHandle;
import com.microsoft.playwright.FileChooser;
import com.microsoft.playwright.Page;
class FileChooserImpl extends ChannelOwner implements FileChooser {
FileChooserImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
}
@Override
public ElementHandle element() {
return null;
}
@Override
public boolean isMultiple() {
return false;
}
@Override
public Page page() {
return null;
}
@Override
public void setFiles(String files, SetFilesOptions options) {
}
}
@@ -556,6 +556,10 @@ public class FrameImpl extends ChannelOwner implements Frame {
} else if ("navigated".equals(event)) {
url = params.get("url").getAsString();
name = params.get("name").getAsString();
// liste
if (!params.has("error") && page != null) {
page.frameNavigated(this);
}
}
for (WaitForNavigationHelper h : new ArrayList<>(eventHelpers)) {
h.handleEvent(event, params);
@@ -76,10 +76,45 @@ public class PageImpl extends ChannelOwner implements Page {
String guid = params.getAsJsonObject("page").get("guid").getAsString();
PageImpl popup = connection.getExistingObject(guid);
listeners.notify(EventType.POPUP, popup);
} else if ("worker".equals(event)) {
String guid = params.getAsJsonObject("worker").get("guid").getAsString();
Worker worker = connection.getExistingObject(guid);
listeners.notify(EventType.WORKER, worker);
} else if ("console".equals(event)) {
String guid = params.getAsJsonObject("message").get("guid").getAsString();
ConsoleMessageImpl message = connection.getExistingObject(guid);
listeners.notify(EventType.CONSOLE, message);
} else if ("download".equals(event)) {
String guid = params.getAsJsonObject("download").get("guid").getAsString();
DownloadImpl download = connection.getExistingObject(guid);
listeners.notify(EventType.DOWNLOAD, download);
} else if ("fileChooser".equals(event)) {
String guid = params.getAsJsonObject("element").get("guid").getAsString();
FileChooser fileChooser = connection.getExistingObject(guid);
listeners.notify(EventType.FILECHOOSER, fileChooser);
} else if ("load".equals(event)) {
listeners.notify(EventType.LOAD, null);
} else if ("domcontentloaded".equals(event)) {
listeners.notify(EventType.DOMCONTENTLOADED, null);
} else if ("request".equals(event)) {
String guid = params.getAsJsonObject("request").get("guid").getAsString();
Request request = connection.getExistingObject(guid);
listeners.notify(EventType.REQUEST, request);
} else if ("requestFailed".equals(event)) {
String guid = params.getAsJsonObject("request").get("guid").getAsString();
RequestImpl request = connection.getExistingObject(guid);
if (params.has("failureText")) {
request.failureText = new Gson().fromJson(params, Request.RequestFailure.class);
}
listeners.notify(EventType.REQUESTFAILED, request);
} else if ("requestFinished".equals(event)) {
String guid = params.getAsJsonObject("request").get("guid").getAsString();
Request request = connection.getExistingObject(guid);
listeners.notify(EventType.REQUESTFINISHED, request);
} else if ("response".equals(event)) {
String guid = params.getAsJsonObject("response").get("guid").getAsString();
Response response = connection.getExistingObject(guid);
listeners.notify(EventType.RESPONSE, response);
} else if ("frameAttached".equals(event)) {
String guid = params.getAsJsonObject("frame").get("guid").getAsString();
FrameImpl frame = connection.getExistingObject(guid);
@@ -108,10 +143,15 @@ public class PageImpl extends ChannelOwner implements Page {
if (!handled) {
route.continue_();
}
} else if ("pageError".equals(event)) {
SerializedError error = new Gson().fromJson(params.getAsJsonObject("error"), SerializedError.class);
listeners.notify(EventType.PAGEERROR, new ErrorImpl(error));
} else if ("crash".equals(event)) {
listeners.notify(EventType.CRASH, null);
} else if ("close".equals(event)) {
isClosed = true;
browserContext.pages.remove(this);
listeners.notify(EventType.CLOSE, this);
listeners.notify(EventType.CLOSE, null);
}
for (WaitEventHelper h : new ArrayList<>(eventHelpers)) {
h.handleEvent(event, params);
@@ -524,6 +564,33 @@ public class PageImpl extends ChannelOwner implements Page {
return mainFrame.waitForNavigation(convertViaJson(options, Frame.WaitForNavigationOptions.class));
}
void frameNavigated(FrameImpl frame) {
listeners.notify(EventType.FRAMENAVIGATED, frame);
}
private static class ErrorImpl implements Error {
private final SerializedError error;
ErrorImpl(SerializedError error) {
this.error = error;
}
@Override
public String message() {
return error.error.message;
}
@Override
public String name() {
return error.error.name;
}
@Override
public String stack() {
return error.error.stack;
}
}
private class WaitEventHelper<R> implements Deferred<R> {
private final CompletableFuture<R> result = new CompletableFuture<>();
private final String event;
@@ -30,6 +30,8 @@ public class RequestImpl extends ChannelOwner implements Request {
private RequestImpl redirectedFrom;
private RequestImpl redirectedTo;
private final Map<String, String> headers = new HashMap<>();
RequestFailure failureText;
RequestImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
@@ -20,9 +20,8 @@ import org.junit.jupiter.api.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestEvalOnSelectorAll {
private static Playwright playwright;