Skip to content

Commit a9a0aa4

Browse files
committed
[bidi][java] Add command "continuewithAuth"
1 parent 569e64b commit a9a0aa4

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Map;
2323
import java.util.Set;
2424
import java.util.function.Consumer;
25+
import org.openqa.selenium.UsernameAndPassword;
2526
import org.openqa.selenium.WebDriver;
2627
import org.openqa.selenium.bidi.network.AddInterceptParameters;
2728
import org.openqa.selenium.bidi.network.BeforeRequestSent;
@@ -81,6 +82,34 @@ public void removeIntercept(String interceptId) {
8182
this.bidi.send(new Command<>("network.removeIntercept", Map.of("intercept", interceptId)));
8283
}
8384

85+
public void continueWithAuth(String requestId, UsernameAndPassword usernameAndPassword) {
86+
this.bidi.send(
87+
new Command<>(
88+
"network.continueWithAuth",
89+
Map.of(
90+
"request",
91+
requestId,
92+
"action",
93+
"provideCredentials",
94+
"credentials",
95+
Map.of(
96+
"type", "password",
97+
"username", usernameAndPassword.username(),
98+
"password", usernameAndPassword.password()))));
99+
}
100+
101+
public void continueWithAuthNoCredentials(String requestId) {
102+
this.bidi.send(
103+
new Command<>(
104+
"network.continueWithAuth", Map.of("request", requestId, "action", "default")));
105+
}
106+
107+
public void cancelAuth(String requestId) {
108+
this.bidi.send(
109+
new Command<>(
110+
"network.continueWithAuth", Map.of("request", requestId, "action", "cancel")));
111+
}
112+
84113
public void onBeforeRequestSent(Consumer<BeforeRequestSent> consumer) {
85114
if (browsingContextIds.isEmpty()) {
86115
this.bidi.addListener(beforeRequestSentEvent, consumer);

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.openqa.selenium.bidi.network;
1919

2020
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
21+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
2122
import static org.openqa.selenium.testing.Safely.safelyCall;
2223
import static org.openqa.selenium.testing.drivers.Browser.EDGE;
2324
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
@@ -27,13 +28,19 @@
2728
import org.junit.jupiter.api.AfterEach;
2829
import org.junit.jupiter.api.BeforeEach;
2930
import org.junit.jupiter.api.Test;
31+
import org.openqa.selenium.Alert;
32+
import org.openqa.selenium.By;
33+
import org.openqa.selenium.TimeoutException;
34+
import org.openqa.selenium.UsernameAndPassword;
3035
import org.openqa.selenium.bidi.Network;
3136
import org.openqa.selenium.environment.webserver.AppServer;
3237
import org.openqa.selenium.environment.webserver.NettyAppServer;
38+
import org.openqa.selenium.support.ui.ExpectedConditions;
3339
import org.openqa.selenium.testing.JupiterTestBase;
3440
import org.openqa.selenium.testing.NotYetImplemented;
3541

3642
class NetworkCommandsTest extends JupiterTestBase {
43+
private String page;
3744
private AppServer server;
3845

3946
@BeforeEach
@@ -70,6 +77,65 @@ void canRemoveIntercept() {
7077
}
7178
}
7279

80+
@Test
81+
@NotYetImplemented(SAFARI)
82+
@NotYetImplemented(IE)
83+
@NotYetImplemented(EDGE)
84+
@NotYetImplemented(FIREFOX)
85+
void canContinueWithAuthCredentials() {
86+
try (Network network = new Network(driver)) {
87+
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
88+
network.onAuthRequired(
89+
responseDetails ->
90+
network.continueWithAuth(
91+
responseDetails.getRequest().getRequestId(),
92+
new UsernameAndPassword("test", "test")));
93+
94+
page = server.whereIs("basicAuth");
95+
driver.get(page);
96+
assertThat(driver.findElement(By.tagName("h1")).getText()).isEqualTo("authorized");
97+
}
98+
}
99+
100+
@Test
101+
@NotYetImplemented(SAFARI)
102+
@NotYetImplemented(IE)
103+
@NotYetImplemented(EDGE)
104+
@NotYetImplemented(FIREFOX)
105+
void canContinueWithoutAuthCredentials() {
106+
try (Network network = new Network(driver)) {
107+
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
108+
network.onAuthRequired(
109+
responseDetails ->
110+
// Does not handle the alert
111+
network.continueWithAuthNoCredentials(responseDetails.getRequest().getRequestId()));
112+
page = server.whereIs("basicAuth");
113+
driver.get(page);
114+
// This would fail if alert was handled
115+
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
116+
alert.dismiss();
117+
}
118+
}
119+
120+
@Test
121+
@NotYetImplemented(SAFARI)
122+
@NotYetImplemented(IE)
123+
@NotYetImplemented(EDGE)
124+
@NotYetImplemented(FIREFOX)
125+
void canCancelAuth() {
126+
try (Network network = new Network(driver)) {
127+
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
128+
network.onAuthRequired(
129+
responseDetails ->
130+
// Does not handle the alert
131+
network.cancelAuth(responseDetails.getRequest().getRequestId()));
132+
page = server.whereIs("basicAuth");
133+
driver.get(page);
134+
assertThatThrownBy(() -> wait.until(ExpectedConditions.alertIsPresent()))
135+
.isInstanceOf(TimeoutException.class);
136+
}
137+
}
138+
73139
@AfterEach
74140
public void quitDriver() {
75141
if (driver != null) {

0 commit comments

Comments
 (0)