Skip to content

Commit 03b4c25

Browse files
authored
[java][BiDi] implement emulation.setScriptingEnabled (#16631)
1 parent d764938 commit 03b4c25

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed

java/src/org/openqa/selenium/bidi/emulation/Emulation.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ public Map<String, Object> setTimezoneOverride(SetTimezoneOverrideParameters par
4949

5050
return bidi.send(new Command<>("emulation.setTimezoneOverride", parameters.toMap(), Map.class));
5151
}
52+
53+
public Map<String, Object> setScriptingEnabled(SetScriptingEnabledParameters parameters) {
54+
Require.nonNull("SetScriptingEnabled parameters", parameters);
55+
56+
return bidi.send(new Command<>("emulation.setScriptingEnabled", parameters.toMap(), Map.class));
57+
}
5258
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.emulation;
19+
20+
public class SetScriptingEnabledParameters extends AbstractOverrideParameters {
21+
public SetScriptingEnabledParameters(Boolean enabled) {
22+
if (Boolean.TRUE.equals(enabled)) {
23+
throw new IllegalArgumentException(
24+
"Only emulation of disabled JavaScript is supported (enabled must be false or null)");
25+
}
26+
map.put("enabled", enabled); // null or false
27+
}
28+
29+
@Override
30+
public SetScriptingEnabledParameters contexts(java.util.List<String> contexts) {
31+
super.contexts(contexts);
32+
return this;
33+
}
34+
35+
@Override
36+
public SetScriptingEnabledParameters userContexts(java.util.List<String> userContexts) {
37+
super.userContexts(userContexts);
38+
return this;
39+
}
40+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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.emulation;
19+
20+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
21+
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
22+
23+
import java.util.List;
24+
import java.util.Optional;
25+
import org.junit.jupiter.api.Test;
26+
import org.openqa.selenium.By;
27+
import org.openqa.selenium.WebElement;
28+
import org.openqa.selenium.WindowType;
29+
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
30+
import org.openqa.selenium.bidi.browsingcontext.CreateContextParameters;
31+
import org.openqa.selenium.bidi.browsingcontext.ReadinessState;
32+
import org.openqa.selenium.bidi.module.Browser;
33+
import org.openqa.selenium.bidi.module.Script;
34+
import org.openqa.selenium.bidi.script.EvaluateResult;
35+
import org.openqa.selenium.bidi.script.EvaluateResultSuccess;
36+
import org.openqa.selenium.testing.Ignore;
37+
import org.openqa.selenium.testing.JupiterTestBase;
38+
import org.openqa.selenium.testing.NeedsFreshDriver;
39+
40+
public class SetScriptingEnabledTest extends JupiterTestBase {
41+
42+
private boolean isFooInWindow(String contextId, Script script) {
43+
EvaluateResult result =
44+
script.evaluateFunctionInBrowsingContext(
45+
contextId, "'foo' in window", false, Optional.empty());
46+
return (Boolean) ((EvaluateResultSuccess) result).getResult().getValue().get();
47+
}
48+
49+
@Test
50+
@NeedsFreshDriver
51+
@Ignore(FIREFOX)
52+
void canSetScriptingEnabledWithContexts() {
53+
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
54+
String contextId = context.getId();
55+
56+
Emulation emulation = new Emulation(driver);
57+
Script script = new Script(driver);
58+
59+
emulation.setScriptingEnabled(
60+
new SetScriptingEnabledParameters(false).contexts(List.of(contextId)));
61+
62+
context.navigate("data:text/html,<script>window.foo=123;</script>", ReadinessState.COMPLETE);
63+
64+
assertThat(isFooInWindow(contextId, script)).isFalse();
65+
66+
emulation.setScriptingEnabled(
67+
new SetScriptingEnabledParameters(null).contexts(List.of(contextId)));
68+
69+
context.navigate("data:text/html,<script>window.foo=123;</script>", ReadinessState.COMPLETE);
70+
71+
assertThat(isFooInWindow(contextId, script)).isTrue();
72+
}
73+
74+
@Test
75+
@NeedsFreshDriver
76+
@Ignore(FIREFOX)
77+
void canSetScriptingEnabledWithUserContexts() {
78+
Browser browser = new Browser(driver);
79+
String userContext = browser.createUserContext();
80+
BrowsingContext context =
81+
new BrowsingContext(
82+
driver, new CreateContextParameters(WindowType.TAB).userContext(userContext));
83+
String contextId = context.getId();
84+
85+
driver.switchTo().window(contextId);
86+
87+
Emulation emulation = new Emulation(driver);
88+
emulation.setScriptingEnabled(
89+
new SetScriptingEnabledParameters(false).userContexts(List.of(userContext)));
90+
91+
String url = appServer.whereIs("javascriptPage.html");
92+
context.navigate(url, ReadinessState.COMPLETE);
93+
94+
// Check that inline event handlers don't work; this page has an onclick handler
95+
WebElement clickField = driver.findElement(By.id("clickField"));
96+
String initialValue = clickField.getAttribute("value"); // initial value is 'Hello'
97+
clickField.click();
98+
99+
// Get the value after click, it should remain unchanged if scripting is disabled
100+
Script script = new Script(driver);
101+
EvaluateResult result =
102+
script.evaluateFunctionInBrowsingContext(
103+
contextId, "document.getElementById('clickField').value", false, Optional.empty());
104+
105+
String resultValue = ((EvaluateResultSuccess) result).getResult().getValue().get().toString();
106+
assertThat(resultValue).isEqualTo(initialValue);
107+
108+
// Clear the scripting override
109+
emulation.setScriptingEnabled(
110+
new SetScriptingEnabledParameters(null).userContexts(List.of(userContext)));
111+
112+
context.navigate(url, ReadinessState.COMPLETE);
113+
114+
// Click the element again, it should change to 'Clicked' now
115+
driver.findElement(By.id("clickField")).click();
116+
EvaluateResult result2 =
117+
script.evaluateFunctionInBrowsingContext(
118+
contextId, "document.getElementById('clickField').value", false, Optional.empty());
119+
120+
String resultValue2 = ((EvaluateResultSuccess) result2).getResult().getValue().get().toString();
121+
assertThat(resultValue2).isEqualTo("Clicked");
122+
123+
browser.removeUserContext(userContext);
124+
}
125+
}

0 commit comments

Comments
 (0)