Skip to content

Commit

Permalink
[java] use common annotations in BiDi tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed Nov 4, 2024
1 parent 4480d12 commit 19afff7
Show file tree
Hide file tree
Showing 16 changed files with 223 additions and 326 deletions.
24 changes: 3 additions & 21 deletions java/test/org/openqa/selenium/bidi/BiDiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@
package org.openqa.selenium.bidi;

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 java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
Expand All @@ -35,24 +32,17 @@
import org.openqa.selenium.bidi.log.JavascriptLogEntry;
import org.openqa.selenium.bidi.log.LogLevel;
import org.openqa.selenium.bidi.module.LogInspector;
import org.openqa.selenium.environment.webserver.AppServer;
import org.openqa.selenium.environment.webserver.NettyAppServer;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NeedsFreshDriver;
import org.openqa.selenium.testing.NotYetImplemented;

class BiDiTest extends JupiterTestBase {

String page;
private AppServer server;

@BeforeEach
public void setUp() {
server = new NettyAppServer();
server.start();
}

@Test
@NotYetImplemented(EDGE)
@NeedsFreshDriver
void canNavigateAndListenToErrors()
throws ExecutionException, InterruptedException, TimeoutException {
try (org.openqa.selenium.bidi.module.LogInspector logInspector = new LogInspector(driver)) {
Expand All @@ -61,7 +51,7 @@ void canNavigateAndListenToErrors()

BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());

page = server.whereIs("/bidi/logEntryAdded.html");
page = appServer.whereIs("/bidi/logEntryAdded.html");
NavigationResult info = browsingContext.navigate(page, ReadinessState.COMPLETE);

// If navigation was successful, we expect both the url and navigation id to be set
Expand All @@ -78,12 +68,4 @@ void canNavigateAndListenToErrors()
assertThat(logEntry.getLevel()).isEqualTo(LogLevel.ERROR);
}
}

@AfterEach
public void quitDriver() {
if (driver != null) {
driver.quit();
}
safelyCall(server::stop);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,25 @@
package org.openqa.selenium.bidi.browser;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.openqa.selenium.testing.Safely.safelyCall;
import static org.openqa.selenium.testing.drivers.Browser.*;

import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.bidi.module.Browser;
import org.openqa.selenium.environment.webserver.AppServer;
import org.openqa.selenium.environment.webserver.NettyAppServer;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NeedsFreshDriver;

class BrowserCommandsTest extends JupiterTestBase {

private AppServer server;
private Browser browser;

@BeforeEach
public void setUp() {
server = new NettyAppServer();
server.start();
browser = new Browser(driver);
}

@Test
@NeedsFreshDriver
void canCreateAUserContext() {
String userContext = browser.createUserContext();

Expand All @@ -52,6 +46,7 @@ void canCreateAUserContext() {
}

@Test
@NeedsFreshDriver
void canGetUserContexts() {
String userContext1 = browser.createUserContext();
String userContext2 = browser.createUserContext();
Expand All @@ -64,6 +59,7 @@ void canGetUserContexts() {
}

@Test
@NeedsFreshDriver
void canRemoveUserContext() {
String userContext1 = browser.createUserContext();
String userContext2 = browser.createUserContext();
Expand All @@ -79,12 +75,4 @@ void canRemoveUserContext() {

browser.removeUserContext(userContext1);
}

@AfterEach
public void quitDriver() {
if (driver != null) {
driver.quit();
}
safelyCall(server::stop);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,22 @@
package org.openqa.selenium.bidi.browsingcontext;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.openqa.selenium.testing.Safely.safelyCall;
import static org.openqa.selenium.testing.drivers.Browser.*;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.bidi.module.BrowsingContextInspector;
import org.openqa.selenium.environment.webserver.AppServer;
import org.openqa.selenium.environment.webserver.NettyAppServer;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NeedsFreshDriver;

class BrowsingContextInspectorTest extends JupiterTestBase {

private AppServer server;

@BeforeEach
public void setUp() {
server = new NettyAppServer();
server.start();
}

@Test
@NeedsFreshDriver
void canListenToWindowBrowsingContextCreatedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
Expand All @@ -66,6 +54,7 @@ void canListenToWindowBrowsingContextCreatedEvent()
}

@Test
@NeedsFreshDriver
void canListenToBrowsingContextDestroyedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
Expand All @@ -87,6 +76,7 @@ void canListenToBrowsingContextDestroyedEvent()
}

@Test
@NeedsFreshDriver
void canListenToTabBrowsingContextCreatedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
Expand All @@ -106,14 +96,15 @@ void canListenToTabBrowsingContextCreatedEvent()
}

@Test
@NeedsFreshDriver
void canListenToDomContentLoadedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<NavigationInfo> future = new CompletableFuture<>();
inspector.onDomContentLoaded(future::complete);

BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
context.navigate(server.whereIs("/bidi/logEntryAdded.html"), ReadinessState.COMPLETE);
context.navigate(appServer.whereIs("/bidi/logEntryAdded.html"), ReadinessState.COMPLETE);

NavigationInfo navigationInfo = future.get(5, TimeUnit.SECONDS);
assertThat(navigationInfo.getBrowsingContextId()).isEqualTo(context.getId());
Expand All @@ -122,14 +113,15 @@ void canListenToDomContentLoadedEvent()
}

@Test
@NeedsFreshDriver
void canListenToBrowsingContextLoadedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<NavigationInfo> future = new CompletableFuture<>();
inspector.onBrowsingContextLoaded(future::complete);

BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
context.navigate(server.whereIs("/bidi/logEntryAdded.html"), ReadinessState.COMPLETE);
context.navigate(appServer.whereIs("/bidi/logEntryAdded.html"), ReadinessState.COMPLETE);

NavigationInfo navigationInfo = future.get(5, TimeUnit.SECONDS);
assertThat(navigationInfo.getBrowsingContextId()).isEqualTo(context.getId());
Expand All @@ -138,14 +130,15 @@ void canListenToBrowsingContextLoadedEvent()
}

@Test
@NeedsFreshDriver
void canListenToNavigationStartedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<NavigationInfo> future = new CompletableFuture<>();
inspector.onNavigationStarted(future::complete);

BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
context.navigate(server.whereIs("/bidi/logEntryAdded.html"), ReadinessState.COMPLETE);
context.navigate(appServer.whereIs("/bidi/logEntryAdded.html"), ReadinessState.COMPLETE);

NavigationInfo navigationInfo = future.get(5, TimeUnit.SECONDS);
assertThat(navigationInfo.getBrowsingContextId()).isEqualTo(context.getId());
Expand All @@ -154,18 +147,19 @@ void canListenToNavigationStartedEvent()
}

@Test
@NeedsFreshDriver
void canListenToFragmentNavigatedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
CompletableFuture<NavigationInfo> future = new CompletableFuture<>();

BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
context.navigate(server.whereIs("/linked_image.html"), ReadinessState.COMPLETE);
context.navigate(appServer.whereIs("/linked_image.html"), ReadinessState.COMPLETE);

inspector.onFragmentNavigated(future::complete);

context.navigate(
server.whereIs("/linked_image.html#linkToAnchorOnThisPage"), ReadinessState.COMPLETE);
appServer.whereIs("/linked_image.html#linkToAnchorOnThisPage"), ReadinessState.COMPLETE);

NavigationInfo navigationInfo = future.get(5, TimeUnit.SECONDS);
assertThat(navigationInfo.getBrowsingContextId()).isEqualTo(context.getId());
Expand All @@ -174,6 +168,7 @@ void canListenToFragmentNavigatedEvent()
}

@Test
@NeedsFreshDriver
void canListenToUserPromptOpenedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
Expand All @@ -182,7 +177,7 @@ void canListenToUserPromptOpenedEvent()
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
inspector.onUserPromptOpened(future::complete);

driver.get(server.whereIs("/alerts.html"));
driver.get(appServer.whereIs("/alerts.html"));

driver.findElement(By.id("alert")).click();

Expand All @@ -193,6 +188,7 @@ void canListenToUserPromptOpenedEvent()
}

@Test
@NeedsFreshDriver
// TODO: This test is flaky for comparing the browsing context id for Chrome and Edge. Fix flaky
// test.
void canListenToUserPromptClosedEvent()
Expand All @@ -203,7 +199,7 @@ void canListenToUserPromptClosedEvent()
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
inspector.onUserPromptClosed(future::complete);

driver.get(server.whereIs("/alerts.html"));
driver.get(appServer.whereIs("/alerts.html"));

driver.findElement(By.id("prompt")).click();

Expand All @@ -216,12 +212,4 @@ void canListenToUserPromptClosedEvent()
assertThat(userPromptClosed.getAccepted()).isTrue();
}
}

@AfterEach
public void quitDriver() {
if (driver != null) {
driver.quit();
}
safelyCall(server::stop);
}
}
Loading

0 comments on commit 19afff7

Please sign in to comment.