1
0
mirror of synced 2026-08-06 07:26:55 +00:00

fix: set apiName for method calls in metadata (#533)

This commit is contained in:
Yury Semikhatsky
2021-07-29 08:19:35 -07:00
committed by GitHub
parent 3631357a35
commit 3e7f8017e0
5 changed files with 39 additions and 20 deletions
@@ -69,9 +69,18 @@ class ChannelOwner extends LoggingSupport {
}
<T> T withWaitLogging(String apiName, Supplier<T> code) {
return super.withLogging(apiName, new WaitForEventLogger<>(this, apiName, code));
return withLogging(apiName, new WaitForEventLogger<>(this, apiName, code));
}
@Override
<T> T withLogging(String apiName, Supplier<T> code) {
String previousApiName = connection.setApiName(apiName);
try {
return super.withLogging(apiName, code);
} finally {
connection.setApiName(previousApiName);
}
}
WaitableResult<JsonElement> sendMessageAsync(String method, JsonObject params) {
return connection.sendMessageAsync(guid, method, params);
@@ -68,6 +68,7 @@ public class Connection {
private int lastId = 0;
private final Path srcDir;
private final Map<Integer, WaitableResult<JsonElement>> callbacks = new HashMap<>();
private String apiName;
private static final boolean isLogging;
static {
String debug = System.getenv("DEBUG");
@@ -94,6 +95,12 @@ public class Connection {
}
}
String setApiName(String name) {
String previous = apiName;
apiName = name;
return previous;
}
void close() throws IOException {
transport.close();
}
@@ -154,11 +161,14 @@ public class Connection {
message.addProperty("guid", guid);
message.addProperty("method", method);
message.add("params", params);
JsonObject metadata = new JsonObject();
if (srcDir != null) {
JsonObject metadata = new JsonObject();
metadata.add("stack", currentStackTrace());
message.add("metadata", metadata);
}
if (apiName != null) {
metadata.addProperty("apiName", apiName);
}
message.add("metadata", metadata);
String messageString = gson().toJson(message);
if (isLogging) {
logWithTimestamp("SEND ► " + messageString);
@@ -23,7 +23,7 @@ import com.microsoft.playwright.Page;
import java.io.InputStream;
import java.nio.file.Path;
class DownloadImpl extends LoggingSupport implements Download {
class DownloadImpl implements Download {
private final PageImpl page;
private final ArtifactImpl artifact;
private final JsonObject initializer;
@@ -46,22 +46,22 @@ class DownloadImpl extends LoggingSupport implements Download {
@Override
public void cancel() {
withLogging("Download.cancel", () -> artifact.cancel());
page.withLogging("Download.cancel", () -> artifact.cancel());
}
@Override
public InputStream createReadStream() {
return withLogging("Download.createReadStream", () -> artifact.createReadStream());
return page.withLogging("Download.createReadStream", () -> artifact.createReadStream());
}
@Override
public void delete() {
withLogging("Download.delete", () -> artifact.delete());
page.withLogging("Download.delete", () -> artifact.delete());
}
@Override
public String failure() {
return withLogging("Download.failure", () -> artifact.failure());
return page.withLogging("Download.failure", () -> artifact.failure());
}
@Override
@@ -71,11 +71,11 @@ class DownloadImpl extends LoggingSupport implements Download {
@Override
public Path path() {
return withLogging("Download.path", () -> artifact.pathAfterFinished());
return page.withLogging("Download.path", () -> artifact.pathAfterFinished());
}
@Override
public void saveAs(Path path) {
withLogging("Download.saveAs", () -> artifact.saveAs(path));
page.withLogging("Download.saveAs", () -> artifact.saveAs(path));
}
}
@@ -21,7 +21,7 @@ import com.microsoft.playwright.Keyboard;
import static com.microsoft.playwright.impl.Serialization.gson;
class KeyboardImpl extends LoggingSupport implements Keyboard {
class KeyboardImpl implements Keyboard {
private final ChannelOwner page;
KeyboardImpl(ChannelOwner page) {
@@ -30,7 +30,7 @@ class KeyboardImpl extends LoggingSupport implements Keyboard {
@Override
public void down(String key) {
withLogging("Keyboard.down", () -> {
page.withLogging("Keyboard.down", () -> {
JsonObject params = new JsonObject();
params.addProperty("key", key);
page.sendMessage("keyboardDown", params);
@@ -39,7 +39,7 @@ class KeyboardImpl extends LoggingSupport implements Keyboard {
@Override
public void insertText(String text) {
withLogging("Keyboard.insertText", () -> {
page.withLogging("Keyboard.insertText", () -> {
JsonObject params = new JsonObject();
params.addProperty("text", text);
page.sendMessage("keyboardInsertText", params);
@@ -48,7 +48,7 @@ class KeyboardImpl extends LoggingSupport implements Keyboard {
@Override
public void press(String key, PressOptions options) {
withLogging("Keyboard.press", () -> pressImpl(key, options));
page.withLogging("Keyboard.press", () -> pressImpl(key, options));
}
private void pressImpl(String key, PressOptions options) {
@@ -62,7 +62,7 @@ class KeyboardImpl extends LoggingSupport implements Keyboard {
@Override
public void type(String text, TypeOptions options) {
withLogging("Keyboard.type", () -> typeImpl(text, options));
page.withLogging("Keyboard.type", () -> typeImpl(text, options));
}
private void typeImpl(String text, TypeOptions options) {
@@ -76,7 +76,7 @@ class KeyboardImpl extends LoggingSupport implements Keyboard {
@Override
public void up(String key) {
withLogging("Keyboard.up", () -> {
page.withLogging("Keyboard.up", () -> {
JsonObject params = new JsonObject();
params.addProperty("key", key);
page.sendMessage("keyboardUp", params);
@@ -24,7 +24,7 @@ import java.nio.file.Paths;
import static java.util.Arrays.asList;
class VideoImpl extends LoggingSupport implements Video {
class VideoImpl implements Video {
private final PageImpl page;
private final WaitableResult<ArtifactImpl> waitableArtifact = new WaitableResult<>();
private final boolean isRemote;
@@ -47,7 +47,7 @@ class VideoImpl extends LoggingSupport implements Video {
@Override
public void delete() {
withLogging("Video.delete", () -> {
page.withLogging("Video.delete", () -> {
try {
waitForArtifact().delete();
} catch (PlaywrightException e) {
@@ -57,7 +57,7 @@ class VideoImpl extends LoggingSupport implements Video {
@Override
public Path path() {
return withLogging("Video.path", () -> {
return page.withLogging("Video.path", () -> {
if (isRemote) {
throw new PlaywrightException("Path is not available when using browserType.connect(). Use saveAs() to save a local copy.");
}
@@ -71,7 +71,7 @@ class VideoImpl extends LoggingSupport implements Video {
@Override
public void saveAs(Path path) {
withLogging("Video.saveAs", () -> {
page.withLogging("Video.saveAs", () -> {
try {
waitForArtifact().saveAs(path);
} catch (PlaywrightException e) {