Skip to content

Commit 126d123

Browse files
committed
[java][bidi] Add Network command tests for Chrome
1 parent f5b8d3e commit 126d123

File tree

2 files changed

+75
-39
lines changed

2 files changed

+75
-39
lines changed

java/test/org/openqa/selenium/bidi/network/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ java_selenium_test_suite(
77
srcs = glob(["*Test.java"]),
88
browsers = [
99
"firefox",
10+
"chrome"
1011
],
1112
tags = [
1213
"selenium-remote",

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

Lines changed: 74 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,26 @@
1919

2020
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
2121
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
22+
import static org.junit.jupiter.api.Assertions.fail;
2223
import static org.openqa.selenium.testing.drivers.Browser.*;
2324

2425
import java.time.Duration;
2526
import java.time.temporal.ChronoUnit;
2627
import java.util.Collections;
2728
import java.util.concurrent.CountDownLatch;
2829
import java.util.concurrent.TimeUnit;
30+
import java.util.concurrent.atomic.AtomicInteger;
31+
2932
import org.junit.jupiter.api.Disabled;
3033
import org.junit.jupiter.api.Test;
31-
import org.openqa.selenium.Alert;
3234
import org.openqa.selenium.By;
33-
import org.openqa.selenium.TimeoutException;
3435
import org.openqa.selenium.UsernameAndPassword;
3536
import org.openqa.selenium.WebDriverException;
3637
import org.openqa.selenium.WindowType;
3738
import org.openqa.selenium.bidi.BiDiException;
3839
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
40+
import org.openqa.selenium.bidi.browsingcontext.ReadinessState;
3941
import org.openqa.selenium.bidi.module.Network;
40-
import org.openqa.selenium.support.ui.ExpectedConditions;
4142
import org.openqa.selenium.testing.JupiterTestBase;
4243
import org.openqa.selenium.testing.NeedsFreshDriver;
4344
import org.openqa.selenium.testing.NotYetImplemented;
@@ -47,8 +48,6 @@ class NetworkCommandsTest extends JupiterTestBase {
4748

4849
@Test
4950
@NeedsFreshDriver
50-
@NotYetImplemented(EDGE)
51-
@NotYetImplemented(CHROME)
5251
void canAddIntercept() {
5352
try (Network network = new Network(driver)) {
5453
String intercept =
@@ -60,7 +59,6 @@ void canAddIntercept() {
6059
@Test
6160
@NeedsFreshDriver
6261
@NotYetImplemented(EDGE)
63-
@NotYetImplemented(CHROME)
6462
void canContinueRequest() throws InterruptedException {
6563
try (Network network = new Network(driver)) {
6664
String intercept =
@@ -84,17 +82,17 @@ void canContinueRequest() throws InterruptedException {
8482

8583
assertThat(intercept).isNotNull();
8684

87-
driver.get(appServer.whereIs("/bidi/logEntryAdded.html"));
85+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
8886

87+
browsingContext.navigate(
88+
appServer.whereIs("/bidi/logEntryAdded.html"), ReadinessState.COMPLETE);
8989
boolean countdown = latch.await(5, TimeUnit.SECONDS);
9090
assertThat(countdown).isTrue();
9191
}
9292
}
9393

9494
@Test
9595
@NeedsFreshDriver
96-
@NotYetImplemented(EDGE)
97-
@NotYetImplemented(CHROME)
9896
void canContinueResponse() throws InterruptedException {
9997
try (Network network = new Network(driver)) {
10098
String intercept =
@@ -113,7 +111,10 @@ void canContinueResponse() throws InterruptedException {
113111

114112
assertThat(intercept).isNotNull();
115113

116-
driver.get(appServer.whereIs("/bidi/logEntryAdded.html"));
114+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
115+
116+
browsingContext.navigate(
117+
appServer.whereIs("/bidi/logEntryAdded.html"), ReadinessState.COMPLETE);
117118

118119
boolean countdown = latch.await(5, TimeUnit.SECONDS);
119120
assertThat(countdown).isTrue();
@@ -122,8 +123,6 @@ void canContinueResponse() throws InterruptedException {
122123

123124
@Test
124125
@NeedsFreshDriver
125-
@NotYetImplemented(EDGE)
126-
@NotYetImplemented(CHROME)
127126
void canProvideResponse() throws InterruptedException {
128127
try (Network network = new Network(driver)) {
129128
String intercept =
@@ -141,7 +140,10 @@ void canProvideResponse() throws InterruptedException {
141140

142141
assertThat(intercept).isNotNull();
143142

144-
driver.get(appServer.whereIs("/bidi/logEntryAdded.html"));
143+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
144+
145+
browsingContext.navigate(
146+
appServer.whereIs("/bidi/logEntryAdded.html"), ReadinessState.COMPLETE);
145147

146148
boolean countdown = latch.await(5, TimeUnit.SECONDS);
147149
assertThat(countdown).isTrue();
@@ -174,7 +176,10 @@ void canProvideResponseWithAllParameters() throws InterruptedException {
174176

175177
assertThat(intercept).isNotNull();
176178

177-
driver.get(appServer.whereIs("/bidi/logEntryAdded.html"));
179+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
180+
181+
browsingContext.navigate(
182+
appServer.whereIs("/bidi/logEntryAdded.html"), ReadinessState.COMPLETE);
178183

179184
boolean countdown = latch.await(5, TimeUnit.SECONDS);
180185
assertThat(countdown).isTrue();
@@ -185,8 +190,6 @@ void canProvideResponseWithAllParameters() throws InterruptedException {
185190

186191
@Test
187192
@NeedsFreshDriver
188-
@NotYetImplemented(EDGE)
189-
@NotYetImplemented(CHROME)
190193
void canRemoveIntercept() {
191194
try (Network network = new Network(driver)) {
192195
String intercept =
@@ -199,8 +202,6 @@ void canRemoveIntercept() {
199202

200203
@Test
201204
@NeedsFreshDriver
202-
@NotYetImplemented(EDGE)
203-
@NotYetImplemented(CHROME)
204205
void canContinueWithAuthCredentials() {
205206
try (Network network = new Network(driver)) {
206207
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
@@ -211,52 +212,79 @@ void canContinueWithAuthCredentials() {
211212
new UsernameAndPassword("test", "test")));
212213

213214
page = appServer.whereIs("basicAuth");
214-
driver.get(page);
215+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
216+
217+
browsingContext.navigate(page, ReadinessState.COMPLETE);
218+
215219
assertThat(driver.findElement(By.tagName("h1")).getText()).isEqualTo("authorized");
216220
}
217221
}
218222

219223
@Test
220224
@NeedsFreshDriver
221-
@NotYetImplemented(EDGE)
222-
@NotYetImplemented(CHROME)
223225
void canContinueWithoutAuthCredentials() {
224226
try (Network network = new Network(driver)) {
225227
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
226228
network.onAuthRequired(
227-
responseDetails ->
228-
// Does not handle the alert
229-
network.continueWithAuthNoCredentials(responseDetails.getRequest().getRequestId()));
229+
responseDetails -> {
230+
if (responseDetails.getRequest().getUrl().contains("basicAuth")) {
231+
network.continueWithAuthNoCredentials(responseDetails.getRequest().getRequestId());
232+
}
233+
});
230234
page = appServer.whereIs("basicAuth");
231-
driver.get(page);
232-
// This would fail if alert was handled
233-
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
234-
alert.dismiss();
235+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
236+
237+
try {
238+
browsingContext.navigate(page, ReadinessState.COMPLETE);
239+
fail("Exception should be thrown");
240+
} catch (Exception e) {
241+
assertThat(e).isInstanceOf(WebDriverException.class);
242+
}
235243
}
236244
}
237245

238246
@Test
239247
@NeedsFreshDriver
240-
@NotYetImplemented(EDGE)
241-
@NotYetImplemented(CHROME)
242-
void canCancelAuth() {
248+
void canCancelAuth() throws InterruptedException {
243249
try (Network network = new Network(driver)) {
244250
network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
245251
network.onAuthRequired(
246-
responseDetails ->
252+
responseDetails -> {
253+
if (responseDetails.getRequest().getUrl().contains("basicAuth")) {
247254
// Does not handle the alert
248-
network.cancelAuth(responseDetails.getRequest().getRequestId()));
255+
network.cancelAuth(responseDetails.getRequest().getRequestId());
256+
}
257+
});
258+
259+
AtomicInteger status = new AtomicInteger();
260+
CountDownLatch latch = new CountDownLatch(1);
261+
262+
network.onResponseCompleted(
263+
responseDetails -> {
264+
if (responseDetails.getRequest().getUrl().contains("basicAuth")) {
265+
status.set(responseDetails.getResponseData().getStatus());
266+
latch.countDown();
267+
}
268+
});
269+
249270
page = appServer.whereIs("basicAuth");
250-
driver.get(page);
251-
assertThatThrownBy(() -> wait.until(ExpectedConditions.alertIsPresent()))
252-
.isInstanceOf(TimeoutException.class);
271+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
272+
273+
try {
274+
browsingContext.navigate(page, ReadinessState.COMPLETE);
275+
} catch (Exception BiDiException) {
276+
// Ignore
277+
// Only Chromium browsers throw an error because the navigation did not complete as
278+
// expected.
279+
}
280+
281+
latch.await(10, TimeUnit.SECONDS);
282+
assertThat(status.get()).isEqualTo(401);
253283
}
254284
}
255285

256286
@Test
257287
@NeedsFreshDriver
258-
@NotYetImplemented(EDGE)
259-
@NotYetImplemented(CHROME)
260288
void canFailRequest() {
261289
try (Network network = new Network(driver)) {
262290
network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT));
@@ -265,7 +293,14 @@ void canFailRequest() {
265293
page = appServer.whereIs("basicAuth");
266294
driver.manage().timeouts().pageLoadTimeout(Duration.of(5, ChronoUnit.SECONDS));
267295

268-
assertThatThrownBy(() -> driver.get(page)).isInstanceOf(WebDriverException.class);
296+
assertThatThrownBy(
297+
() -> {
298+
BrowsingContext browsingContext =
299+
new BrowsingContext(driver, driver.getWindowHandle());
300+
browsingContext.navigate(
301+
appServer.whereIs("/bidi/logEntryAdded.html"), ReadinessState.COMPLETE);
302+
})
303+
.isInstanceOf(WebDriverException.class);
269304
}
270305
}
271306

0 commit comments

Comments
 (0)