From 52f162593f90b04ef34c5a1549f19ecb0054e678 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Wed, 3 Jan 2024 17:31:12 +0530 Subject: [PATCH] [bidi][java] Add auth required event https://w3c.github.io/webdriver-bidi/#event-network-authRequired --- .../src/org/openqa/selenium/bidi/Network.java | 12 +++++++++ .../bidi/network/NetworkEventsTest.java | 26 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/java/src/org/openqa/selenium/bidi/Network.java b/java/src/org/openqa/selenium/bidi/Network.java index 1848e00ec9f01..4cba35cc180fc 100644 --- a/java/src/org/openqa/selenium/bidi/Network.java +++ b/java/src/org/openqa/selenium/bidi/Network.java @@ -41,6 +41,9 @@ public class Network implements AutoCloseable { private final Event responseCompleted = new Event<>("network.responseStarted", ResponseDetails::fromJsonMap); + private final Event authRequired = + new Event<>("network.authRequired", ResponseDetails::fromJsonMap); + public Network(WebDriver driver) { this(new HashSet<>(), driver); } @@ -85,10 +88,19 @@ public void onResponseCompleted(Consumer consumer) { } } + public void onAuthRequired(Consumer consumer) { + if (browsingContextIds.isEmpty()) { + this.bidi.addListener(authRequired, consumer); + } else { + this.bidi.addListener(browsingContextIds, authRequired, consumer); + } + } + @Override public void close() { this.bidi.clearListener(beforeRequestSentEvent); this.bidi.clearListener(responseStarted); this.bidi.clearListener(responseCompleted); + this.bidi.clearListener(authRequired); } } diff --git a/java/test/org/openqa/selenium/bidi/network/NetworkEventsTest.java b/java/test/org/openqa/selenium/bidi/network/NetworkEventsTest.java index 4451dda2c1e13..6f45915d4b299 100644 --- a/java/test/org/openqa/selenium/bidi/network/NetworkEventsTest.java +++ b/java/test/org/openqa/selenium/bidi/network/NetworkEventsTest.java @@ -20,6 +20,7 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.openqa.selenium.testing.Safely.safelyCall; import static org.openqa.selenium.testing.drivers.Browser.EDGE; +import static org.openqa.selenium.testing.drivers.Browser.FIREFOX; import static org.openqa.selenium.testing.drivers.Browser.IE; import static org.openqa.selenium.testing.drivers.Browser.SAFARI; @@ -143,6 +144,31 @@ void canListenToResponseCompletedEventWithCookie() } } + @Test + @NotYetImplemented(SAFARI) + @NotYetImplemented(IE) + @NotYetImplemented(EDGE) + @NotYetImplemented(FIREFOX) // Implemented in Firefox Nightly version 123 + void canListenToOnAuthRequiredEvent() + throws ExecutionException, InterruptedException, TimeoutException { + try (Network network = new Network(driver)) { + CompletableFuture future = new CompletableFuture<>(); + network.onAuthRequired(future::complete); + page = server.whereIs("basicAuth"); + driver.get(page); + + ResponseDetails response = future.get(5, TimeUnit.SECONDS); + String windowHandle = driver.getWindowHandle(); + assertThat(response.getBrowsingContextId()).isEqualTo(windowHandle); + assertThat(response.getRequest().getRequestId()).isNotNull(); + assertThat(response.getRequest().getMethod()).isEqualToIgnoringCase("get"); + assertThat(response.getRequest().getUrl()).isNotNull(); + assertThat(response.getResponseData().getHeaders().size()).isGreaterThanOrEqualTo(1); + assertThat(response.getResponseData().getUrl()).contains("basicAuth"); + assertThat(response.getResponseData().getStatus()).isEqualTo(401L); + } + } + @AfterEach public void quitDriver() { if (driver != null) {