Skip to content

Commit 2154194

Browse files
navin772gryznar
authored andcommitted
[java][bidi]: implement bidi setCacheBehavior (SeleniumHQ#15130)
1 parent 00315ff commit 2154194

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

java/src/org/openqa/selenium/bidi/module/Network.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.Collections;
2121
import java.util.HashSet;
22+
import java.util.List;
2223
import java.util.Map;
2324
import java.util.Set;
2425
import java.util.function.Consumer;
@@ -30,6 +31,7 @@
3031
import org.openqa.selenium.bidi.HasBiDi;
3132
import org.openqa.selenium.bidi.network.AddInterceptParameters;
3233
import org.openqa.selenium.bidi.network.BeforeRequestSent;
34+
import org.openqa.selenium.bidi.network.CacheBehavior;
3335
import org.openqa.selenium.bidi.network.ContinueRequestParameters;
3436
import org.openqa.selenium.bidi.network.ContinueResponseParameters;
3537
import org.openqa.selenium.bidi.network.FetchError;
@@ -137,6 +139,22 @@ public void provideResponse(ProvideResponseParameters parameters) {
137139
this.bidi.send(new Command<>("network.provideResponse", parameters.toMap()));
138140
}
139141

142+
public void setCacheBehavior(CacheBehavior cacheBehavior) {
143+
Require.nonNull("Cache behavior", cacheBehavior);
144+
this.bidi.send(
145+
new Command<>(
146+
"network.setCacheBehavior", Map.of("cacheBehavior", cacheBehavior.toString())));
147+
}
148+
149+
public void setCacheBehavior(CacheBehavior cacheBehavior, List<String> contexts) {
150+
Require.nonNull("Cache behavior", cacheBehavior);
151+
Require.nonNull("Contexts", contexts);
152+
this.bidi.send(
153+
new Command<>(
154+
"network.setCacheBehavior",
155+
Map.of("cacheBehavior", cacheBehavior.toString(), "contexts", contexts)));
156+
}
157+
140158
public void onBeforeRequestSent(Consumer<BeforeRequestSent> consumer) {
141159
if (browsingContextIds.isEmpty()) {
142160
this.bidi.addListener(beforeRequestSentEvent, consumer);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.network;
19+
20+
public enum CacheBehavior {
21+
DEFAULT("default"),
22+
BYPASS("bypass");
23+
24+
private final String behavior;
25+
26+
CacheBehavior(String behavior) {
27+
this.behavior = behavior;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return behavior;
33+
}
34+
}

java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.time.Duration;
2525
import java.time.temporal.ChronoUnit;
26+
import java.util.Collections;
2627
import java.util.concurrent.CountDownLatch;
2728
import java.util.concurrent.TimeUnit;
2829
import org.junit.jupiter.api.Disabled;
@@ -32,6 +33,9 @@
3233
import org.openqa.selenium.TimeoutException;
3334
import org.openqa.selenium.UsernameAndPassword;
3435
import org.openqa.selenium.WebDriverException;
36+
import org.openqa.selenium.WindowType;
37+
import org.openqa.selenium.bidi.BiDiException;
38+
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
3539
import org.openqa.selenium.bidi.module.Network;
3640
import org.openqa.selenium.support.ui.ExpectedConditions;
3741
import org.openqa.selenium.testing.JupiterTestBase;
@@ -264,4 +268,56 @@ void canFailRequest() {
264268
assertThatThrownBy(() -> driver.get(page)).isInstanceOf(WebDriverException.class);
265269
}
266270
}
271+
272+
@Test
273+
@NeedsFreshDriver
274+
void canSetCacheBehaviorToBypass() {
275+
try (Network network = new Network(driver)) {
276+
page = appServer.whereIs("basicAuth");
277+
278+
BrowsingContext context = new BrowsingContext(driver, WindowType.TAB);
279+
String contextId = context.getId();
280+
281+
network.setCacheBehavior(CacheBehavior.BYPASS, Collections.singletonList(contextId));
282+
}
283+
}
284+
285+
@Test
286+
@NeedsFreshDriver
287+
void canSetCacheBehaviorToDefault() {
288+
try (Network network = new Network(driver)) {
289+
page = appServer.whereIs("basicAuth");
290+
291+
BrowsingContext context = new BrowsingContext(driver, WindowType.TAB);
292+
String contextId = context.getId();
293+
294+
network.setCacheBehavior(CacheBehavior.DEFAULT, Collections.singletonList(contextId));
295+
}
296+
}
297+
298+
@Test
299+
@NeedsFreshDriver
300+
void canSetCacheBehaviorWithNoContextId() {
301+
try (Network network = new Network(driver)) {
302+
page = appServer.whereIs("basicAuth");
303+
304+
network.setCacheBehavior(CacheBehavior.BYPASS);
305+
network.setCacheBehavior(CacheBehavior.DEFAULT);
306+
}
307+
}
308+
309+
@Test
310+
@NeedsFreshDriver
311+
void throwsExceptionForInvalidContext() {
312+
try (Network network = new Network(driver)) {
313+
page = appServer.whereIs("basicAuth");
314+
315+
assertThatThrownBy(
316+
() ->
317+
network.setCacheBehavior(
318+
CacheBehavior.DEFAULT, Collections.singletonList("invalid-context")))
319+
.isInstanceOf(BiDiException.class)
320+
.hasMessageContaining("no such frame");
321+
}
322+
}
267323
}

0 commit comments

Comments
 (0)