Skip to content

Commit 265df15

Browse files
authored
[bidi][java] Add Permissions Module commands (#15294)
1 parent 013ab47 commit 265df15

File tree

8 files changed

+349
-0
lines changed

8 files changed

+349
-0
lines changed

java/src/org/openqa/selenium/bidi/module/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ java_library(
2222
"//java/src/org/openqa/selenium/bidi/browsingcontext",
2323
"//java/src/org/openqa/selenium/bidi/log",
2424
"//java/src/org/openqa/selenium/bidi/network",
25+
"//java/src/org/openqa/selenium/bidi/permissions",
2526
"//java/src/org/openqa/selenium/bidi/script",
2627
"//java/src/org/openqa/selenium/bidi/storage",
2728
"//java/src/org/openqa/selenium/json",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.module;
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import org.openqa.selenium.WebDriver;
23+
import org.openqa.selenium.bidi.BiDi;
24+
import org.openqa.selenium.bidi.Command;
25+
import org.openqa.selenium.bidi.HasBiDi;
26+
import org.openqa.selenium.bidi.permissions.PermissionState;
27+
import org.openqa.selenium.internal.Require;
28+
29+
public class Permission {
30+
31+
private final BiDi bidi;
32+
33+
public Permission(WebDriver driver) {
34+
Require.nonNull("WebDriver", driver);
35+
36+
if (!(driver instanceof HasBiDi)) {
37+
throw new IllegalArgumentException("WebDriver instance must support BiDi protocol");
38+
}
39+
40+
this.bidi = ((HasBiDi) driver).getBiDi();
41+
}
42+
43+
public void setPermission(
44+
Map<String, String> permissionDescriptor, PermissionState state, String origin) {
45+
this.setPermission(permissionDescriptor, state, origin, null);
46+
}
47+
48+
public void setPermission(
49+
Map<String, String> permissionDescriptor,
50+
PermissionState state,
51+
String origin,
52+
String userContext) {
53+
Require.nonNull("Permission descriptor", permissionDescriptor);
54+
Require.nonNull("Permission state", state);
55+
Require.nonNull("Origin", origin);
56+
57+
Map<String, Object> params =
58+
new HashMap<>(
59+
Map.of(
60+
"descriptor", permissionDescriptor,
61+
"state", state.toString(),
62+
"origin", origin));
63+
64+
if (userContext != null) {
65+
params.put("userContext", userContext);
66+
}
67+
68+
this.bidi.send(new Command<>("permissions.setPermission", params));
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
load("//java:defs.bzl", "java_library")
2+
3+
java_library(
4+
name = "permissions",
5+
srcs = glob(
6+
[
7+
"*.java",
8+
],
9+
),
10+
visibility = [
11+
"//java/src/org/openqa/selenium/bidi:__subpackages__",
12+
"//java/src/org/openqa/selenium/firefox:__subpackages__",
13+
"//java/src/org/openqa/selenium/remote:__pkg__",
14+
"//java/test/org/openqa/selenium/bidi:__subpackages__",
15+
"//java/test/org/openqa/selenium/grid:__subpackages__",
16+
],
17+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.permissions;
19+
20+
public enum PermissionState {
21+
GRANTED("granted"),
22+
DENIED("denied"),
23+
PROMPT("prompt");
24+
25+
private final String state;
26+
27+
PermissionState(String state) {
28+
this.state = state;
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return state;
34+
}
35+
36+
public static PermissionState findByName(String name) {
37+
PermissionState result = null;
38+
for (PermissionState state : values()) {
39+
if (state.toString().equalsIgnoreCase(name)) {
40+
result = state;
41+
break;
42+
}
43+
}
44+
return result;
45+
}
46+
}

java/src/org/openqa/selenium/remote/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ java_export(
3131
"//java/src/org/openqa/selenium/bidi/log",
3232
"//java/src/org/openqa/selenium/bidi/module",
3333
"//java/src/org/openqa/selenium/bidi/network",
34+
"//java/src/org/openqa/selenium/bidi/permissions",
3435
"//java/src/org/openqa/selenium/bidi/script",
3536
"//java/src/org/openqa/selenium/bidi/storage",
3637
"//java/src/org/openqa/selenium/devtools",

java/test/org/openqa/selenium/bidi/input/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ java_selenium_test_suite(
2222
"//java/src/org/openqa/selenium/bidi/log",
2323
"//java/src/org/openqa/selenium/bidi/module",
2424
"//java/src/org/openqa/selenium/bidi/network",
25+
"//java/src/org/openqa/selenium/bidi/permissions",
2526
"//java/src/org/openqa/selenium/bidi/script",
2627
"//java/src/org/openqa/selenium/chrome",
2728
"//java/src/org/openqa/selenium/firefox",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
load("@rules_jvm_external//:defs.bzl", "artifact")
2+
load("//java:defs.bzl", "JUNIT5_DEPS", "java_selenium_test_suite")
3+
4+
java_selenium_test_suite(
5+
name = "large-tests",
6+
size = "large",
7+
srcs = glob(["*Test.java"]),
8+
browsers = [
9+
"firefox",
10+
],
11+
data = [
12+
"//third_party/chrome_ext:backspace.crx",
13+
],
14+
tags = [
15+
"selenium-remote",
16+
],
17+
deps = [
18+
"//java/src/org/openqa/selenium/bidi",
19+
"//java/src/org/openqa/selenium/bidi/browsingcontext",
20+
"//java/src/org/openqa/selenium/bidi/log",
21+
"//java/src/org/openqa/selenium/bidi/module",
22+
"//java/src/org/openqa/selenium/bidi/network",
23+
"//java/src/org/openqa/selenium/bidi/script",
24+
"//java/src/org/openqa/selenium/chrome",
25+
"//java/src/org/openqa/selenium/firefox",
26+
"//java/src/org/openqa/selenium/json",
27+
"//java/src/org/openqa/selenium/remote",
28+
"//java/src/org/openqa/selenium/support",
29+
"//java/test/org/openqa/selenium:helpers",
30+
"//java/test/org/openqa/selenium/build",
31+
"//java/test/org/openqa/selenium/environment",
32+
"//java/test/org/openqa/selenium/testing:annotations",
33+
"//java/test/org/openqa/selenium/testing:test-base",
34+
"//java/test/org/openqa/selenium/testing/drivers",
35+
artifact("org.junit.jupiter:junit-jupiter-api"),
36+
artifact("org.assertj:assertj-core"),
37+
artifact("org.mockito:mockito-core"),
38+
] + JUNIT5_DEPS,
39+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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.permissions;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
import java.util.Map;
23+
import java.util.Optional;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
26+
import org.openqa.selenium.WindowType;
27+
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
28+
import org.openqa.selenium.bidi.browsingcontext.CreateContextParameters;
29+
import org.openqa.selenium.bidi.browsingcontext.ReadinessState;
30+
import org.openqa.selenium.bidi.module.Browser;
31+
import org.openqa.selenium.bidi.module.Permission;
32+
import org.openqa.selenium.bidi.module.Script;
33+
import org.openqa.selenium.bidi.script.EvaluateResult;
34+
import org.openqa.selenium.bidi.script.EvaluateResultSuccess;
35+
import org.openqa.selenium.testing.JupiterTestBase;
36+
import org.openqa.selenium.testing.NeedsFreshDriver;
37+
import org.openqa.selenium.testing.Pages;
38+
39+
public class PermissionsTest extends JupiterTestBase {
40+
private Permission permission;
41+
private Script script;
42+
43+
private static final String GET_GEOLOCATION_PERMISSION =
44+
"() => {return navigator.permissions.query({ name: 'geolocation' })"
45+
+ ".then(val => val.state, err => err.message)}";
46+
private static final String GET_ORIGIN = "() => {return window.location.origin;}";
47+
48+
@BeforeEach
49+
public void setUp() {
50+
permission = new Permission(driver);
51+
script = new Script(driver);
52+
}
53+
54+
@Test
55+
@NeedsFreshDriver
56+
public void canSetPermission() {
57+
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
58+
59+
context.navigate(new Pages(appServer).blankPage, ReadinessState.COMPLETE);
60+
61+
String contextId = context.getId();
62+
63+
EvaluateResult origin =
64+
script.callFunctionInBrowsingContext(
65+
contextId, GET_ORIGIN, true, Optional.empty(), Optional.empty(), Optional.empty());
66+
67+
String originValue = (String) ((EvaluateResultSuccess) origin).getResult().getValue().get();
68+
69+
permission.setPermission(Map.of("name", "geolocation"), PermissionState.GRANTED, originValue);
70+
71+
EvaluateResult result =
72+
script.callFunctionInBrowsingContext(
73+
contextId,
74+
GET_GEOLOCATION_PERMISSION,
75+
true,
76+
Optional.empty(),
77+
Optional.empty(),
78+
Optional.empty());
79+
80+
String resultValue = (String) ((EvaluateResultSuccess) result).getResult().getValue().get();
81+
assertThat(resultValue).isEqualTo("granted");
82+
83+
permission.setPermission(Map.of("name", "geolocation"), PermissionState.DENIED, originValue);
84+
85+
result =
86+
script.callFunctionInBrowsingContext(
87+
contextId,
88+
GET_GEOLOCATION_PERMISSION,
89+
true,
90+
Optional.empty(),
91+
Optional.empty(),
92+
Optional.empty());
93+
94+
resultValue = (String) ((EvaluateResultSuccess) result).getResult().getValue().get();
95+
assertThat(resultValue).isEqualTo("denied");
96+
97+
permission.setPermission(Map.of("name", "geolocation"), PermissionState.PROMPT, originValue);
98+
99+
result =
100+
script.callFunctionInBrowsingContext(
101+
contextId,
102+
GET_GEOLOCATION_PERMISSION,
103+
true,
104+
Optional.empty(),
105+
Optional.empty(),
106+
Optional.empty());
107+
108+
resultValue = (String) ((EvaluateResultSuccess) result).getResult().getValue().get();
109+
assertThat(resultValue).isEqualTo("prompt");
110+
}
111+
112+
@Test
113+
@NeedsFreshDriver
114+
public void canSetPermissionForAUserContext() {
115+
Browser browser = new Browser(driver);
116+
117+
String url = new Pages(appServer).blankPage;
118+
119+
String userContext = browser.createUserContext();
120+
121+
String originalTab = driver.getWindowHandle();
122+
123+
BrowsingContext context1 = new BrowsingContext(this.driver, driver.getWindowHandle());
124+
BrowsingContext context2 =
125+
new BrowsingContext(
126+
this.driver, new CreateContextParameters(WindowType.TAB).userContext(userContext));
127+
128+
String newTab = context2.getId();
129+
130+
context1.navigate(url, ReadinessState.COMPLETE);
131+
context2.navigate(url, ReadinessState.COMPLETE);
132+
133+
EvaluateResult origin =
134+
script.callFunctionInBrowsingContext(
135+
originalTab, GET_ORIGIN, true, Optional.empty(), Optional.empty(), Optional.empty());
136+
137+
String originValue = (String) ((EvaluateResultSuccess) origin).getResult().getValue().get();
138+
139+
permission.setPermission(
140+
Map.of("name", "geolocation"), PermissionState.GRANTED, originValue, userContext);
141+
142+
EvaluateResult newTabUpdatedPermission =
143+
script.callFunctionInBrowsingContext(
144+
newTab,
145+
GET_GEOLOCATION_PERMISSION,
146+
true,
147+
Optional.empty(),
148+
Optional.empty(),
149+
Optional.empty());
150+
151+
String newTabUpdatedPermissionValue =
152+
(String) ((EvaluateResultSuccess) newTabUpdatedPermission).getResult().getValue().get();
153+
assertThat(newTabUpdatedPermissionValue).isEqualTo("granted");
154+
155+
BrowsingContext context3 =
156+
new BrowsingContext(
157+
this.driver, new CreateContextParameters(WindowType.TAB).userContext(userContext));
158+
159+
context3.navigate(url, ReadinessState.COMPLETE);
160+
161+
newTabUpdatedPermission =
162+
script.callFunctionInBrowsingContext(
163+
context3.getId(),
164+
GET_GEOLOCATION_PERMISSION,
165+
true,
166+
Optional.empty(),
167+
Optional.empty(),
168+
Optional.empty());
169+
170+
newTabUpdatedPermissionValue =
171+
(String) ((EvaluateResultSuccess) newTabUpdatedPermission).getResult().getValue().get();
172+
assertThat(newTabUpdatedPermissionValue).isEqualTo("granted");
173+
}
174+
}

0 commit comments

Comments
 (0)