Skip to content

Commit c9bced0

Browse files
authored
[java][bidi]: implement getClientWindows method (#14869)
1 parent c97d9b7 commit c9bced0

File tree

7 files changed

+226
-0
lines changed

7 files changed

+226
-0
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
load("@rules_jvm_external//:defs.bzl", "artifact")
2+
load("//java:defs.bzl", "java_library")
3+
4+
java_library(
5+
name = "browser",
6+
srcs = glob(
7+
[
8+
"*.java",
9+
],
10+
),
11+
visibility = [
12+
"//java/src/org/openqa/selenium/bidi:__subpackages__",
13+
"//java/src/org/openqa/selenium/firefox:__subpackages__",
14+
"//java/src/org/openqa/selenium/remote:__pkg__",
15+
"//java/test/org/openqa/selenium/bidi:__subpackages__",
16+
"//java/test/org/openqa/selenium/grid:__subpackages__",
17+
],
18+
deps = [
19+
"//java/src/org/openqa/selenium:core",
20+
"//java/src/org/openqa/selenium/bidi",
21+
"//java/src/org/openqa/selenium/bidi/script",
22+
"//java/src/org/openqa/selenium/json",
23+
"//java/src/org/openqa/selenium/remote/http",
24+
artifact("com.google.auto.service:auto-service-annotations"),
25+
],
26+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.bidi.browser;
19+
20+
public class ClientWindow {
21+
private final String id;
22+
23+
public ClientWindow(String id) {
24+
this.id = id;
25+
}
26+
27+
public String getId() {
28+
return id;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.bidi.browser;
19+
20+
import java.util.Map;
21+
22+
public class ClientWindowInfo {
23+
private final String clientWindow;
24+
private final ClientWindowState state;
25+
private final Integer width;
26+
private final Integer height;
27+
private final Integer x;
28+
private final Integer y;
29+
private final boolean active;
30+
31+
public ClientWindowInfo(
32+
String clientWindow,
33+
ClientWindowState state,
34+
Integer width,
35+
Integer height,
36+
Integer x,
37+
Integer y,
38+
boolean active) {
39+
this.clientWindow = clientWindow;
40+
this.state = state;
41+
this.width = width;
42+
this.height = height;
43+
this.x = x;
44+
this.y = y;
45+
this.active = active;
46+
}
47+
48+
public static ClientWindowInfo fromJson(Map<String, Object> map) {
49+
return new ClientWindowInfo(
50+
(String) map.get("clientWindow"),
51+
ClientWindowState.fromString((String) map.get("state")),
52+
((Number) map.get("width")).intValue(),
53+
((Number) map.get("height")).intValue(),
54+
((Number) map.get("x")).intValue(),
55+
((Number) map.get("y")).intValue(),
56+
(Boolean) map.get("active"));
57+
}
58+
59+
public String getClientWindow() {
60+
return clientWindow;
61+
}
62+
63+
public ClientWindowState getState() {
64+
return state;
65+
}
66+
67+
public Integer getWidth() {
68+
return width;
69+
}
70+
71+
public Integer getHeight() {
72+
return height;
73+
}
74+
75+
public Integer getX() {
76+
return x;
77+
}
78+
79+
public Integer getY() {
80+
return y;
81+
}
82+
83+
public boolean isActive() {
84+
return active;
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.bidi.browser;
19+
20+
public enum ClientWindowState {
21+
FULLSCREEN("fullscreen"),
22+
MAXIMIZED("maximized"),
23+
MINIMIZED("minimized"),
24+
NORMAL("normal");
25+
26+
private final String state;
27+
28+
ClientWindowState(String state) {
29+
this.state = state;
30+
}
31+
32+
public String toString() {
33+
return state;
34+
}
35+
36+
public static ClientWindowState fromString(String state) {
37+
for (ClientWindowState windowState : values()) {
38+
if (windowState.state.equals(state)) {
39+
return windowState;
40+
}
41+
}
42+
throw new IllegalArgumentException("Invalid window state: " + state);
43+
}
44+
}

Diff for: java/src/org/openqa/selenium/bidi/module/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ java_library(
1818
deps = [
1919
"//java/src/org/openqa/selenium:core",
2020
"//java/src/org/openqa/selenium/bidi",
21+
"//java/src/org/openqa/selenium/bidi/browser",
2122
"//java/src/org/openqa/selenium/bidi/browsingcontext",
2223
"//java/src/org/openqa/selenium/bidi/log",
2324
"//java/src/org/openqa/selenium/bidi/network",

Diff for: java/src/org/openqa/selenium/bidi/module/Browser.java

+23
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.openqa.selenium.bidi.BiDi;
2626
import org.openqa.selenium.bidi.Command;
2727
import org.openqa.selenium.bidi.HasBiDi;
28+
import org.openqa.selenium.bidi.browser.ClientWindowInfo;
2829
import org.openqa.selenium.json.JsonInput;
2930

3031
public class Browser {
@@ -52,6 +53,24 @@ public class Browser {
5253
return userContexts;
5354
};
5455

56+
private final Function<JsonInput, List<ClientWindowInfo>> clientWindowsInfoMapper =
57+
jsonInput -> {
58+
Map<String, Object> response = jsonInput.read(Map.class);
59+
List<Map<String, Object>> clientWindowsResponse =
60+
(List<Map<String, Object>>) response.get("clientWindows");
61+
62+
List<ClientWindowInfo> clientWindows = new ArrayList<>();
63+
clientWindowsResponse.forEach(map -> clientWindows.add(ClientWindowInfo.fromJson(map)));
64+
65+
return clientWindows;
66+
};
67+
68+
private final Function<JsonInput, ClientWindowInfo> clientWindowInfoMapper =
69+
jsonInput -> {
70+
Map<String, Object> response = jsonInput.read(Map.class);
71+
return ClientWindowInfo.fromJson(response);
72+
};
73+
5574
public Browser(WebDriver driver) {
5675
this.bidi = ((HasBiDi) driver).getBiDi();
5776
}
@@ -67,4 +86,8 @@ public List<String> getUserContexts() {
6786
public void removeUserContext(String userContext) {
6887
bidi.send(new Command<>("browser.removeUserContext", Map.of("userContext", userContext)));
6988
}
89+
90+
public List<ClientWindowInfo> getClientWindows() {
91+
return bidi.send(new Command<>("browser.getClientWindows", Map.of(), clientWindowsInfoMapper));
92+
}
7093
}

Diff for: java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,20 @@ void canRemoveUserContext() {
7575

7676
browser.removeUserContext(userContext1);
7777
}
78+
79+
@Test
80+
@NeedsFreshDriver
81+
void canGetClientWindows() {
82+
List<ClientWindowInfo> clientWindows = browser.getClientWindows();
83+
84+
assertThat(clientWindows).isNotNull();
85+
assertThat(clientWindows.size()).isGreaterThan(0);
86+
87+
ClientWindowInfo windowInfo = clientWindows.get(0);
88+
assertThat(windowInfo.getClientWindow()).isNotNull();
89+
assertThat(windowInfo.getState()).isInstanceOf(ClientWindowState.class);
90+
assertThat(windowInfo.getWidth()).isGreaterThan(0);
91+
assertThat(windowInfo.getHeight()).isGreaterThan(0);
92+
assertThat(windowInfo.isActive()).isIn(true, false);
93+
}
7894
}

0 commit comments

Comments
 (0)