Skip to content

Commit 0b22bc8

Browse files
committed
[bidi][java] Add 'fetchError' event
1 parent 7971007 commit 0b22bc8

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.openqa.selenium.WebDriver;
2727
import org.openqa.selenium.bidi.network.AddInterceptParameters;
2828
import org.openqa.selenium.bidi.network.BeforeRequestSent;
29+
import org.openqa.selenium.bidi.network.FetchError;
2930
import org.openqa.selenium.bidi.network.ResponseDetails;
3031
import org.openqa.selenium.internal.Require;
3132

@@ -38,6 +39,9 @@ public class Network implements AutoCloseable {
3839
private final Event<BeforeRequestSent> beforeRequestSentEvent =
3940
new Event<>("network.beforeRequestSent", BeforeRequestSent::fromJsonMap);
4041

42+
private final Event<FetchError> fetchErrorEvent =
43+
new Event<>("network.fetchError", FetchError::fromJsonMap);
44+
4145
private final Event<ResponseDetails> responseStarted =
4246
new Event<>("network.responseStarted", ResponseDetails::fromJsonMap);
4347

@@ -122,6 +126,14 @@ public void onBeforeRequestSent(Consumer<BeforeRequestSent> consumer) {
122126
}
123127
}
124128

129+
public void onFetchError(Consumer<FetchError> consumer) {
130+
if (browsingContextIds.isEmpty()) {
131+
this.bidi.addListener(fetchErrorEvent, consumer);
132+
} else {
133+
this.bidi.addListener(browsingContextIds, fetchErrorEvent, consumer);
134+
}
135+
}
136+
125137
public void onResponseStarted(Consumer<ResponseDetails> consumer) {
126138
if (browsingContextIds.isEmpty()) {
127139
this.bidi.addListener(responseStarted, consumer);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
import java.io.StringReader;
21+
import java.util.Map;
22+
import org.openqa.selenium.json.Json;
23+
import org.openqa.selenium.json.JsonInput;
24+
25+
public class FetchError extends BaseParameters {
26+
private static final Json JSON = new Json();
27+
28+
private final String errorText;
29+
30+
private FetchError(BaseParameters baseParameters, String errorText) {
31+
super(
32+
baseParameters.getBrowsingContextId(),
33+
baseParameters.isBlocked(),
34+
baseParameters.getNavigationId(),
35+
baseParameters.getRedirectCount(),
36+
baseParameters.getRequest(),
37+
baseParameters.getTimestamp(),
38+
baseParameters.getIntercepts());
39+
this.errorText = errorText;
40+
}
41+
42+
public static FetchError fromJsonMap(Map<String, Object> jsonMap) {
43+
try (StringReader baseParameterReader = new StringReader(JSON.toJson(jsonMap));
44+
JsonInput baseParamsInput = JSON.newInput(baseParameterReader); ) {
45+
String errorText = JSON.toJson(jsonMap.get("errorText"));
46+
return new FetchError(BaseParameters.fromJson(baseParamsInput), errorText);
47+
}
48+
}
49+
50+
public String getErrorText() {
51+
return errorText;
52+
}
53+
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
2121
import static org.openqa.selenium.testing.Safely.safelyCall;
2222
import static org.openqa.selenium.testing.drivers.Browser.EDGE;
23+
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
2324
import static org.openqa.selenium.testing.drivers.Browser.IE;
2425
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
2526

@@ -31,6 +32,7 @@
3132
import org.junit.jupiter.api.BeforeEach;
3233
import org.junit.jupiter.api.Test;
3334
import org.openqa.selenium.Cookie;
35+
import org.openqa.selenium.WebDriverException;
3436
import org.openqa.selenium.bidi.Network;
3537
import org.openqa.selenium.environment.webserver.AppServer;
3638
import org.openqa.selenium.environment.webserver.NettyAppServer;
@@ -167,6 +169,33 @@ void canListenToOnAuthRequiredEvent()
167169
}
168170
}
169171

172+
@Test
173+
@NotYetImplemented(SAFARI)
174+
@NotYetImplemented(IE)
175+
@NotYetImplemented(EDGE)
176+
@NotYetImplemented(FIREFOX)
177+
void canListenToFetchError() throws ExecutionException, InterruptedException, TimeoutException {
178+
try (Network network = new Network(driver)) {
179+
CompletableFuture<FetchError> future = new CompletableFuture<>();
180+
network.onFetchError(future::complete);
181+
page = server.whereIs("error");
182+
try {
183+
driver.get("https://not_a_valid_url.test/");
184+
} catch (WebDriverException ignored) {
185+
}
186+
187+
FetchError fetchError = future.get(5, TimeUnit.SECONDS);
188+
String windowHandle = driver.getWindowHandle();
189+
assertThat(fetchError.getBrowsingContextId()).isEqualTo(windowHandle);
190+
assertThat(fetchError.getRequest().getRequestId()).isNotNull();
191+
assertThat(fetchError.getRequest().getMethod()).isEqualToIgnoringCase("get");
192+
assertThat(fetchError.getRequest().getUrl()).contains("https://not_a_valid_url.test/");
193+
assertThat(fetchError.getRequest().getHeaders().size()).isGreaterThanOrEqualTo(1);
194+
assertThat(fetchError.getNavigationId()).isNotNull();
195+
assertThat(fetchError.getErrorText()).contains("UNKNOWN_HOST");
196+
}
197+
}
198+
170199
@AfterEach
171200
public void quitDriver() {
172201
if (driver != null) {

0 commit comments

Comments
 (0)