diff --git a/java/test/org/openqa/selenium/bidi/network/BUILD.bazel b/java/test/org/openqa/selenium/bidi/network/BUILD.bazel index c8fedb2afd95b..3068b724013b7 100644 --- a/java/test/org/openqa/selenium/bidi/network/BUILD.bazel +++ b/java/test/org/openqa/selenium/bidi/network/BUILD.bazel @@ -7,6 +7,7 @@ java_selenium_test_suite( srcs = glob(["*Test.java"]), browsers = [ "firefox", + "chrome", ], tags = [ "selenium-remote", diff --git a/java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java b/java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java index d60b4e367ec0c..15d4fdeffabd4 100644 --- a/java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java +++ b/java/test/org/openqa/selenium/bidi/network/NetworkCommandsTest.java @@ -19,6 +19,7 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.fail; import static org.openqa.selenium.testing.drivers.Browser.*; import java.time.Duration; @@ -26,18 +27,17 @@ import java.util.Collections; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.Disabled; 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.WebDriverException; import org.openqa.selenium.WindowType; import org.openqa.selenium.bidi.BiDiException; import org.openqa.selenium.bidi.browsingcontext.BrowsingContext; +import org.openqa.selenium.bidi.browsingcontext.ReadinessState; import org.openqa.selenium.bidi.module.Network; -import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.testing.JupiterTestBase; import org.openqa.selenium.testing.NeedsFreshDriver; import org.openqa.selenium.testing.NotYetImplemented; @@ -47,8 +47,6 @@ class NetworkCommandsTest extends JupiterTestBase { @Test @NeedsFreshDriver - @NotYetImplemented(EDGE) - @NotYetImplemented(CHROME) void canAddIntercept() { try (Network network = new Network(driver)) { String intercept = @@ -60,7 +58,6 @@ void canAddIntercept() { @Test @NeedsFreshDriver @NotYetImplemented(EDGE) - @NotYetImplemented(CHROME) void canContinueRequest() throws InterruptedException { try (Network network = new Network(driver)) { String intercept = @@ -84,8 +81,10 @@ void canContinueRequest() throws InterruptedException { assertThat(intercept).isNotNull(); - driver.get(appServer.whereIs("/bidi/logEntryAdded.html")); + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); + browsingContext.navigate( + appServer.whereIs("/bidi/logEntryAdded.html"), ReadinessState.COMPLETE); boolean countdown = latch.await(5, TimeUnit.SECONDS); assertThat(countdown).isTrue(); } @@ -93,8 +92,6 @@ void canContinueRequest() throws InterruptedException { @Test @NeedsFreshDriver - @NotYetImplemented(EDGE) - @NotYetImplemented(CHROME) void canContinueResponse() throws InterruptedException { try (Network network = new Network(driver)) { String intercept = @@ -113,7 +110,10 @@ void canContinueResponse() throws InterruptedException { assertThat(intercept).isNotNull(); - driver.get(appServer.whereIs("/bidi/logEntryAdded.html")); + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); + + browsingContext.navigate( + appServer.whereIs("/bidi/logEntryAdded.html"), ReadinessState.COMPLETE); boolean countdown = latch.await(5, TimeUnit.SECONDS); assertThat(countdown).isTrue(); @@ -122,8 +122,6 @@ void canContinueResponse() throws InterruptedException { @Test @NeedsFreshDriver - @NotYetImplemented(EDGE) - @NotYetImplemented(CHROME) void canProvideResponse() throws InterruptedException { try (Network network = new Network(driver)) { String intercept = @@ -141,7 +139,10 @@ void canProvideResponse() throws InterruptedException { assertThat(intercept).isNotNull(); - driver.get(appServer.whereIs("/bidi/logEntryAdded.html")); + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); + + browsingContext.navigate( + appServer.whereIs("/bidi/logEntryAdded.html"), ReadinessState.COMPLETE); boolean countdown = latch.await(5, TimeUnit.SECONDS); assertThat(countdown).isTrue(); @@ -174,7 +175,10 @@ void canProvideResponseWithAllParameters() throws InterruptedException { assertThat(intercept).isNotNull(); - driver.get(appServer.whereIs("/bidi/logEntryAdded.html")); + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); + + browsingContext.navigate( + appServer.whereIs("/bidi/logEntryAdded.html"), ReadinessState.COMPLETE); boolean countdown = latch.await(5, TimeUnit.SECONDS); assertThat(countdown).isTrue(); @@ -185,8 +189,6 @@ void canProvideResponseWithAllParameters() throws InterruptedException { @Test @NeedsFreshDriver - @NotYetImplemented(EDGE) - @NotYetImplemented(CHROME) void canRemoveIntercept() { try (Network network = new Network(driver)) { String intercept = @@ -199,8 +201,6 @@ void canRemoveIntercept() { @Test @NeedsFreshDriver - @NotYetImplemented(EDGE) - @NotYetImplemented(CHROME) void canContinueWithAuthCredentials() { try (Network network = new Network(driver)) { network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED)); @@ -211,52 +211,79 @@ void canContinueWithAuthCredentials() { new UsernameAndPassword("test", "test"))); page = appServer.whereIs("basicAuth"); - driver.get(page); + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); + + browsingContext.navigate(page, ReadinessState.COMPLETE); + assertThat(driver.findElement(By.tagName("h1")).getText()).isEqualTo("authorized"); } } @Test @NeedsFreshDriver - @NotYetImplemented(EDGE) - @NotYetImplemented(CHROME) 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())); + responseDetails -> { + if (responseDetails.getRequest().getUrl().contains("basicAuth")) { + network.continueWithAuthNoCredentials(responseDetails.getRequest().getRequestId()); + } + }); page = appServer.whereIs("basicAuth"); - driver.get(page); - // This would fail if alert was handled - Alert alert = wait.until(ExpectedConditions.alertIsPresent()); - alert.dismiss(); + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); + + try { + browsingContext.navigate(page, ReadinessState.COMPLETE); + fail("Exception should be thrown"); + } catch (Exception e) { + assertThat(e).isInstanceOf(WebDriverException.class); + } } } @Test @NeedsFreshDriver - @NotYetImplemented(EDGE) - @NotYetImplemented(CHROME) - void canCancelAuth() { + void canCancelAuth() throws InterruptedException { try (Network network = new Network(driver)) { network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED)); network.onAuthRequired( - responseDetails -> + responseDetails -> { + if (responseDetails.getRequest().getUrl().contains("basicAuth")) { // Does not handle the alert - network.cancelAuth(responseDetails.getRequest().getRequestId())); + network.cancelAuth(responseDetails.getRequest().getRequestId()); + } + }); + + AtomicInteger status = new AtomicInteger(); + CountDownLatch latch = new CountDownLatch(1); + + network.onResponseCompleted( + responseDetails -> { + if (responseDetails.getRequest().getUrl().contains("basicAuth")) { + status.set(responseDetails.getResponseData().getStatus()); + latch.countDown(); + } + }); + page = appServer.whereIs("basicAuth"); - driver.get(page); - assertThatThrownBy(() -> wait.until(ExpectedConditions.alertIsPresent())) - .isInstanceOf(TimeoutException.class); + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); + + try { + browsingContext.navigate(page, ReadinessState.COMPLETE); + } catch (Exception BiDiException) { + // Ignore + // Only Chromium browsers throw an error because the navigation did not complete as + // expected. + } + + latch.await(10, TimeUnit.SECONDS); + assertThat(status.get()).isEqualTo(401); } } @Test @NeedsFreshDriver - @NotYetImplemented(EDGE) - @NotYetImplemented(CHROME) void canFailRequest() { try (Network network = new Network(driver)) { network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT)); @@ -265,7 +292,14 @@ void canFailRequest() { page = appServer.whereIs("basicAuth"); driver.manage().timeouts().pageLoadTimeout(Duration.of(5, ChronoUnit.SECONDS)); - assertThatThrownBy(() -> driver.get(page)).isInstanceOf(WebDriverException.class); + assertThatThrownBy( + () -> { + BrowsingContext browsingContext = + new BrowsingContext(driver, driver.getWindowHandle()); + browsingContext.navigate( + appServer.whereIs("/bidi/logEntryAdded.html"), ReadinessState.COMPLETE); + }) + .isInstanceOf(WebDriverException.class); } }