From 975e74a575335fcaceb43d7a1565d14ed3b8c593 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Tue, 3 Dec 2024 15:41:49 +0530 Subject: [PATCH 01/10] add bidi implementation for `getClientWindows` and `setClientWindowState` --- .../selenium/bidi/browser/ClientWindow.java | 30 +++++++ .../bidi/browser/ClientWindowInfo.java | 73 +++++++++++++++++ .../bidi/browser/ClientWindowState.java | 79 +++++++++++++++++++ .../openqa/selenium/bidi/module/Browser.java | 32 ++++++++ .../bidi/browser/BrowserCommandsTest.java | 32 ++++++++ 5 files changed, 246 insertions(+) create mode 100644 java/src/org/openqa/selenium/bidi/browser/ClientWindow.java create mode 100644 java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java create mode 100644 java/src/org/openqa/selenium/bidi/browser/ClientWindowState.java 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..d56ff1c1492f5 --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java @@ -0,0 +1,73 @@ +// 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 String state; + private final Integer width; + private final Integer height; + private final Integer x; + private final Integer y; + + public ClientWindowInfo( + String clientWindow, String state, Integer width, Integer height, Integer x, Integer y) { + this.clientWindow = clientWindow; + this.state = state; + this.width = width; + this.height = height; + this.x = x; + this.y = y; + } + + public static ClientWindowInfo fromJson(Map map) { + return new ClientWindowInfo( + (String) map.get("clientWindow"), + (String) map.get("state"), + (Integer) map.get("width"), + (Integer) map.get("height"), + (Integer) map.get("x"), + (Integer) map.get("y")); + } + + public String getClientWindow() { + return clientWindow; + } + + public String getState() { + return state; + } + + public Integer getWidth() { + return width; + } + + public Integer getHeight() { + return height; + } + + public Integer getX() { + return x; + } + + public Integer getY() { + return y; + } +} 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..0059b7c70f31f --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/browser/ClientWindowState.java @@ -0,0 +1,79 @@ +// 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.HashMap; +import java.util.Map; + +public class ClientWindowState { + private final String state; + private Integer width; + private Integer height; + private Integer x; + private Integer y; + + private ClientWindowState(String state) { + this.state = state; + } + + public static ClientWindowState normal() { + return new ClientWindowState("normal"); + } + + public static ClientWindowState minimized() { + return new ClientWindowState("minimized"); + } + + public static ClientWindowState maximized() { + return new ClientWindowState("maximized"); + } + + public static ClientWindowState fullscreen() { + return new ClientWindowState("fullscreen"); + } + + public ClientWindowState setWidth(Integer width) { + this.width = width; + return this; + } + + public ClientWindowState setHeight(Integer height) { + this.height = height; + return this; + } + + public ClientWindowState setX(Integer x) { + this.x = x; + return this; + } + + public ClientWindowState setY(Integer y) { + this.y = y; + return this; + } + + public Map toMap() { + Map map = new HashMap<>(); + map.put("state", state); + if (width != null) map.put("width", width); + if (height != null) map.put("height", height); + if (x != null) map.put("x", x); + if (y != null) map.put("y", y); + return map; + } +} diff --git a/java/src/org/openqa/selenium/bidi/module/Browser.java b/java/src/org/openqa/selenium/bidi/module/Browser.java index c199eeab59716..276aa0f311e8a 100644 --- a/java/src/org/openqa/selenium/bidi/module/Browser.java +++ b/java/src/org/openqa/selenium/bidi/module/Browser.java @@ -25,6 +25,9 @@ import org.openqa.selenium.bidi.BiDi; import org.openqa.selenium.bidi.Command; import org.openqa.selenium.bidi.HasBiDi; +import org.openqa.selenium.bidi.browser.ClientWindow; +import org.openqa.selenium.bidi.browser.ClientWindowInfo; +import org.openqa.selenium.bidi.browser.ClientWindowState; import org.openqa.selenium.json.JsonInput; public class Browser { @@ -52,6 +55,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 +88,15 @@ 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)); + } + + public ClientWindowInfo setClientWindowState(ClientWindow clientWindow, ClientWindowState state) { + Map params = state.toMap(); + params.put("clientWindow", clientWindow.getId()); + + return bidi.send(new Command<>("browser.setClientWindowState", params, clientWindowInfoMapper)); + } } diff --git a/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java b/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java index b35b9d4c14f45..fb5f83ebf96f6 100644 --- a/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java +++ b/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java @@ -75,4 +75,36 @@ 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()).isNotNull(); + } + + @Test + @NeedsFreshDriver + void canSetClientWindowState() { + List clientWindows = browser.getClientWindows(); + ClientWindow clientWindow = new ClientWindow(clientWindows.get(0).getClientWindow()); + + ClientWindowState state = ClientWindowState.maximized(); + ClientWindowInfo updatedWindowInfo = browser.setClientWindowState(clientWindow, state); + + assertThat(updatedWindowInfo.getState()).isEqualTo("maximized"); + + state = ClientWindowState.normal().setWidth(800).setHeight(600); + updatedWindowInfo = browser.setClientWindowState(clientWindow, state); + + assertThat(updatedWindowInfo.getState()).isEqualTo("normal"); + assertThat(updatedWindowInfo.getWidth()).isEqualTo(800); + assertThat(updatedWindowInfo.getHeight()).isEqualTo(600); + } } From 645c81a041bb940e3e17b97573c4680440aa473d Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Tue, 3 Dec 2024 15:42:18 +0530 Subject: [PATCH 02/10] update bazel build files --- .../openqa/selenium/bidi/browser/BUILD.bazel | 26 +++++++++++++++++++ .../openqa/selenium/bidi/module/BUILD.bazel | 1 + 2 files changed, 27 insertions(+) create mode 100644 java/src/org/openqa/selenium/bidi/browser/BUILD.bazel 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/module/BUILD.bazel b/java/src/org/openqa/selenium/bidi/module/BUILD.bazel index 9706b1493f778..7f273db01f315 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:browser", "//java/src/org/openqa/selenium/bidi/browsingcontext", "//java/src/org/openqa/selenium/bidi/log", "//java/src/org/openqa/selenium/bidi/network", From cb54432f4b4c86092fe79a33154849c1427fe454 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Wed, 4 Dec 2024 18:19:34 +0530 Subject: [PATCH 03/10] change to Number --- .../openqa/selenium/bidi/browser/ClientWindowInfo.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java b/java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java index d56ff1c1492f5..331fdc6c222fa 100644 --- a/java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java +++ b/java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java @@ -41,10 +41,10 @@ public static ClientWindowInfo fromJson(Map map) { return new ClientWindowInfo( (String) map.get("clientWindow"), (String) map.get("state"), - (Integer) map.get("width"), - (Integer) map.get("height"), - (Integer) map.get("x"), - (Integer) map.get("y")); + ((Number) map.get("width")).intValue(), + ((Number) map.get("height")).intValue(), + ((Number) map.get("x")).intValue(), + ((Number) map.get("y")).intValue()); } public String getClientWindow() { From 60278f75c51d7a81dedb999239019899ee846cc4 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Wed, 4 Dec 2024 18:21:50 +0530 Subject: [PATCH 04/10] update BUILD.bazel for browser --- java/src/org/openqa/selenium/bidi/module/BUILD.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/src/org/openqa/selenium/bidi/module/BUILD.bazel b/java/src/org/openqa/selenium/bidi/module/BUILD.bazel index 7f273db01f315..59676269b2733 100644 --- a/java/src/org/openqa/selenium/bidi/module/BUILD.bazel +++ b/java/src/org/openqa/selenium/bidi/module/BUILD.bazel @@ -18,7 +18,7 @@ java_library( deps = [ "//java/src/org/openqa/selenium:core", "//java/src/org/openqa/selenium/bidi", - "//java/src/org/openqa/selenium/bidi/browser:browser", + "//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", From 7fbdca616e6d85fb900db73d28e26d409ea618aa Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Tue, 3 Dec 2024 15:41:49 +0530 Subject: [PATCH 05/10] add bidi implementation for `getClientWindows` and `setClientWindowState` --- .../selenium/bidi/browser/ClientWindow.java | 30 +++++++ .../bidi/browser/ClientWindowInfo.java | 73 +++++++++++++++++ .../bidi/browser/ClientWindowState.java | 79 +++++++++++++++++++ .../openqa/selenium/bidi/module/Browser.java | 32 ++++++++ .../bidi/browser/BrowserCommandsTest.java | 32 ++++++++ 5 files changed, 246 insertions(+) create mode 100644 java/src/org/openqa/selenium/bidi/browser/ClientWindow.java create mode 100644 java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java create mode 100644 java/src/org/openqa/selenium/bidi/browser/ClientWindowState.java 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..d56ff1c1492f5 --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java @@ -0,0 +1,73 @@ +// 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 String state; + private final Integer width; + private final Integer height; + private final Integer x; + private final Integer y; + + public ClientWindowInfo( + String clientWindow, String state, Integer width, Integer height, Integer x, Integer y) { + this.clientWindow = clientWindow; + this.state = state; + this.width = width; + this.height = height; + this.x = x; + this.y = y; + } + + public static ClientWindowInfo fromJson(Map map) { + return new ClientWindowInfo( + (String) map.get("clientWindow"), + (String) map.get("state"), + (Integer) map.get("width"), + (Integer) map.get("height"), + (Integer) map.get("x"), + (Integer) map.get("y")); + } + + public String getClientWindow() { + return clientWindow; + } + + public String getState() { + return state; + } + + public Integer getWidth() { + return width; + } + + public Integer getHeight() { + return height; + } + + public Integer getX() { + return x; + } + + public Integer getY() { + return y; + } +} 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..0059b7c70f31f --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/browser/ClientWindowState.java @@ -0,0 +1,79 @@ +// 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.HashMap; +import java.util.Map; + +public class ClientWindowState { + private final String state; + private Integer width; + private Integer height; + private Integer x; + private Integer y; + + private ClientWindowState(String state) { + this.state = state; + } + + public static ClientWindowState normal() { + return new ClientWindowState("normal"); + } + + public static ClientWindowState minimized() { + return new ClientWindowState("minimized"); + } + + public static ClientWindowState maximized() { + return new ClientWindowState("maximized"); + } + + public static ClientWindowState fullscreen() { + return new ClientWindowState("fullscreen"); + } + + public ClientWindowState setWidth(Integer width) { + this.width = width; + return this; + } + + public ClientWindowState setHeight(Integer height) { + this.height = height; + return this; + } + + public ClientWindowState setX(Integer x) { + this.x = x; + return this; + } + + public ClientWindowState setY(Integer y) { + this.y = y; + return this; + } + + public Map toMap() { + Map map = new HashMap<>(); + map.put("state", state); + if (width != null) map.put("width", width); + if (height != null) map.put("height", height); + if (x != null) map.put("x", x); + if (y != null) map.put("y", y); + return map; + } +} diff --git a/java/src/org/openqa/selenium/bidi/module/Browser.java b/java/src/org/openqa/selenium/bidi/module/Browser.java index c199eeab59716..276aa0f311e8a 100644 --- a/java/src/org/openqa/selenium/bidi/module/Browser.java +++ b/java/src/org/openqa/selenium/bidi/module/Browser.java @@ -25,6 +25,9 @@ import org.openqa.selenium.bidi.BiDi; import org.openqa.selenium.bidi.Command; import org.openqa.selenium.bidi.HasBiDi; +import org.openqa.selenium.bidi.browser.ClientWindow; +import org.openqa.selenium.bidi.browser.ClientWindowInfo; +import org.openqa.selenium.bidi.browser.ClientWindowState; import org.openqa.selenium.json.JsonInput; public class Browser { @@ -52,6 +55,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 +88,15 @@ 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)); + } + + public ClientWindowInfo setClientWindowState(ClientWindow clientWindow, ClientWindowState state) { + Map params = state.toMap(); + params.put("clientWindow", clientWindow.getId()); + + return bidi.send(new Command<>("browser.setClientWindowState", params, clientWindowInfoMapper)); + } } diff --git a/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java b/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java index b35b9d4c14f45..fb5f83ebf96f6 100644 --- a/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java +++ b/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java @@ -75,4 +75,36 @@ 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()).isNotNull(); + } + + @Test + @NeedsFreshDriver + void canSetClientWindowState() { + List clientWindows = browser.getClientWindows(); + ClientWindow clientWindow = new ClientWindow(clientWindows.get(0).getClientWindow()); + + ClientWindowState state = ClientWindowState.maximized(); + ClientWindowInfo updatedWindowInfo = browser.setClientWindowState(clientWindow, state); + + assertThat(updatedWindowInfo.getState()).isEqualTo("maximized"); + + state = ClientWindowState.normal().setWidth(800).setHeight(600); + updatedWindowInfo = browser.setClientWindowState(clientWindow, state); + + assertThat(updatedWindowInfo.getState()).isEqualTo("normal"); + assertThat(updatedWindowInfo.getWidth()).isEqualTo(800); + assertThat(updatedWindowInfo.getHeight()).isEqualTo(600); + } } From dc3c85a7a199a8f6cf1e5df5c882548f80f1ce04 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Tue, 3 Dec 2024 15:42:18 +0530 Subject: [PATCH 06/10] update bazel build files --- .../openqa/selenium/bidi/browser/BUILD.bazel | 26 +++++++++++++++++++ .../openqa/selenium/bidi/module/BUILD.bazel | 1 + 2 files changed, 27 insertions(+) create mode 100644 java/src/org/openqa/selenium/bidi/browser/BUILD.bazel 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/module/BUILD.bazel b/java/src/org/openqa/selenium/bidi/module/BUILD.bazel index 9706b1493f778..7f273db01f315 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:browser", "//java/src/org/openqa/selenium/bidi/browsingcontext", "//java/src/org/openqa/selenium/bidi/log", "//java/src/org/openqa/selenium/bidi/network", From dd00040ae09ebe44f16c6bb55bb206742e10cd45 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Wed, 4 Dec 2024 18:19:34 +0530 Subject: [PATCH 07/10] change to Number --- .../openqa/selenium/bidi/browser/ClientWindowInfo.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java b/java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java index d56ff1c1492f5..331fdc6c222fa 100644 --- a/java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java +++ b/java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java @@ -41,10 +41,10 @@ public static ClientWindowInfo fromJson(Map map) { return new ClientWindowInfo( (String) map.get("clientWindow"), (String) map.get("state"), - (Integer) map.get("width"), - (Integer) map.get("height"), - (Integer) map.get("x"), - (Integer) map.get("y")); + ((Number) map.get("width")).intValue(), + ((Number) map.get("height")).intValue(), + ((Number) map.get("x")).intValue(), + ((Number) map.get("y")).intValue()); } public String getClientWindow() { From dfabe2ee33822682196691461c9fac362dc9bf57 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Wed, 4 Dec 2024 18:21:50 +0530 Subject: [PATCH 08/10] update BUILD.bazel for browser --- java/src/org/openqa/selenium/bidi/module/BUILD.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/src/org/openqa/selenium/bidi/module/BUILD.bazel b/java/src/org/openqa/selenium/bidi/module/BUILD.bazel index 7f273db01f315..59676269b2733 100644 --- a/java/src/org/openqa/selenium/bidi/module/BUILD.bazel +++ b/java/src/org/openqa/selenium/bidi/module/BUILD.bazel @@ -18,7 +18,7 @@ java_library( deps = [ "//java/src/org/openqa/selenium:core", "//java/src/org/openqa/selenium/bidi", - "//java/src/org/openqa/selenium/bidi/browser:browser", + "//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", From 528fb6c4fdbb08e197024b95f2f64cc8a30f8e01 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Fri, 6 Dec 2024 20:06:17 +0530 Subject: [PATCH 09/10] java bidi `getClientWindows` implementation --- .../bidi/browser/ClientWindowState.java | 79 ------------------- .../openqa/selenium/bidi/module/Browser.java | 9 --- .../bidi/browser/BrowserCommandsTest.java | 19 ----- 3 files changed, 107 deletions(-) delete mode 100644 java/src/org/openqa/selenium/bidi/browser/ClientWindowState.java diff --git a/java/src/org/openqa/selenium/bidi/browser/ClientWindowState.java b/java/src/org/openqa/selenium/bidi/browser/ClientWindowState.java deleted file mode 100644 index 0059b7c70f31f..0000000000000 --- a/java/src/org/openqa/selenium/bidi/browser/ClientWindowState.java +++ /dev/null @@ -1,79 +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.browser; - -import java.util.HashMap; -import java.util.Map; - -public class ClientWindowState { - private final String state; - private Integer width; - private Integer height; - private Integer x; - private Integer y; - - private ClientWindowState(String state) { - this.state = state; - } - - public static ClientWindowState normal() { - return new ClientWindowState("normal"); - } - - public static ClientWindowState minimized() { - return new ClientWindowState("minimized"); - } - - public static ClientWindowState maximized() { - return new ClientWindowState("maximized"); - } - - public static ClientWindowState fullscreen() { - return new ClientWindowState("fullscreen"); - } - - public ClientWindowState setWidth(Integer width) { - this.width = width; - return this; - } - - public ClientWindowState setHeight(Integer height) { - this.height = height; - return this; - } - - public ClientWindowState setX(Integer x) { - this.x = x; - return this; - } - - public ClientWindowState setY(Integer y) { - this.y = y; - return this; - } - - public Map toMap() { - Map map = new HashMap<>(); - map.put("state", state); - if (width != null) map.put("width", width); - if (height != null) map.put("height", height); - if (x != null) map.put("x", x); - if (y != null) map.put("y", y); - return map; - } -} diff --git a/java/src/org/openqa/selenium/bidi/module/Browser.java b/java/src/org/openqa/selenium/bidi/module/Browser.java index 276aa0f311e8a..be14ef2af666e 100644 --- a/java/src/org/openqa/selenium/bidi/module/Browser.java +++ b/java/src/org/openqa/selenium/bidi/module/Browser.java @@ -25,9 +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.ClientWindow; import org.openqa.selenium.bidi.browser.ClientWindowInfo; -import org.openqa.selenium.bidi.browser.ClientWindowState; import org.openqa.selenium.json.JsonInput; public class Browser { @@ -92,11 +90,4 @@ public void removeUserContext(String userContext) { public List getClientWindows() { return bidi.send(new Command<>("browser.getClientWindows", Map.of(), clientWindowsInfoMapper)); } - - public ClientWindowInfo setClientWindowState(ClientWindow clientWindow, ClientWindowState state) { - Map params = state.toMap(); - params.put("clientWindow", clientWindow.getId()); - - return bidi.send(new Command<>("browser.setClientWindowState", params, clientWindowInfoMapper)); - } } diff --git a/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java b/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java index fb5f83ebf96f6..477a4a1e65242 100644 --- a/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java +++ b/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java @@ -88,23 +88,4 @@ void canGetClientWindows() { assertThat(windowInfo.getClientWindow()).isNotNull(); assertThat(windowInfo.getState()).isNotNull(); } - - @Test - @NeedsFreshDriver - void canSetClientWindowState() { - List clientWindows = browser.getClientWindows(); - ClientWindow clientWindow = new ClientWindow(clientWindows.get(0).getClientWindow()); - - ClientWindowState state = ClientWindowState.maximized(); - ClientWindowInfo updatedWindowInfo = browser.setClientWindowState(clientWindow, state); - - assertThat(updatedWindowInfo.getState()).isEqualTo("maximized"); - - state = ClientWindowState.normal().setWidth(800).setHeight(600); - updatedWindowInfo = browser.setClientWindowState(clientWindow, state); - - assertThat(updatedWindowInfo.getState()).isEqualTo("normal"); - assertThat(updatedWindowInfo.getWidth()).isEqualTo(800); - assertThat(updatedWindowInfo.getHeight()).isEqualTo(600); - } } From 2be5dd9785f408a061e38d11b152a0376e49842b Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Thu, 6 Feb 2025 13:55:32 +0530 Subject: [PATCH 10/10] add `ClientWindowState` enum and `active` bool field --- .../bidi/browser/ClientWindowInfo.java | 23 +++++++--- .../bidi/browser/ClientWindowState.java | 44 +++++++++++++++++++ .../bidi/browser/BrowserCommandsTest.java | 5 ++- 3 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 java/src/org/openqa/selenium/bidi/browser/ClientWindowState.java diff --git a/java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java b/java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java index 331fdc6c222fa..17562895dd52d 100644 --- a/java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java +++ b/java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java @@ -21,37 +21,46 @@ public class ClientWindowInfo { private final String clientWindow; - private final String state; + 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, String state, Integer width, Integer height, Integer x, Integer y) { + 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"), - (String) map.get("state"), + 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()); + ((Number) map.get("y")).intValue(), + (Boolean) map.get("active")); } public String getClientWindow() { return clientWindow; } - public String getState() { + public ClientWindowState getState() { return state; } @@ -70,4 +79,8 @@ public Integer getX() { 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/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java b/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java index 477a4a1e65242..98239a6732e00 100644 --- a/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java +++ b/java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java @@ -86,6 +86,9 @@ void canGetClientWindows() { ClientWindowInfo windowInfo = clientWindows.get(0); assertThat(windowInfo.getClientWindow()).isNotNull(); - assertThat(windowInfo.getState()).isNotNull(); + assertThat(windowInfo.getState()).isInstanceOf(ClientWindowState.class); + assertThat(windowInfo.getWidth()).isGreaterThan(0); + assertThat(windowInfo.getHeight()).isGreaterThan(0); + assertThat(windowInfo.isActive()).isIn(true, false); } }