Skip to content

Commit

Permalink
[java][bidi]: implement getClientWindows method (#14869)
Browse files Browse the repository at this point in the history
  • Loading branch information
navin772 authored Feb 17, 2025
1 parent c97d9b7 commit c9bced0
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 0 deletions.
26 changes: 26 additions & 0 deletions java/src/org/openqa/selenium/bidi/browser/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"),
],
)
30 changes: 30 additions & 0 deletions java/src/org/openqa/selenium/bidi/browser/ClientWindow.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
86 changes: 86 additions & 0 deletions java/src/org/openqa/selenium/bidi/browser/ClientWindowInfo.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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;
}
}
44 changes: 44 additions & 0 deletions java/src/org/openqa/selenium/bidi/browser/ClientWindowState.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions java/src/org/openqa/selenium/bidi/module/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 23 additions & 0 deletions java/src/org/openqa/selenium/bidi/module/Browser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -52,6 +53,24 @@ public class Browser {
return userContexts;
};

private final Function<JsonInput, List<ClientWindowInfo>> clientWindowsInfoMapper =
jsonInput -> {
Map<String, Object> response = jsonInput.read(Map.class);
List<Map<String, Object>> clientWindowsResponse =
(List<Map<String, Object>>) response.get("clientWindows");

List<ClientWindowInfo> clientWindows = new ArrayList<>();
clientWindowsResponse.forEach(map -> clientWindows.add(ClientWindowInfo.fromJson(map)));

return clientWindows;
};

private final Function<JsonInput, ClientWindowInfo> clientWindowInfoMapper =
jsonInput -> {
Map<String, Object> response = jsonInput.read(Map.class);
return ClientWindowInfo.fromJson(response);
};

public Browser(WebDriver driver) {
this.bidi = ((HasBiDi) driver).getBiDi();
}
Expand All @@ -67,4 +86,8 @@ public List<String> getUserContexts() {
public void removeUserContext(String userContext) {
bidi.send(new Command<>("browser.removeUserContext", Map.of("userContext", userContext)));
}

public List<ClientWindowInfo> getClientWindows() {
return bidi.send(new Command<>("browser.getClientWindows", Map.of(), clientWindowsInfoMapper));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,20 @@ void canRemoveUserContext() {

browser.removeUserContext(userContext1);
}

@Test
@NeedsFreshDriver
void canGetClientWindows() {
List<ClientWindowInfo> 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);
}
}

0 comments on commit c9bced0

Please sign in to comment.