Skip to content

[java][bidi]: implement bidi setCacheBehavior #15130

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 5 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions java/src/org/openqa/selenium/bidi/module/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String> 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<BeforeRequestSent> consumer) {
if (browsingContextIds.isEmpty()) {
this.bidi.addListener(beforeRequestSentEvent, consumer);
Expand Down
34 changes: 34 additions & 0 deletions java/src/org/openqa/selenium/bidi/network/CacheBehavior.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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");
}
}
}
Loading