From 4d912193e7100ef4e28af378d632a9608bd1f591 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 6 Sep 2023 13:33:16 -0700 Subject: [PATCH] chore: support js Map, Set in protocol results (#1376) --- .../java/com/microsoft/playwright/impl/Protocol.java | 4 ++++ .../com/microsoft/playwright/impl/Serialization.java | 10 ++++++++++ .../com/microsoft/playwright/TestPageEvaluate.java | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/Protocol.java b/playwright/src/main/java/com/microsoft/playwright/impl/Protocol.java index 3c4170ec..10589bc9 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/Protocol.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/Protocol.java @@ -47,6 +47,10 @@ class SerializedValue{ Number h; Integer id; Integer ref; + // JS representation of Map: [[key1, value1], [key2, value2], ...]. + SerializedValue m; + // JS representation of Set: [item1, item2, ...]. + SerializedValue se; } class SerializedArgument{ diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/Serialization.java b/playwright/src/main/java/com/microsoft/playwright/impl/Serialization.java index b5d9f256..8b2588bc 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/Serialization.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/Serialization.java @@ -280,6 +280,16 @@ class Serialization { } return (T) map; } + if (value.m != null) { + Map map = new LinkedHashMap<>(); + idToValue.put(value.id, map); + return (T) map; + } + if (value.se != null) { + Map map = new LinkedHashMap<>(); + idToValue.put(value.id, map); + return (T) map; + } throw new PlaywrightException("Unexpected result: " + gson().toJson(value)); } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageEvaluate.java b/playwright/src/test/java/com/microsoft/playwright/TestPageEvaluate.java index 10da53c7..f426a025 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageEvaluate.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageEvaluate.java @@ -645,4 +645,14 @@ public class TestPageEvaluate extends TestBase { assertTrue(object instanceof Date); assertEquals(Date.from(instant), object); } + + @Test + void shouldTransferMaps() { + assertEquals(mapOf(), page.evaluate("() => new Map([[1, { test: 42n }]])")); + } + + @Test + void shouldTransferSets() { + assertEquals(mapOf(), page.evaluate("() => new Set([1, { test: 42n }])")); + } }