diff --git a/java/src/org/openqa/selenium/bidi/module/Network.java b/java/src/org/openqa/selenium/bidi/module/Network.java index 1237fceeb5023..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,6 +31,7 @@ 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; @@ -137,6 +139,22 @@ public void provideResponse(ProvideResponseParameters parameters) { this.bidi.send(new Command<>("network.provideResponse", 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) { 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/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java b/java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java index cb7c065bb49ba..d60b4e367ec0c 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,56 @@ 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(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(CacheBehavior.DEFAULT, Collections.singletonList(contextId)); + } + } + + @Test + @NeedsFreshDriver + void canSetCacheBehaviorWithNoContextId() { + try (Network network = new Network(driver)) { + page = appServer.whereIs("basicAuth"); + + network.setCacheBehavior(CacheBehavior.BYPASS); + network.setCacheBehavior(CacheBehavior.DEFAULT); + } + } + + @Test + @NeedsFreshDriver + void throwsExceptionForInvalidContext() { + try (Network network = new Network(driver)) { + page = appServer.whereIs("basicAuth"); + + assertThatThrownBy( + () -> + network.setCacheBehavior( + CacheBehavior.DEFAULT, Collections.singletonList("invalid-context"))) + .isInstanceOf(BiDiException.class) + .hasMessageContaining("no such frame"); + } + } }