diff --git a/java/src/org/openqa/selenium/bidi/browser/BUILD.bazel b/java/src/org/openqa/selenium/bidi/browser/BUILD.bazel new file mode 100644 index 0000000000000..895f0a72e8bcb --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/browser/BUILD.bazel @@ -0,0 +1,26 @@ +load("@rules_jvm_external//:defs.bzl", "artifact") +load("//java:defs.bzl", "java_library") + +java_library( + name = "browser", + srcs = glob( + [ + "*.java", + ], + ), + visibility = [ + "//java/src/org/openqa/selenium/bidi:__subpackages__", + "//java/src/org/openqa/selenium/firefox:__subpackages__", + "//java/src/org/openqa/selenium/remote:__pkg__", + "//java/test/org/openqa/selenium/bidi:__subpackages__", + "//java/test/org/openqa/selenium/grid:__subpackages__", + ], + deps = [ + "//java/src/org/openqa/selenium:core", + "//java/src/org/openqa/selenium/bidi", + "//java/src/org/openqa/selenium/bidi/script", + "//java/src/org/openqa/selenium/json", + "//java/src/org/openqa/selenium/remote/http", + artifact("com.google.auto.service:auto-service-annotations"), + ], +) diff --git a/java/src/org/openqa/selenium/bidi/browser/ClientWindow.java b/java/src/org/openqa/selenium/bidi/browser/ClientWindow.java new file mode 100644 index 0000000000000..ee48d6e704a96 --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/browser/ClientWindow.java @@ -0,0 +1,30 @@ +// 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.browser; + +public class ClientWindow { + private final String id; + + public ClientWindow(String id) { + this.id = id; + } + + public String getId() { + return id; + } +} diff --git a/java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java b/java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java new file mode 100644 index 0000000000000..17562895dd52d --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java @@ -0,0 +1,86 @@ +// 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.browser; + +import java.util.Map; + +public class ClientWindowInfo { + private final String clientWindow; + private final ClientWindowState state; + private final Integer width; + private final Integer height; + private final Integer x; + private final Integer y; + private final boolean active; + + public ClientWindowInfo( + String clientWindow, + ClientWindowState state, + Integer width, + Integer height, + Integer x, + Integer y, + boolean active) { + this.clientWindow = clientWindow; + this.state = state; + this.width = width; + this.height = height; + this.x = x; + this.y = y; + this.active = active; + } + + public static ClientWindowInfo fromJson(Map map) { + return new ClientWindowInfo( + (String) map.get("clientWindow"), + ClientWindowState.fromString((String) map.get("state")), + ((Number) map.get("width")).intValue(), + ((Number) map.get("height")).intValue(), + ((Number) map.get("x")).intValue(), + ((Number) map.get("y")).intValue(), + (Boolean) map.get("active")); + } + + public String getClientWindow() { + return clientWindow; + } + + public ClientWindowState getState() { + return state; + } + + public Integer getWidth() { + return width; + } + + public Integer getHeight() { + return height; + } + + public Integer getX() { + return x; + } + + public Integer getY() { + return y; + } + + public boolean isActive() { + return active; + } +} diff --git a/java/src/org/openqa/selenium/bidi/browser/ClientWindowState.java b/java/src/org/openqa/selenium/bidi/browser/ClientWindowState.java new file mode 100644 index 0000000000000..027fd361894bd --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/browser/ClientWindowState.java @@ -0,0 +1,44 @@ +// 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.browser; + +public enum ClientWindowState { + FULLSCREEN("fullscreen"), + MAXIMIZED("maximized"), + MINIMIZED("minimized"), + NORMAL("normal"); + + private final String state; + + ClientWindowState(String state) { + this.state = state; + } + + public String toString() { + return state; + } + + public static ClientWindowState fromString(String state) { + for (ClientWindowState windowState : values()) { + if (windowState.state.equals(state)) { + return windowState; + } + } + throw new IllegalArgumentException("Invalid window state: " + state); + } +} diff --git a/java/src/org/openqa/selenium/bidi/module/BUILD.bazel b/java/src/org/openqa/selenium/bidi/module/BUILD.bazel index 9706b1493f778..59676269b2733 100644 --- a/java/src/org/openqa/selenium/bidi/module/BUILD.bazel +++ b/java/src/org/openqa/selenium/bidi/module/BUILD.bazel @@ -18,6 +18,7 @@ java_library( deps = [ "//java/src/org/openqa/selenium:core", "//java/src/org/openqa/selenium/bidi", + "//java/src/org/openqa/selenium/bidi/browser", "//java/src/org/openqa/selenium/bidi/browsingcontext", "//java/src/org/openqa/selenium/bidi/log", "//java/src/org/openqa/selenium/bidi/network", diff --git a/java/src/org/openqa/selenium/bidi/module/Browser.java b/java/src/org/openqa/selenium/bidi/module/Browser.java index c199eeab59716..be14ef2af666e 100644 --- a/java/src/org/openqa/selenium/bidi/module/Browser.java +++ b/java/src/org/openqa/selenium/bidi/module/Browser.java @@ -25,6 +25,7 @@ import org.openqa.selenium.bidi.BiDi; import org.openqa.selenium.bidi.Command; import org.openqa.selenium.bidi.HasBiDi; +import org.openqa.selenium.bidi.browser.ClientWindowInfo; import org.openqa.selenium.json.JsonInput; public class Browser { @@ -52,6 +53,24 @@ public class Browser { return userContexts; }; + private final Function> clientWindowsInfoMapper = + jsonInput -> { + Map response = jsonInput.read(Map.class); + List> clientWindowsResponse = + (List>) response.get("clientWindows"); + + List clientWindows = new ArrayList<>(); + clientWindowsResponse.forEach(map -> clientWindows.add(ClientWindowInfo.fromJson(map))); + + return clientWindows; + }; + + private final Function clientWindowInfoMapper = + jsonInput -> { + Map response = jsonInput.read(Map.class); + return ClientWindowInfo.fromJson(response); + }; + public Browser(WebDriver driver) { this.bidi = ((HasBiDi) driver).getBiDi(); } @@ -67,4 +86,8 @@ public List getUserContexts() { public void removeUserContext(String userContext) { bidi.send(new Command<>("browser.removeUserContext", Map.of("userContext", userContext))); } + + public List getClientWindows() { + return bidi.send(new Command<>("browser.getClientWindows", Map.of(), clientWindowsInfoMapper)); + } } diff --git a/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java b/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java index b35b9d4c14f45..98239a6732e00 100644 --- a/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java +++ b/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java @@ -75,4 +75,20 @@ void canRemoveUserContext() { browser.removeUserContext(userContext1); } + + @Test + @NeedsFreshDriver + void canGetClientWindows() { + List clientWindows = browser.getClientWindows(); + + assertThat(clientWindows).isNotNull(); + assertThat(clientWindows.size()).isGreaterThan(0); + + ClientWindowInfo windowInfo = clientWindows.get(0); + assertThat(windowInfo.getClientWindow()).isNotNull(); + assertThat(windowInfo.getState()).isInstanceOf(ClientWindowState.class); + assertThat(windowInfo.getWidth()).isGreaterThan(0); + assertThat(windowInfo.getHeight()).isGreaterThan(0); + assertThat(windowInfo.isActive()).isIn(true, false); + } }