Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions java/src/org/openqa/selenium/bidi/module/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<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
@@ -0,0 +1,48 @@
// 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<String> contexts;

public SetCacheBehaviorParameters(CacheBehavior cacheBehavior) {
this(cacheBehavior, null);
}

public SetCacheBehaviorParameters(CacheBehavior cacheBehavior, List<String> contexts) {
this.cacheBehavior = Require.nonNull("Cache behavior", cacheBehavior);
this.contexts = contexts;
}

public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("cacheBehavior", cacheBehavior.toString());

if (contexts != null && !contexts.isEmpty()) {
map.put("contexts", contexts);
}

return map;
}
}
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,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");
}
}
}
Loading