From c83de447590a97f5ee46a59a503b70e2c13fe5c0 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Wed, 22 Jan 2025 12:37:23 +0530 Subject: [PATCH 1/4] implement BiDi `setCacheBehavior` for java bindings --- .../openqa/selenium/bidi/module/Network.java | 5 ++ .../selenium/bidi/network/CacheBehavior.java | 34 ++++++++++++++ .../network/SetCacheBehaviorParameters.java | 46 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 java/src/org/openqa/selenium/bidi/network/CacheBehavior.java create mode 100644 java/src/org/openqa/selenium/bidi/network/SetCacheBehaviorParameters.java diff --git a/java/src/org/openqa/selenium/bidi/module/Network.java b/java/src/org/openqa/selenium/bidi/module/Network.java index 1237fceeb5023..46641bc60b6cb 100644 --- a/java/src/org/openqa/selenium/bidi/module/Network.java +++ b/java/src/org/openqa/selenium/bidi/module/Network.java @@ -35,6 +35,7 @@ import org.openqa.selenium.bidi.network.FetchError; import org.openqa.selenium.bidi.network.ProvideResponseParameters; import org.openqa.selenium.bidi.network.ResponseDetails; +import org.openqa.selenium.bidi.network.SetCacheBehaviorParameters; import org.openqa.selenium.internal.Require; public class Network implements AutoCloseable { @@ -137,6 +138,10 @@ public void provideResponse(ProvideResponseParameters parameters) { this.bidi.send(new Command<>("network.provideResponse", parameters.toMap())); } + public void setCacheBehavior(SetCacheBehaviorParameters parameters) { + this.bidi.send(new Command<>("network.setCacheBehavior", parameters.toMap())); + } + public void onBeforeRequestSent(Consumer consumer) { if (browsingContextIds.isEmpty()) { this.bidi.addListener(beforeRequestSentEvent, consumer); diff --git a/java/src/org/openqa/selenium/bidi/network/CacheBehavior.java b/java/src/org/openqa/selenium/bidi/network/CacheBehavior.java new file mode 100644 index 0000000000000..314992859c4a8 --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/network/CacheBehavior.java @@ -0,0 +1,34 @@ +// 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.network; + +public enum CacheBehavior { + DEFAULT("default"), + BYPASS("bypass"); + + private final String behavior; + + CacheBehavior(String behavior) { + this.behavior = behavior; + } + + @Override + public String toString() { + return behavior; + } +} diff --git a/java/src/org/openqa/selenium/bidi/network/SetCacheBehaviorParameters.java b/java/src/org/openqa/selenium/bidi/network/SetCacheBehaviorParameters.java new file mode 100644 index 0000000000000..ca1c9388e2fcc --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/network/SetCacheBehaviorParameters.java @@ -0,0 +1,46 @@ +// 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.network; + +import java.util.List; +import java.util.Map; +import org.openqa.selenium.internal.Require; + +public class SetCacheBehaviorParameters { + private final CacheBehavior cacheBehavior; + private final List contexts; + + public SetCacheBehaviorParameters(CacheBehavior cacheBehavior) { + this(cacheBehavior, null); + } + + public SetCacheBehaviorParameters(CacheBehavior cacheBehavior, List contexts) { + this.cacheBehavior = Require.nonNull("Cache behavior", cacheBehavior); + this.contexts = contexts; + } + + public Map toMap() { + Map map = Map.of("cacheBehavior", cacheBehavior.toString()); + + if (contexts != null && !contexts.isEmpty()) { + return Map.of("cacheBehavior", cacheBehavior.toString(), "contexts", contexts); + } + + return map; + } +} From db6123e02168171cc830bbff3c01e3de6f7a3b91 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Wed, 22 Jan 2025 12:38:03 +0530 Subject: [PATCH 2/4] add tests for `setCacheBehavior` --- .../bidi/network/NetworkCommandsTest.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java b/java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java index cb7c065bb49ba..59d3696b8cfb6 100644 --- a/java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java +++ b/java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java @@ -23,6 +23,7 @@ import java.time.Duration; import java.time.temporal.ChronoUnit; +import java.util.Collections; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.Disabled; @@ -32,6 +33,9 @@ import org.openqa.selenium.TimeoutException; import org.openqa.selenium.UsernameAndPassword; import org.openqa.selenium.WebDriverException; +import org.openqa.selenium.WindowType; +import org.openqa.selenium.bidi.BiDiException; +import org.openqa.selenium.bidi.browsingcontext.BrowsingContext; import org.openqa.selenium.bidi.module.Network; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.testing.JupiterTestBase; @@ -264,4 +268,61 @@ void canFailRequest() { assertThatThrownBy(() -> driver.get(page)).isInstanceOf(WebDriverException.class); } } + + @Test + @NeedsFreshDriver + void canSetCacheBehaviorToBypass() { + try (Network network = new Network(driver)) { + page = appServer.whereIs("basicAuth"); + + BrowsingContext context = new BrowsingContext(driver, WindowType.TAB); + String contextId = context.getId(); + + network.setCacheBehavior( + new SetCacheBehaviorParameters( + CacheBehavior.BYPASS, Collections.singletonList(contextId))); + } + } + + @Test + @NeedsFreshDriver + void canSetCacheBehaviorToDefault() { + try (Network network = new Network(driver)) { + page = appServer.whereIs("basicAuth"); + + BrowsingContext context = new BrowsingContext(driver, WindowType.TAB); + String contextId = context.getId(); + + network.setCacheBehavior( + new SetCacheBehaviorParameters( + CacheBehavior.DEFAULT, Collections.singletonList(contextId))); + } + } + + @Test + @NeedsFreshDriver + void canSetCacheBehaviorWithNoContextId() { + try (Network network = new Network(driver)) { + page = appServer.whereIs("basicAuth"); + + network.setCacheBehavior(new SetCacheBehaviorParameters(CacheBehavior.BYPASS)); + network.setCacheBehavior(new SetCacheBehaviorParameters(CacheBehavior.DEFAULT)); + } + } + + @Test + @NeedsFreshDriver + void throwsExceptionForInvalidContext() { + try (Network network = new Network(driver)) { + page = appServer.whereIs("basicAuth"); + + assertThatThrownBy( + () -> + network.setCacheBehavior( + new SetCacheBehaviorParameters( + CacheBehavior.DEFAULT, Collections.singletonList("invalid-context")))) + .isInstanceOf(BiDiException.class) + .hasMessageContaining("no such frame"); + } + } } From 6fe0c009489551c0980fd4769bb32e3d12059061 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Wed, 22 Jan 2025 12:57:03 +0530 Subject: [PATCH 3/4] optimize map creation --- .../selenium/bidi/network/SetCacheBehaviorParameters.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/java/src/org/openqa/selenium/bidi/network/SetCacheBehaviorParameters.java b/java/src/org/openqa/selenium/bidi/network/SetCacheBehaviorParameters.java index ca1c9388e2fcc..591ad42db93af 100644 --- a/java/src/org/openqa/selenium/bidi/network/SetCacheBehaviorParameters.java +++ b/java/src/org/openqa/selenium/bidi/network/SetCacheBehaviorParameters.java @@ -17,6 +17,7 @@ package org.openqa.selenium.bidi.network; +import java.util.HashMap; import java.util.List; import java.util.Map; import org.openqa.selenium.internal.Require; @@ -35,10 +36,11 @@ public SetCacheBehaviorParameters(CacheBehavior cacheBehavior, List cont } public Map toMap() { - Map map = Map.of("cacheBehavior", cacheBehavior.toString()); + Map map = new HashMap<>(); + map.put("cacheBehavior", cacheBehavior.toString()); if (contexts != null && !contexts.isEmpty()) { - return Map.of("cacheBehavior", cacheBehavior.toString(), "contexts", contexts); + map.put("contexts", contexts); } return map; From 83ed37da1e3c237f839007cb78008da4ae4bda3b Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Wed, 22 Jan 2025 16:22:24 +0530 Subject: [PATCH 4/4] remove `SetCacheBehaviorParameters` class --- .../openqa/selenium/bidi/module/Network.java | 19 ++++++-- .../network/SetCacheBehaviorParameters.java | 48 ------------------- .../bidi/network/NetworkCommandsTest.java | 15 ++---- 3 files changed, 21 insertions(+), 61 deletions(-) delete mode 100644 java/src/org/openqa/selenium/bidi/network/SetCacheBehaviorParameters.java diff --git a/java/src/org/openqa/selenium/bidi/module/Network.java b/java/src/org/openqa/selenium/bidi/module/Network.java index 46641bc60b6cb..c295009d39c93 100644 --- a/java/src/org/openqa/selenium/bidi/module/Network.java +++ b/java/src/org/openqa/selenium/bidi/module/Network.java @@ -19,6 +19,7 @@ import java.util.Collections; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Consumer; @@ -30,12 +31,12 @@ import org.openqa.selenium.bidi.HasBiDi; import org.openqa.selenium.bidi.network.AddInterceptParameters; import org.openqa.selenium.bidi.network.BeforeRequestSent; +import org.openqa.selenium.bidi.network.CacheBehavior; import org.openqa.selenium.bidi.network.ContinueRequestParameters; import org.openqa.selenium.bidi.network.ContinueResponseParameters; import org.openqa.selenium.bidi.network.FetchError; import org.openqa.selenium.bidi.network.ProvideResponseParameters; import org.openqa.selenium.bidi.network.ResponseDetails; -import org.openqa.selenium.bidi.network.SetCacheBehaviorParameters; import org.openqa.selenium.internal.Require; public class Network implements AutoCloseable { @@ -138,8 +139,20 @@ public void provideResponse(ProvideResponseParameters parameters) { this.bidi.send(new Command<>("network.provideResponse", parameters.toMap())); } - public void setCacheBehavior(SetCacheBehaviorParameters parameters) { - this.bidi.send(new Command<>("network.setCacheBehavior", parameters.toMap())); + public void setCacheBehavior(CacheBehavior cacheBehavior) { + Require.nonNull("Cache behavior", cacheBehavior); + this.bidi.send( + new Command<>( + "network.setCacheBehavior", Map.of("cacheBehavior", cacheBehavior.toString()))); + } + + public void setCacheBehavior(CacheBehavior cacheBehavior, List contexts) { + Require.nonNull("Cache behavior", cacheBehavior); + Require.nonNull("Contexts", contexts); + this.bidi.send( + new Command<>( + "network.setCacheBehavior", + Map.of("cacheBehavior", cacheBehavior.toString(), "contexts", contexts))); } public void onBeforeRequestSent(Consumer consumer) { diff --git a/java/src/org/openqa/selenium/bidi/network/SetCacheBehaviorParameters.java b/java/src/org/openqa/selenium/bidi/network/SetCacheBehaviorParameters.java deleted file mode 100644 index 591ad42db93af..0000000000000 --- a/java/src/org/openqa/selenium/bidi/network/SetCacheBehaviorParameters.java +++ /dev/null @@ -1,48 +0,0 @@ -// 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.network; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.openqa.selenium.internal.Require; - -public class SetCacheBehaviorParameters { - private final CacheBehavior cacheBehavior; - private final List contexts; - - public SetCacheBehaviorParameters(CacheBehavior cacheBehavior) { - this(cacheBehavior, null); - } - - public SetCacheBehaviorParameters(CacheBehavior cacheBehavior, List contexts) { - this.cacheBehavior = Require.nonNull("Cache behavior", cacheBehavior); - this.contexts = contexts; - } - - public Map toMap() { - Map map = new HashMap<>(); - map.put("cacheBehavior", cacheBehavior.toString()); - - if (contexts != null && !contexts.isEmpty()) { - map.put("contexts", contexts); - } - - return map; - } -} diff --git a/java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java b/java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java index 59d3696b8cfb6..d60b4e367ec0c 100644 --- a/java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java +++ b/java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java @@ -278,9 +278,7 @@ void canSetCacheBehaviorToBypass() { BrowsingContext context = new BrowsingContext(driver, WindowType.TAB); String contextId = context.getId(); - network.setCacheBehavior( - new SetCacheBehaviorParameters( - CacheBehavior.BYPASS, Collections.singletonList(contextId))); + network.setCacheBehavior(CacheBehavior.BYPASS, Collections.singletonList(contextId)); } } @@ -293,9 +291,7 @@ void canSetCacheBehaviorToDefault() { BrowsingContext context = new BrowsingContext(driver, WindowType.TAB); String contextId = context.getId(); - network.setCacheBehavior( - new SetCacheBehaviorParameters( - CacheBehavior.DEFAULT, Collections.singletonList(contextId))); + network.setCacheBehavior(CacheBehavior.DEFAULT, Collections.singletonList(contextId)); } } @@ -305,8 +301,8 @@ void canSetCacheBehaviorWithNoContextId() { try (Network network = new Network(driver)) { page = appServer.whereIs("basicAuth"); - network.setCacheBehavior(new SetCacheBehaviorParameters(CacheBehavior.BYPASS)); - network.setCacheBehavior(new SetCacheBehaviorParameters(CacheBehavior.DEFAULT)); + network.setCacheBehavior(CacheBehavior.BYPASS); + network.setCacheBehavior(CacheBehavior.DEFAULT); } } @@ -319,8 +315,7 @@ void throwsExceptionForInvalidContext() { assertThatThrownBy( () -> network.setCacheBehavior( - new SetCacheBehaviorParameters( - CacheBehavior.DEFAULT, Collections.singletonList("invalid-context")))) + CacheBehavior.DEFAULT, Collections.singletonList("invalid-context"))) .isInstanceOf(BiDiException.class) .hasMessageContaining("no such frame"); }