diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java b/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java index 6491371d370de..51c9fae58e4fc 100644 --- a/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java @@ -42,31 +42,31 @@ public class BrowsingContext { private static final String HANDLE_USER_PROMPT = "browsingContext.handleUserPrompt"; protected static final Type LIST_OF_BROWSING_CONTEXT_INFO = - new TypeToken>() {}.getType(); + new TypeToken>() {}.getType(); private final Function browsingContextIdMapper = - jsonInput -> { - Map result = jsonInput.read(Map.class); - return result.getOrDefault(CONTEXT, "").toString(); - }; + jsonInput -> { + Map result = jsonInput.read(Map.class); + return result.getOrDefault(CONTEXT, "").toString(); + }; private final Function navigationInfoMapper = - jsonInput -> (NavigationResult) jsonInput.read(NavigationResult.class); + jsonInput -> (NavigationResult) jsonInput.read(NavigationResult.class); private final Function> browsingContextInfoListMapper = - jsonInput -> { - Map result = jsonInput.read(Map.class); - List contexts = (List) result.getOrDefault("contexts", new ArrayList<>()); + jsonInput -> { + Map result = jsonInput.read(Map.class); + List contexts = (List) result.getOrDefault("contexts", new ArrayList<>()); - if (contexts.isEmpty()) { - return new ArrayList<>(); - } + if (contexts.isEmpty()) { + return new ArrayList<>(); + } - Json json = new Json(); - String dtr = json.toJson(contexts); + Json json = new Json(); + String dtr = json.toJson(contexts); - return json.toType(dtr, LIST_OF_BROWSING_CONTEXT_INFO); - }; + return json.toType(dtr, LIST_OF_BROWSING_CONTEXT_INFO); + }; public BrowsingContext(WebDriver driver, String id) { Require.nonNull("WebDriver", driver); @@ -96,6 +96,9 @@ public BrowsingContext(WebDriver driver, WindowType type) { public BrowsingContext(WebDriver driver, WindowType type, String referenceContextId) { Require.nonNull("WebDriver", driver); Require.nonNull("Reference browsing context id", referenceContextId); + + Require.precondition(!referenceContextId.isEmpty(),"Reference Context id cannot be empty"); + if (!(driver instanceof HasBiDi)) { throw new IllegalArgumentException("WebDriver instance must support BiDi protocol"); } @@ -110,51 +113,51 @@ public String getId() { private String create(WindowType type) { return this.bidi.send( - new Command<>( - "browsingContext.create", Map.of("type", type.toString()), browsingContextIdMapper)); + new Command<>( + "browsingContext.create", Map.of("type", type.toString()), browsingContextIdMapper)); } private String create(WindowType type, String referenceContext) { return this.bidi.send( - new Command<>( - "browsingContext.create", - Map.of("type", type.toString(), "referenceContext", referenceContext), - browsingContextIdMapper)); + new Command<>( + "browsingContext.create", + Map.of("type", type.toString(), "referenceContext", referenceContext), + browsingContextIdMapper)); } public NavigationResult navigate(String url) { return this.bidi.send( - new Command<>( - "browsingContext.navigate", Map.of(CONTEXT, id, "url", url), navigationInfoMapper)); + new Command<>( + "browsingContext.navigate", Map.of(CONTEXT, id, "url", url), navigationInfoMapper)); } public NavigationResult navigate(String url, ReadinessState readinessState) { return this.bidi.send( - new Command<>( - "browsingContext.navigate", - Map.of(CONTEXT, id, "url", url, "wait", readinessState.toString()), - navigationInfoMapper)); + new Command<>( + "browsingContext.navigate", + Map.of(CONTEXT, id, "url", url, "wait", readinessState.toString()), + navigationInfoMapper)); } public List getTree() { return this.bidi.send( - new Command<>( - "browsingContext.getTree", Map.of("root", id), browsingContextInfoListMapper)); + new Command<>( + "browsingContext.getTree", Map.of("root", id), browsingContextInfoListMapper)); } public List getTree(int maxDepth) { return this.bidi.send( - new Command<>( - "browsingContext.getTree", - Map.of( - "root", id, - "maxDepth", maxDepth), - browsingContextInfoListMapper)); + new Command<>( + "browsingContext.getTree", + Map.of( + "root", id, + "maxDepth", maxDepth), + browsingContextInfoListMapper)); } public List getTopLevelContexts() { return this.bidi.send( - new Command<>("browsingContext.getTree", new HashMap<>(), browsingContextInfoListMapper)); + new Command<>("browsingContext.getTree", new HashMap<>(), browsingContextInfoListMapper)); } public NavigationResult reload() { @@ -164,25 +167,25 @@ public NavigationResult reload() { // Yet to be implemented by browser vendors private NavigationResult reload(boolean ignoreCache) { return this.bidi.send( - new Command<>( - RELOAD, Map.of(CONTEXT, id, "ignoreCache", ignoreCache), navigationInfoMapper)); + new Command<>( + RELOAD, Map.of(CONTEXT, id, "ignoreCache", ignoreCache), navigationInfoMapper)); } // TODO: Handle timeouts in case of Readiness state "interactive" and "complete". // Refer https://github.com/w3c/webdriver-bidi/issues/188 public NavigationResult reload(ReadinessState readinessState) { return this.bidi.send( - new Command<>( - RELOAD, Map.of(CONTEXT, id, "wait", readinessState.toString()), navigationInfoMapper)); + new Command<>( + RELOAD, Map.of(CONTEXT, id, "wait", readinessState.toString()), navigationInfoMapper)); } // Yet to be implemented by browser vendors private NavigationResult reload(boolean ignoreCache, ReadinessState readinessState) { return this.bidi.send( - new Command<>( - RELOAD, - Map.of(CONTEXT, id, "ignoreCache", ignoreCache, "wait", readinessState.toString()), - navigationInfoMapper)); + new Command<>( + RELOAD, + Map.of(CONTEXT, id, "ignoreCache", ignoreCache, "wait", readinessState.toString()), + navigationInfoMapper)); } public void handleUserPrompt() { @@ -199,81 +202,81 @@ public void handleUserPrompt(String userText) { public void handleUserPrompt(boolean accept, String userText) { this.bidi.send( - new Command<>( - HANDLE_USER_PROMPT, Map.of(CONTEXT, id, "accept", accept, "userText", userText))); + new Command<>( + HANDLE_USER_PROMPT, Map.of(CONTEXT, id, "accept", accept, "userText", userText))); } public String captureScreenshot() { return this.bidi.send( - new Command<>( - "browsingContext.captureScreenshot", - Map.of(CONTEXT, id), - jsonInput -> { - Map result = jsonInput.read(Map.class); - return (String) result.get("data"); - })); + new Command<>( + "browsingContext.captureScreenshot", + Map.of(CONTEXT, id), + jsonInput -> { + Map result = jsonInput.read(Map.class); + return (String) result.get("data"); + })); } public String captureBoxScreenshot(double x, double y, double width, double height) { return this.bidi.send( - new Command<>( - "browsingContext.captureScreenshot", - Map.of( - CONTEXT, - id, - "clip", - Map.of( - "type", "viewport", - "x", x, - "y", y, - "width", width, - "height", height)), - jsonInput -> { - Map result = jsonInput.read(Map.class); - return (String) result.get("data"); - })); + new Command<>( + "browsingContext.captureScreenshot", + Map.of( + CONTEXT, + id, + "clip", + Map.of( + "type", "viewport", + "x", x, + "y", y, + "width", width, + "height", height)), + jsonInput -> { + Map result = jsonInput.read(Map.class); + return (String) result.get("data"); + })); } public String captureElementScreenshot(String elementId) { return this.bidi.send( - new Command<>( - "browsingContext.captureScreenshot", - Map.of( - CONTEXT, - id, - "clip", - Map.of( - "type", - "element", - "element", - Map.of("sharedId", elementId), - "scrollIntoView", - false)), - jsonInput -> { - Map result = jsonInput.read(Map.class); - return (String) result.get("data"); - })); + new Command<>( + "browsingContext.captureScreenshot", + Map.of( + CONTEXT, + id, + "clip", + Map.of( + "type", + "element", + "element", + Map.of("sharedId", elementId), + "scrollIntoView", + false)), + jsonInput -> { + Map result = jsonInput.read(Map.class); + return (String) result.get("data"); + })); } public String captureElementScreenshot(String elementId, boolean scrollIntoView) { return this.bidi.send( - new Command<>( - "browsingContext.captureScreenshot", - Map.of( - CONTEXT, - id, - "clip", - Map.of( - "type", - "element", - "element", - Map.of("sharedId", elementId), - "scrollIntoView", - scrollIntoView)), - jsonInput -> { - Map result = jsonInput.read(Map.class); - return (String) result.get("data"); - })); + new Command<>( + "browsingContext.captureScreenshot", + Map.of( + CONTEXT, + id, + "clip", + Map.of( + "type", + "element", + "element", + Map.of("sharedId", elementId), + "scrollIntoView", + scrollIntoView)), + jsonInput -> { + Map result = jsonInput.read(Map.class); + return (String) result.get("data"); + })); } public void setViewport(double width, double height) { @@ -281,9 +284,9 @@ public void setViewport(double width, double height) { Require.positive("Viewport height", height); this.bidi.send( - new Command<>( - "browsingContext.setViewport", - Map.of(CONTEXT, id, "viewport", Map.of("width", width, "height", height)))); + new Command<>( + "browsingContext.setViewport", + Map.of(CONTEXT, id, "viewport", Map.of("width", width, "height", height)))); } public void setViewport(double width, double height, double devicePixelRatio) { @@ -292,15 +295,15 @@ public void setViewport(double width, double height, double devicePixelRatio) { Require.positive("Device pixel ratio.", devicePixelRatio); this.bidi.send( - new Command<>( - "browsingContext.setViewport", - Map.of( - CONTEXT, - id, - "viewport", - Map.of("width", width, "height", height), - "devicePixelRatio", - devicePixelRatio))); + new Command<>( + "browsingContext.setViewport", + Map.of( + CONTEXT, + id, + "viewport", + Map.of("width", width, "height", height), + "devicePixelRatio", + devicePixelRatio))); } public void activate() { diff --git a/java/src/org/openqa/selenium/bidi/log/GenericLogEntry.java b/java/src/org/openqa/selenium/bidi/log/GenericLogEntry.java index fbb193d988958..77b1817758110 100644 --- a/java/src/org/openqa/selenium/bidi/log/GenericLogEntry.java +++ b/java/src/org/openqa/selenium/bidi/log/GenericLogEntry.java @@ -25,12 +25,12 @@ // @see https://w3c.github.io/webdriver-bidi/#types-log-logentry -public class GenericLogEntry extends BaseLogEntry implements LogEntryUtils{ +public class GenericLogEntry extends BaseLogEntry { private final String type; public GenericLogEntry( - LogLevel level, String text, long timestamp, String type, StackTrace stackTrace) { + LogLevel level, String text, long timestamp, String type, StackTrace stackTrace) { super(level, text, timestamp, stackTrace); this.type = type; } @@ -40,7 +40,44 @@ public String getType() { } public static GenericLogEntry fromJson(JsonInput input) { - return LogEntryUtils.fromJson("GenericLogEntry", input, "defaultType"); + LogLevel level = null; + String text = null; + long timestamp = 0; + String type = null; + StackTrace stackTrace = null; + + input.beginObject(); + while (input.hasNext()) { + switch (input.nextName()) { + case "level": + level = input.read(LogLevel.class); + break; + + case "text": + text = input.read(String.class); + break; + + case "timestamp": + timestamp = input.read(Long.class); + break; + + case "type": + type = input.read(String.class); + break; + + case "stackTrace": + stackTrace = input.read(StackTrace.class); + break; + + default: + input.skipValue(); + break; + } + } + + input.endObject(); + + return new GenericLogEntry(level, text, timestamp, type, stackTrace); } private Map toJson() { diff --git a/java/src/org/openqa/selenium/bidi/log/JavascriptLogEntry.java b/java/src/org/openqa/selenium/bidi/log/JavascriptLogEntry.java index 7d136b55cae21..6dbef964c5751 100644 --- a/java/src/org/openqa/selenium/bidi/log/JavascriptLogEntry.java +++ b/java/src/org/openqa/selenium/bidi/log/JavascriptLogEntry.java @@ -25,12 +25,12 @@ // @see https://w3c.github.io/webdriver-bidi/#types-log-logentry -public class JavascriptLogEntry extends GenericLogEntry implements LogEntryUtils { +public class JavascriptLogEntry extends GenericLogEntry { private final String type; public JavascriptLogEntry( - LogLevel level, String text, long timestamp, StackTrace stackTrace) { + LogLevel level, String text, long timestamp, String type, StackTrace stackTrace) { super(level, text, timestamp, "javascript", stackTrace); this.type = "javascript"; } @@ -40,7 +40,44 @@ public String getType() { } public static JavascriptLogEntry fromJson(JsonInput input) { - return (JavascriptLogEntry) LogEntryUtils.fromJson("JavascriptLogEntry", input, "javascript"); + LogLevel level = null; + String text = null; + long timestamp = 0; + String type = null; + StackTrace stackTrace = null; + + input.beginObject(); + while (input.hasNext()) { + switch (input.nextName()) { + case "level": + level = input.read(LogLevel.class); + break; + + case "text": + text = input.read(String.class); + break; + + case "timestamp": + timestamp = input.read(Long.class); + break; + + case "type": + type = input.read(String.class); + break; + + case "stackTrace": + stackTrace = input.read(StackTrace.class); + break; + + default: + input.skipValue(); + break; + } + } + + input.endObject(); + + return new JavascriptLogEntry(level, text, timestamp, type, stackTrace); } private Map toJson() { diff --git a/java/src/org/openqa/selenium/bidi/log/LogEntryUtils.java b/java/src/org/openqa/selenium/bidi/log/LogEntryUtils.java deleted file mode 100644 index 37f745a79db87..0000000000000 --- a/java/src/org/openqa/selenium/bidi/log/LogEntryUtils.java +++ /dev/null @@ -1,62 +0,0 @@ -// Licensed to the Software Freedom Conservancy (SFC) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The SFC licenses this file -// to you 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 org.openqa.selenium.bidi.log; - -import org.openqa.selenium.json.JsonInput; - -public interface LogEntryUtils { - static GenericLogEntry fromJson(String name,JsonInput input, String type) { - LogLevel level = null; - String text = null; - long timestamp = 0; - StackTrace stackTrace = null; - - input.beginObject(); - while (input.hasNext()) { - switch (input.nextName()) { - case "level": - level = input.read(LogLevel.class); - break; - case "text": - text = input.read(String.class); - break; - case "timestamp": - timestamp = input.read(Long.class); - break; - case "stackTrace": - stackTrace = input.read(StackTrace.class); - break; - default: - input.skipValue(); - break; - } - } - input.endObject(); - - switch (name) { - case "GenericLogEntry": - return new GenericLogEntry(level, text, timestamp, type, stackTrace); - case "JavascriptLogEntry": - return new JavascriptLogEntry(level, text, timestamp, stackTrace); - default: - throw new IllegalArgumentException("Unknown name: " + name); - } - - } - -} diff --git a/java/src/org/openqa/selenium/json/JsonInputFactory.java b/java/src/org/openqa/selenium/json/JsonInputFactory.java deleted file mode 100644 index e7230651cdfb5..0000000000000 --- a/java/src/org/openqa/selenium/json/JsonInputFactory.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.openqa.selenium.json; - -import java.io.StringReader; - -public class JsonInputFactory { - public static JsonInput createJsonInput(String json) { - return new JsonInput(new StringReader(json), new JsonTypeCoercer(), PropertySetting.BY_NAME); - } -} diff --git a/java/test/org/openqa/selenium/bidi/log/LogEntryUtilsTest.java b/java/test/org/openqa/selenium/bidi/log/LogEntryUtilsTest.java deleted file mode 100644 index 962a333511383..0000000000000 --- a/java/test/org/openqa/selenium/bidi/log/LogEntryUtilsTest.java +++ /dev/null @@ -1,69 +0,0 @@ -// Licensed to the Software Freedom Conservancy (SFC) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The SFC licenses this file -// to you 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 org.openqa.selenium.bidi.log; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.junit.jupiter.api.Test; -import org.openqa.selenium.json.JsonInput; -import org.openqa.selenium.json.JsonInputFactory; -import org.openqa.selenium.testing.JupiterTestBase; - - -public class LogEntryUtilsTest extends JupiterTestBase { - - @Test - public void testFromJsonForGenericLogEntry(){ - String json = "{ \"level\": \"info\", \"text\": \"Test\", \"timestamp\": 12345 }"; - JsonInput input = JsonInputFactory.createJsonInput(json); // Utiliza JsonInputFactory - - GenericLogEntry entry = (GenericLogEntry) LogEntryUtils.fromJson("GenericLogEntry", input, "defaultType"); - - assertEquals("info", entry.getLevel().toString()); - assertEquals("Test", entry.getText()); - assertEquals(12345, entry.getTimestamp()); - assertEquals("defaultType", entry.getType()); - } - - @Test - public void testFromJsonForJavascriptLogEntry(){ - String json = "{ \"level\": \"info\", \"text\": \"Test JS\", \"timestamp\": 67890 }"; - JsonInput input = JsonInputFactory.createJsonInput(json); // Utiliza JsonInputFactory - - JavascriptLogEntry entry = (JavascriptLogEntry) LogEntryUtils.fromJson("JavascriptLogEntry", input, "javascript"); - - assertEquals("info", entry.getLevel().toString()); - assertEquals("Test JS", entry.getText()); - assertEquals(67890, entry.getTimestamp()); - assertEquals("javascript", entry.getType()); - } - - @Test - public void testFromJsonForUnknownType(){ - String json = "{ \"level\": \"INFO\", \"text\": \"Test\", \"timestamp\": 12345 }"; - JsonInput input = JsonInputFactory.createJsonInput(json); // Utiliza JsonInputFactory - - try { - LogEntryUtils.fromJson("UnknownType", input, "defaultType"); - } catch (IllegalArgumentException e) { - assertTrue(e.getMessage().contains("Unknown name: UnknownType")); - } - } -} -