diff --git a/java/src/org/openqa/selenium/bidi/BiDi.java b/java/src/org/openqa/selenium/bidi/BiDi.java index 1d99c075fe032..4639a56ca5e45 100644 --- a/java/src/org/openqa/selenium/bidi/BiDi.java +++ b/java/src/org/openqa/selenium/bidi/BiDi.java @@ -17,10 +17,10 @@ package org.openqa.selenium.bidi; -import com.google.common.collect.ImmutableMap; import java.io.Closeable; import java.time.Duration; import java.util.Collections; +import java.util.Map; import java.util.Set; import java.util.function.Consumer; import org.openqa.selenium.internal.Require; @@ -60,8 +60,7 @@ public void addListener(Event event, Consumer handler) { send( new Command<>( - "session.subscribe", - ImmutableMap.of("events", Collections.singletonList(event.getMethod())))); + "session.subscribe", Map.of("events", Collections.singletonList(event.getMethod())))); connection.addListener(event, handler); } @@ -74,7 +73,7 @@ void addListener(String browsingContextId, Event event, Consumer handl send( new Command<>( "session.subscribe", - ImmutableMap.of( + Map.of( "contexts", Collections.singletonList(browsingContextId), "events", @@ -91,7 +90,7 @@ void addListener(Set browsingContextIds, Event event, Consumer send( new Command<>( "session.subscribe", - ImmutableMap.of( + Map.of( "contexts", browsingContextIds, "events", @@ -109,7 +108,7 @@ public void clearListener(Event event) { send( new Command<>( "session.unsubscribe", - ImmutableMap.of("events", Collections.singletonList(event.getMethod())))); + Map.of("events", Collections.singletonList(event.getMethod())))); connection.clearListener(event); } diff --git a/java/src/org/openqa/selenium/bidi/Command.java b/java/src/org/openqa/selenium/bidi/Command.java index 2ecca3f651447..8a120a3369cf7 100644 --- a/java/src/org/openqa/selenium/bidi/Command.java +++ b/java/src/org/openqa/selenium/bidi/Command.java @@ -17,7 +17,6 @@ package org.openqa.selenium.bidi; -import com.google.common.collect.ImmutableMap; import java.lang.reflect.Type; import java.util.Map; import java.util.function.Function; @@ -49,7 +48,7 @@ public Command( Function mapper, boolean sendsResponse) { this.method = Require.nonNull("Method name", method); - this.params = ImmutableMap.copyOf(Require.nonNull("Command parameters", params)); + this.params = Map.copyOf(Require.nonNull("Command parameters", params)); this.mapper = Require.nonNull("Mapper for result", mapper); this.sendsResponse = sendsResponse; } diff --git a/java/src/org/openqa/selenium/bidi/Connection.java b/java/src/org/openqa/selenium/bidi/Connection.java index bc9f580824b87..51254fdf96eb6 100644 --- a/java/src/org/openqa/selenium/bidi/Connection.java +++ b/java/src/org/openqa/selenium/bidi/Connection.java @@ -23,7 +23,6 @@ import static org.openqa.selenium.remote.http.HttpMethod.GET; import com.google.common.collect.HashMultimap; -import com.google.common.collect.ImmutableMap; import com.google.common.collect.Multimap; import java.io.Closeable; import java.io.StringReader; @@ -136,14 +135,15 @@ public CompletableFuture send(Command command) { })); } - ImmutableMap.Builder serialized = ImmutableMap.builder(); - serialized.put("id", id); - serialized.put("method", command.getMethod()); - serialized.put("params", command.getParams()); + Map serialized = + Map.of( + "id", id, + "method", command.getMethod(), + "params", command.getParams()); StringBuilder json = new StringBuilder(); try (JsonOutput out = JSON.newOutput(json).writeClassName(false)) { - out.write(serialized.build()); + out.write(serialized); } LOG.log(getDebugLogLevel(), "-> {0}", json); socket.sendText(json); @@ -219,7 +219,7 @@ public void clearListeners() { // will throw errors. // Ideally, such errors should not prevent freeing up resources. if (!underlyingSocketClosed.get()) { - send(new Command<>("session.unsubscribe", ImmutableMap.of("events", events))); + send(new Command<>("session.unsubscribe", Map.of("events", events))); } eventCallbacks.clear(); diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java b/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java index d424d678b4530..2ef7538529394 100644 --- a/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java @@ -17,7 +17,6 @@ package org.openqa.selenium.bidi.browsingcontext; -import com.google.common.collect.ImmutableMap; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.HashMap; @@ -110,46 +109,42 @@ public String getId() { private String create(WindowType type) { return this.bidi.send( new Command<>( - "browsingContext.create", - ImmutableMap.of("type", type.toString()), - browsingContextIdMapper)); + "browsingContext.create", Map.of("type", type.toString()), browsingContextIdMapper)); } private String create(WindowType type, String referenceContext) { return this.bidi.send( new Command<>( "browsingContext.create", - ImmutableMap.of("type", type.toString(), "referenceContext", referenceContext), + Map.of("type", type.toString(), "referenceContext", referenceContext), browsingContextIdMapper)); } public NavigationResult navigate(String url) { return this.bidi.send( new Command<>( - "browsingContext.navigate", - ImmutableMap.of(CONTEXT, id, "url", url), - navigationInfoMapper)); + "browsingContext.navigate", Map.of(CONTEXT, id, "url", url), navigationInfoMapper)); } public NavigationResult navigate(String url, ReadinessState readinessState) { return this.bidi.send( new Command<>( "browsingContext.navigate", - ImmutableMap.of(CONTEXT, id, "url", url, "wait", readinessState.toString()), + Map.of(CONTEXT, id, "url", url, "wait", readinessState.toString()), navigationInfoMapper)); } public List getTree() { return this.bidi.send( new Command<>( - "browsingContext.getTree", ImmutableMap.of("root", id), browsingContextInfoListMapper)); + "browsingContext.getTree", Map.of("root", id), browsingContextInfoListMapper)); } public List getTree(int maxDepth) { return this.bidi.send( new Command<>( "browsingContext.getTree", - ImmutableMap.of( + Map.of( "root", id, "maxDepth", maxDepth), browsingContextInfoListMapper)); @@ -161,17 +156,14 @@ public List getTopLevelContexts() { } public NavigationResult reload() { - return this.bidi.send( - new Command<>(RELOAD, ImmutableMap.of(CONTEXT, id), navigationInfoMapper)); + return this.bidi.send(new Command<>(RELOAD, Map.of(CONTEXT, id), navigationInfoMapper)); } // Yet to be implemented by browser vendors private NavigationResult reload(boolean ignoreCache) { return this.bidi.send( new Command<>( - RELOAD, - ImmutableMap.of(CONTEXT, id, "ignoreCache", ignoreCache), - navigationInfoMapper)); + RELOAD, Map.of(CONTEXT, id, "ignoreCache", ignoreCache), navigationInfoMapper)); } // TODO: Handle timeouts in case of Readiness state "interactive" and "complete". @@ -179,9 +171,7 @@ private NavigationResult reload(boolean ignoreCache) { public NavigationResult reload(ReadinessState readinessState) { return this.bidi.send( new Command<>( - RELOAD, - ImmutableMap.of(CONTEXT, id, "wait", readinessState.toString()), - navigationInfoMapper)); + RELOAD, Map.of(CONTEXT, id, "wait", readinessState.toString()), navigationInfoMapper)); } // Yet to be implemented by browser vendors @@ -189,37 +179,33 @@ private NavigationResult reload(boolean ignoreCache, ReadinessState readinessSta return this.bidi.send( new Command<>( RELOAD, - ImmutableMap.of( - CONTEXT, id, "ignoreCache", ignoreCache, "wait", readinessState.toString()), + Map.of(CONTEXT, id, "ignoreCache", ignoreCache, "wait", readinessState.toString()), navigationInfoMapper)); } public void handleUserPrompt() { - this.bidi.send(new Command<>(HANDLE_USER_PROMPT, ImmutableMap.of(CONTEXT, id))); + this.bidi.send(new Command<>(HANDLE_USER_PROMPT, Map.of(CONTEXT, id))); } public void handleUserPrompt(boolean accept) { - this.bidi.send( - new Command<>(HANDLE_USER_PROMPT, ImmutableMap.of(CONTEXT, id, "accept", accept))); + this.bidi.send(new Command<>(HANDLE_USER_PROMPT, Map.of(CONTEXT, id, "accept", accept))); } public void handleUserPrompt(String userText) { - this.bidi.send( - new Command<>(HANDLE_USER_PROMPT, ImmutableMap.of(CONTEXT, id, "userText", userText))); + this.bidi.send(new Command<>(HANDLE_USER_PROMPT, Map.of(CONTEXT, id, "userText", userText))); } public void handleUserPrompt(boolean accept, String userText) { this.bidi.send( new Command<>( - HANDLE_USER_PROMPT, - ImmutableMap.of(CONTEXT, id, "accept", accept, "userText", userText))); + HANDLE_USER_PROMPT, Map.of(CONTEXT, id, "accept", accept, "userText", userText))); } public String captureScreenshot() { return this.bidi.send( new Command<>( "browsingContext.captureScreenshot", - ImmutableMap.of(CONTEXT, id), + Map.of(CONTEXT, id), jsonInput -> { Map result = jsonInput.read(Map.class); return (String) result.get("data"); @@ -230,11 +216,11 @@ public String captureBoxScreenshot(double x, double y, double width, double heig return this.bidi.send( new Command<>( "browsingContext.captureScreenshot", - ImmutableMap.of( + Map.of( CONTEXT, id, "clip", - ImmutableMap.of( + Map.of( "type", "viewport", "x", x, "y", y, @@ -250,15 +236,15 @@ public String captureElementScreenshot(String elementId) { return this.bidi.send( new Command<>( "browsingContext.captureScreenshot", - ImmutableMap.of( + Map.of( CONTEXT, id, "clip", - ImmutableMap.of( + Map.of( "type", "element", "element", - ImmutableMap.of("sharedId", elementId), + Map.of("sharedId", elementId), "scrollIntoView", false)), jsonInput -> { @@ -271,15 +257,15 @@ public String captureElementScreenshot(String elementId, boolean scrollIntoView) return this.bidi.send( new Command<>( "browsingContext.captureScreenshot", - ImmutableMap.of( + Map.of( CONTEXT, id, "clip", - ImmutableMap.of( + Map.of( "type", "element", "element", - ImmutableMap.of("sharedId", elementId), + Map.of("sharedId", elementId), "scrollIntoView", scrollIntoView)), jsonInput -> { @@ -323,6 +309,6 @@ public void close() { // This might need more clean up actions once the behavior is defined. // Specially when last tab or window is closed. // Refer: https://github.com/w3c/webdriver-bidi/issues/187 - this.bidi.send(new Command<>("browsingContext.close", ImmutableMap.of(CONTEXT, id))); + this.bidi.send(new Command<>("browsingContext.close", Map.of(CONTEXT, id))); } }