Skip to content

Commit 52f1625

Browse files
committed
1 parent 214df71 commit 52f1625

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-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
@@ -41,6 +41,9 @@ public class Network implements AutoCloseable {
4141
private final Event<ResponseDetails> responseCompleted =
4242
new Event<>("network.responseStarted", ResponseDetails::fromJsonMap);
4343

44+
private final Event<ResponseDetails> authRequired =
45+
new Event<>("network.authRequired", ResponseDetails::fromJsonMap);
46+
4447
public Network(WebDriver driver) {
4548
this(new HashSet<>(), driver);
4649
}
@@ -85,10 +88,19 @@ public void onResponseCompleted(Consumer<ResponseDetails> consumer) {
8588
}
8689
}
8790

91+
public void onAuthRequired(Consumer<ResponseDetails> consumer) {
92+
if (browsingContextIds.isEmpty()) {
93+
this.bidi.addListener(authRequired, consumer);
94+
} else {
95+
this.bidi.addListener(browsingContextIds, authRequired, consumer);
96+
}
97+
}
98+
8899
@Override
89100
public void close() {
90101
this.bidi.clearListener(beforeRequestSentEvent);
91102
this.bidi.clearListener(responseStarted);
92103
this.bidi.clearListener(responseCompleted);
104+
this.bidi.clearListener(authRequired);
93105
}
94106
}

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

Lines changed: 26 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

@@ -143,6 +144,31 @@ void canListenToResponseCompletedEventWithCookie()
143144
}
144145
}
145146

147+
@Test
148+
@NotYetImplemented(SAFARI)
149+
@NotYetImplemented(IE)
150+
@NotYetImplemented(EDGE)
151+
@NotYetImplemented(FIREFOX) // Implemented in Firefox Nightly version 123
152+
void canListenToOnAuthRequiredEvent()
153+
throws ExecutionException, InterruptedException, TimeoutException {
154+
try (Network network = new Network(driver)) {
155+
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
156+
network.onAuthRequired(future::complete);
157+
page = server.whereIs("basicAuth");
158+
driver.get(page);
159+
160+
ResponseDetails response = future.get(5, TimeUnit.SECONDS);
161+
String windowHandle = driver.getWindowHandle();
162+
assertThat(response.getBrowsingContextId()).isEqualTo(windowHandle);
163+
assertThat(response.getRequest().getRequestId()).isNotNull();
164+
assertThat(response.getRequest().getMethod()).isEqualToIgnoringCase("get");
165+
assertThat(response.getRequest().getUrl()).isNotNull();
166+
assertThat(response.getResponseData().getHeaders().size()).isGreaterThanOrEqualTo(1);
167+
assertThat(response.getResponseData().getUrl()).contains("basicAuth");
168+
assertThat(response.getResponseData().getStatus()).isEqualTo(401L);
169+
}
170+
}
171+
146172
@AfterEach
147173
public void quitDriver() {
148174
if (driver != null) {

0 commit comments

Comments
 (0)