-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[java][BiDi] implement emulation.setScriptingEnabled
#16631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0ceb2e1
[java][BiDi] implement `emulation.setScriptingEnabled`
Delta456 3e3fa85
Merge branch 'trunk' into emulation_setscript
navin772 cf1756d
refactor
Delta456 a7c98cc
fix spotbug
Delta456 e9e4022
Merge branch 'trunk' into emulation_setscript
navin772 13d21ed
Merge branch 'trunk' into emulation_setscript
Delta456 ab99703
Merge branch 'trunk' into emulation_setscript
Delta456 be4919d
Merge branch 'trunk' into emulation_setscript
Delta456 2756ef9
Merge branch 'trunk' into emulation_setscript
Delta456 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>Script Test Page</title> | ||
| </head> | ||
| <body> | ||
| <script>window.foo = 123;</script> | ||
| </body> | ||
| </html> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
java/src/org/openqa/selenium/bidi/emulation/SetScriptingEnabledParameters.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.emulation; | ||
|
|
||
| public class SetScriptingEnabledParameters extends AbstractOverrideParameters { | ||
| public SetScriptingEnabledParameters(Boolean enabled) { | ||
| // allow null | ||
| if (enabled == null) { | ||
| map.put("enabled", null); | ||
| } else if (!enabled) { | ||
| map.put("enabled", enabled); | ||
| } else { | ||
| throw new IllegalArgumentException( | ||
| "Only emulation of disabled JavaScript is supported (enabled must be false or null)"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public SetScriptingEnabledParameters contexts(java.util.List<String> contexts) { | ||
| super.contexts(contexts); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public SetScriptingEnabledParameters userContexts(java.util.List<String> userContexts) { | ||
| super.userContexts(userContexts); | ||
| return this; | ||
| } | ||
| } |
126 changes: 126 additions & 0 deletions
126
java/test/org/openqa/selenium/bidi/emulation/SetScriptingEnabledTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // 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.emulation; | ||
|
|
||
| import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
| import static org.openqa.selenium.testing.drivers.Browser.FIREFOX; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.openqa.selenium.By; | ||
| import org.openqa.selenium.WebElement; | ||
| import org.openqa.selenium.WindowType; | ||
| import org.openqa.selenium.bidi.browsingcontext.BrowsingContext; | ||
| import org.openqa.selenium.bidi.browsingcontext.CreateContextParameters; | ||
| import org.openqa.selenium.bidi.browsingcontext.ReadinessState; | ||
| import org.openqa.selenium.bidi.module.Browser; | ||
| import org.openqa.selenium.bidi.module.Script; | ||
| import org.openqa.selenium.bidi.script.EvaluateResult; | ||
| import org.openqa.selenium.bidi.script.EvaluateResultSuccess; | ||
| import org.openqa.selenium.testing.Ignore; | ||
| import org.openqa.selenium.testing.JupiterTestBase; | ||
| import org.openqa.selenium.testing.NeedsFreshDriver; | ||
|
|
||
| public class SetScriptingEnabledTest extends JupiterTestBase { | ||
|
|
||
| @Test | ||
| @NeedsFreshDriver | ||
| @Ignore(FIREFOX) | ||
| void canSetScriptingEnabledWithContexts() { | ||
| BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle()); | ||
| String contextId = context.getId(); | ||
|
|
||
| Emulation emulation = new Emulation(driver); | ||
| Script script = new Script(driver); | ||
|
|
||
| emulation.setScriptingEnabled( | ||
| new SetScriptingEnabledParameters(false).contexts(List.of(contextId))); | ||
|
|
||
| context.navigate(appServer.whereIs("script_page.html"), ReadinessState.COMPLETE); | ||
|
|
||
| EvaluateResult resultDisabled = | ||
| script.evaluateFunctionInBrowsingContext( | ||
| contextId, "'foo' in window", false, Optional.empty()); | ||
| Boolean valueDisabled = | ||
| (Boolean) ((EvaluateResultSuccess) resultDisabled).getResult().getValue().get(); | ||
| assertThat(valueDisabled).isFalse(); | ||
|
|
||
| emulation.setScriptingEnabled( | ||
| new SetScriptingEnabledParameters(null).contexts(List.of(contextId))); | ||
|
|
||
| context.navigate(appServer.whereIs("script_page.html"), ReadinessState.COMPLETE); | ||
|
|
||
| EvaluateResult resultEnabled = | ||
| script.evaluateFunctionInBrowsingContext( | ||
| contextId, "'foo' in window", false, Optional.empty()); | ||
| Boolean valueEnabled = | ||
| (Boolean) ((EvaluateResultSuccess) resultEnabled).getResult().getValue().get(); | ||
| assertThat(valueEnabled).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| @NeedsFreshDriver | ||
| @Ignore(FIREFOX) | ||
| void canSetScriptingEnabledWithUserContexts() { | ||
| Browser browser = new Browser(driver); | ||
| String userContext = browser.createUserContext(); | ||
| BrowsingContext context = | ||
| new BrowsingContext( | ||
| driver, new CreateContextParameters(WindowType.TAB).userContext(userContext)); | ||
| String contextId = context.getId(); | ||
|
|
||
| driver.switchTo().window(contextId); | ||
|
|
||
| Emulation emulation = new Emulation(driver); | ||
| emulation.setScriptingEnabled( | ||
| new SetScriptingEnabledParameters(false).userContexts(List.of(userContext))); | ||
|
|
||
| String url = appServer.whereIs("javascriptPage.html"); | ||
| context.navigate(url, ReadinessState.COMPLETE); | ||
|
|
||
| // Check that inline event handlers don't work; this page has an onclick handler | ||
| WebElement clickField = driver.findElement(By.id("clickField")); | ||
| String initialValue = clickField.getAttribute("value"); // initial value is 'Hello' | ||
| clickField.click(); | ||
|
|
||
| // Get the value after click, it should remain unchanged if scripting is disabled | ||
| Script script = new Script(driver); | ||
| EvaluateResult result = | ||
| script.evaluateFunctionInBrowsingContext( | ||
| contextId, "document.getElementById('clickField').value", false, Optional.empty()); | ||
|
|
||
| String resultValue = ((EvaluateResultSuccess) result).getResult().getValue().get().toString(); | ||
| assertThat(resultValue).isEqualTo(initialValue); | ||
|
|
||
| // Clear the scripting override | ||
| emulation.setScriptingEnabled( | ||
| new SetScriptingEnabledParameters(null).userContexts(List.of(userContext))); | ||
|
|
||
| context.navigate(url, ReadinessState.COMPLETE); | ||
|
|
||
| // Click the element again, it should change to 'Clicked' now | ||
| driver.findElement(By.id("clickField")).click(); | ||
| EvaluateResult result2 = | ||
| script.evaluateFunctionInBrowsingContext( | ||
| contextId, "document.getElementById('clickField').value", false, Optional.empty()); | ||
|
|
||
| String resultValue2 = ((EvaluateResultSuccess) result2).getResult().getValue().get().toString(); | ||
| assertThat(resultValue2).isEqualTo("Clicked"); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.