Skip to content

Commit

Permalink
[bidi][java] Add auth required event
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Jan 3, 2024
1 parent 214df71 commit 52f1625
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions java/src/org/openqa/selenium/bidi/Network.java
Expand Up @@ -41,6 +41,9 @@ public class Network implements AutoCloseable {
private final Event<ResponseDetails> responseCompleted =
new Event<>("network.responseStarted", ResponseDetails::fromJsonMap);

private final Event<ResponseDetails> authRequired =
new Event<>("network.authRequired", ResponseDetails::fromJsonMap);

public Network(WebDriver driver) {
this(new HashSet<>(), driver);
}
Expand Down Expand Up @@ -85,10 +88,19 @@ public void onResponseCompleted(Consumer<ResponseDetails> consumer) {
}
}

public void onAuthRequired(Consumer<ResponseDetails> 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);
}
}
26 changes: 26 additions & 0 deletions java/test/org/openqa/selenium/bidi/network/NetworkEventsTest.java
Expand Up @@ -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;

Expand Down Expand Up @@ -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<ResponseDetails> 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) {
Expand Down

0 comments on commit 52f1625

Please sign in to comment.