Skip to content

Commit

Permalink
[bidi][java] Add command "continuewithAuth"
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Jan 25, 2024
1 parent 569e64b commit a9a0aa4
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
29 changes: 29 additions & 0 deletions java/src/org/openqa/selenium/bidi/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.bidi.network.AddInterceptParameters;
import org.openqa.selenium.bidi.network.BeforeRequestSent;
Expand Down Expand Up @@ -81,6 +82,34 @@ public void removeIntercept(String interceptId) {
this.bidi.send(new Command<>("network.removeIntercept", Map.of("intercept", interceptId)));
}

public void continueWithAuth(String requestId, UsernameAndPassword usernameAndPassword) {
this.bidi.send(
new Command<>(
"network.continueWithAuth",
Map.of(
"request",
requestId,
"action",
"provideCredentials",
"credentials",
Map.of(
"type", "password",
"username", usernameAndPassword.username(),
"password", usernameAndPassword.password()))));
}

public void continueWithAuthNoCredentials(String requestId) {
this.bidi.send(
new Command<>(
"network.continueWithAuth", Map.of("request", requestId, "action", "default")));
}

public void cancelAuth(String requestId) {
this.bidi.send(
new Command<>(
"network.continueWithAuth", Map.of("request", requestId, "action", "cancel")));
}

public void onBeforeRequestSent(Consumer<BeforeRequestSent> consumer) {
if (browsingContextIds.isEmpty()) {
this.bidi.addListener(beforeRequestSentEvent, consumer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.openqa.selenium.bidi.network;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
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;
Expand All @@ -27,13 +28,19 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.bidi.Network;
import org.openqa.selenium.environment.webserver.AppServer;
import org.openqa.selenium.environment.webserver.NettyAppServer;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NotYetImplemented;

class NetworkCommandsTest extends JupiterTestBase {
private String page;
private AppServer server;

@BeforeEach
Expand Down Expand Up @@ -70,6 +77,65 @@ void canRemoveIntercept() {
}
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(EDGE)
@NotYetImplemented(FIREFOX)
void canContinueWithAuthCredentials() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
network.continueWithAuth(
responseDetails.getRequest().getRequestId(),
new UsernameAndPassword("test", "test")));

page = server.whereIs("basicAuth");
driver.get(page);
assertThat(driver.findElement(By.tagName("h1")).getText()).isEqualTo("authorized");
}
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(EDGE)
@NotYetImplemented(FIREFOX)
void canContinueWithoutAuthCredentials() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
// Does not handle the alert
network.continueWithAuthNoCredentials(responseDetails.getRequest().getRequestId()));
page = server.whereIs("basicAuth");
driver.get(page);
// This would fail if alert was handled
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.dismiss();
}
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(EDGE)
@NotYetImplemented(FIREFOX)
void canCancelAuth() {
try (Network network = new Network(driver)) {
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
network.onAuthRequired(
responseDetails ->
// Does not handle the alert
network.cancelAuth(responseDetails.getRequest().getRequestId()));
page = server.whereIs("basicAuth");
driver.get(page);
assertThatThrownBy(() -> wait.until(ExpectedConditions.alertIsPresent()))
.isInstanceOf(TimeoutException.class);
}
}

@AfterEach
public void quitDriver() {
if (driver != null) {
Expand Down

0 comments on commit a9a0aa4

Please sign in to comment.