Skip to content

Commit

Permalink
[java] use annotations to restart the driver in unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed Nov 4, 2024
1 parent 97c5118 commit 53836c7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 59 deletions.
38 changes: 15 additions & 23 deletions java/test/org/openqa/selenium/WebNetworkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,34 @@

import java.net.URI;
import java.util.function.Predicate;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.environment.webserver.AppServer;
import org.openqa.selenium.environment.webserver.NettyAppServer;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NeedsFreshDriver;
import org.openqa.selenium.testing.drivers.Browser;

class WebNetworkTest extends JupiterTestBase {

private String page;
private AppServer server;

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

@AfterEach
public void cleanUp() {
driver.quit();
}

@Test
@NeedsFreshDriver
@Ignore(Browser.CHROME)
@Ignore(Browser.EDGE)
void canAddAuthenticationHandler() {
((RemoteWebDriver) driver)
.network()
.addAuthenticationHandler(new UsernameAndPassword("test", "test"));

page = server.whereIs("basicAuth");
page = appServer.whereIs("basicAuth");
driver.get(page);

assertThat(driver.findElement(By.tagName("h1")).getText()).isEqualTo("authorized");
}

@Test
@NeedsFreshDriver
@Ignore(Browser.CHROME)
@Ignore(Browser.EDGE)
void canAddAuthenticationHandlerWithFilter() {
Expand All @@ -72,13 +59,14 @@ void canAddAuthenticationHandlerWithFilter() {
.network()
.addAuthenticationHandler(filter, new UsernameAndPassword("test", "test"));

page = server.whereIs("basicAuth");
page = appServer.whereIs("basicAuth");
driver.get(page);

assertThat(driver.findElement(By.tagName("h1")).getText()).isEqualTo("authorized");
}

@Test
@NeedsFreshDriver
@Ignore(Browser.CHROME)
@Ignore(Browser.EDGE)
void canAddMultipleAuthenticationHandlersWithFilter() {
Expand All @@ -92,13 +80,14 @@ void canAddMultipleAuthenticationHandlersWithFilter() {
.addAuthenticationHandler(
uri -> uri.getPath().contains("test"), new UsernameAndPassword("test1", "test1"));

page = server.whereIs("basicAuth");
page = appServer.whereIs("basicAuth");
driver.get(page);

assertThat(driver.findElement(By.tagName("h1")).getText()).isEqualTo("authorized");
}

@Test
@NeedsFreshDriver
@Ignore(Browser.CHROME)
@Ignore(Browser.EDGE)
void canAddMultipleAuthenticationHandlersWithTheSameFilter() {
Expand All @@ -112,13 +101,14 @@ void canAddMultipleAuthenticationHandlersWithTheSameFilter() {
.addAuthenticationHandler(
uri -> uri.getPath().contains("basicAuth"), new UsernameAndPassword("test", "test"));

page = server.whereIs("basicAuth");
page = appServer.whereIs("basicAuth");
driver.get(page);

assertThat(driver.findElement(By.tagName("h1")).getText()).isEqualTo("authorized");
}

@Test
@NeedsFreshDriver
@Ignore(Browser.CHROME)
@Ignore(Browser.EDGE)
void canRemoveAuthenticationHandler() {
Expand All @@ -128,26 +118,28 @@ void canRemoveAuthenticationHandler() {
.addAuthenticationHandler(new UsernameAndPassword("test", "test"));

((RemoteWebDriver) driver).network().removeAuthenticationHandler(id);
page = server.whereIs("basicAuth");
page = appServer.whereIs("basicAuth");
driver.get(page);

assertThatExceptionOfType(UnhandledAlertException.class)
.isThrownBy(() -> driver.findElement(By.tagName("h1")));
}

@Test
@NeedsFreshDriver
@Ignore(Browser.CHROME)
@Ignore(Browser.EDGE)
void canRemoveAuthenticationHandlerThatDoesNotExist() {
((RemoteWebDriver) driver).network().removeAuthenticationHandler(5);
page = server.whereIs("basicAuth");
page = appServer.whereIs("basicAuth");
driver.get(page);

assertThatExceptionOfType(UnhandledAlertException.class)
.isThrownBy(() -> driver.findElement(By.tagName("h1")));
}

@Test
@NeedsFreshDriver
@Ignore(Browser.CHROME)
@Ignore(Browser.EDGE)
void canClearAuthenticationHandlers() {
Expand All @@ -165,7 +157,7 @@ void canClearAuthenticationHandlers() {
.addAuthenticationHandler(new UsernameAndPassword("test1", "test1"));

((RemoteWebDriver) driver).network().clearAuthenticationHandlers();
page = server.whereIs("basicAuth");
page = appServer.whereIs("basicAuth");
driver.get(page);

assertThatExceptionOfType(UnhandledAlertException.class)
Expand Down
40 changes: 17 additions & 23 deletions java/test/org/openqa/selenium/WebScriptTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,29 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.bidi.log.ConsoleLogEntry;
import org.openqa.selenium.bidi.log.JavascriptLogEntry;
import org.openqa.selenium.bidi.log.LogLevel;
import org.openqa.selenium.environment.webserver.AppServer;
import org.openqa.selenium.environment.webserver.NettyAppServer;
import org.openqa.selenium.remote.DomMutation;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NeedsFreshDriver;

class WebScriptTest extends JupiterTestBase {

String page;
private AppServer server;

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

@AfterEach
public void cleanUp() {
driver.quit();
}

@Test
@NeedsFreshDriver
void canAddConsoleMessageHandler()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<ConsoleLogEntry> future = new CompletableFuture<>();

long id = ((RemoteWebDriver) driver).script().addConsoleMessageHandler(future::complete);

page = server.whereIs("/bidi/logEntryAdded.html");
page = appServer.whereIs("/bidi/logEntryAdded.html");
driver.get(page);
driver.findElement(By.id("consoleLog")).click();

Expand All @@ -81,6 +67,7 @@ void canAddConsoleMessageHandler()
}

@Test
@NeedsFreshDriver
void canRemoveConsoleMessageHandler()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<ConsoleLogEntry> future1 = new CompletableFuture<>();
Expand All @@ -96,7 +83,7 @@ void canRemoveConsoleMessageHandler()
// Removing the second consumer, so it will no longer get the console message.
((RemoteWebDriver) driver).script().removeConsoleMessageHandler(id2);

page = server.whereIs("/bidi/logEntryAdded.html");
page = appServer.whereIs("/bidi/logEntryAdded.html");
driver.get(page);
driver.findElement(By.id("consoleLog")).click();

Expand All @@ -113,12 +100,13 @@ void canRemoveConsoleMessageHandler()
}

@Test
@NeedsFreshDriver
void canAddJsErrorHandler() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<JavascriptLogEntry> future = new CompletableFuture<>();

long id = ((RemoteWebDriver) driver).script().addJavaScriptErrorHandler(future::complete);

page = server.whereIs("/bidi/logEntryAdded.html");
page = appServer.whereIs("/bidi/logEntryAdded.html");
driver.get(page);
driver.findElement(By.id("jsException")).click();

Expand All @@ -132,6 +120,7 @@ void canAddJsErrorHandler() throws ExecutionException, InterruptedException, Tim
}

@Test
@NeedsFreshDriver
void canRemoveJsErrorHandler() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<JavascriptLogEntry> future1 = new CompletableFuture<>();
CompletableFuture<JavascriptLogEntry> future2 = new CompletableFuture<>();
Expand All @@ -146,7 +135,7 @@ void canRemoveJsErrorHandler() throws ExecutionException, InterruptedException,
// Removing the second consumer, so it will no longer get the JS error.
((RemoteWebDriver) driver).script().removeJavaScriptErrorHandler(id2);

page = server.whereIs("/bidi/logEntryAdded.html");
page = appServer.whereIs("/bidi/logEntryAdded.html");
driver.get(page);
driver.findElement(By.id("jsException")).click();

Expand All @@ -166,6 +155,7 @@ void canRemoveJsErrorHandler() throws ExecutionException, InterruptedException,
}

@Test
@NeedsFreshDriver
void canAddMultipleHandlers() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<JavascriptLogEntry> future1 = new CompletableFuture<>();
CompletableFuture<JavascriptLogEntry> future2 = new CompletableFuture<>();
Expand All @@ -177,7 +167,7 @@ void canAddMultipleHandlers() throws ExecutionException, InterruptedException, T
long id1 = ((RemoteWebDriver) driver).script().addJavaScriptErrorHandler(consumer1);
long id2 = ((RemoteWebDriver) driver).script().addJavaScriptErrorHandler(consumer2);

page = server.whereIs("/bidi/logEntryAdded.html");
page = appServer.whereIs("/bidi/logEntryAdded.html");
driver.get(page);
driver.findElement(By.id("jsException")).click();

Expand All @@ -193,6 +183,7 @@ void canAddMultipleHandlers() throws ExecutionException, InterruptedException, T
}

@Test
@NeedsFreshDriver
void canAddDomMutationHandler() throws InterruptedException {
AtomicReference<DomMutation> seen = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
Expand Down Expand Up @@ -220,6 +211,7 @@ void canAddDomMutationHandler() throws InterruptedException {
}

@Test
@NeedsFreshDriver
void canRemoveDomMutationHandler() throws InterruptedException {
AtomicReference<DomMutation> seen = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
Expand Down Expand Up @@ -247,14 +239,15 @@ void canRemoveDomMutationHandler() throws InterruptedException {
}

@Test
@NeedsFreshDriver
void canPinScript() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<ConsoleLogEntry> future = new CompletableFuture<>();

((RemoteWebDriver) driver).script().pin("() => { console.log('Hello!'); }");

long id = ((RemoteWebDriver) driver).script().addConsoleMessageHandler(future::complete);

page = server.whereIs("/bidi/logEntryAdded.html");
page = appServer.whereIs("/bidi/logEntryAdded.html");
driver.get(page);

ConsoleLogEntry logEntry = future.get(5, TimeUnit.SECONDS);
Expand All @@ -265,6 +258,7 @@ void canPinScript() throws ExecutionException, InterruptedException, TimeoutExce
}

@Test
@NeedsFreshDriver
void canUnpinScript() throws ExecutionException, InterruptedException, TimeoutException {
CountDownLatch latch = new CountDownLatch(2);

Expand All @@ -276,7 +270,7 @@ void canUnpinScript() throws ExecutionException, InterruptedException, TimeoutEx
.script()
.addConsoleMessageHandler(consoleLogEntry -> latch.countDown());

page = server.whereIs("/bidi/logEntryAdded.html");
page = appServer.whereIs("/bidi/logEntryAdded.html");

driver.get(page);

Expand Down
13 changes: 0 additions & 13 deletions java/test/org/openqa/selenium/javascript/JavaScriptTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@
import java.util.function.Function;
import java.util.function.Supplier;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.build.InProject;
import org.openqa.selenium.environment.GlobalTestEnvironment;
import org.openqa.selenium.environment.InProcessTestEnvironment;
import org.openqa.selenium.environment.TestEnvironment;
import org.openqa.selenium.environment.webserver.AppServer;
import org.openqa.selenium.testing.drivers.WebDriverBuilder;

Expand All @@ -48,8 +45,6 @@ class JavaScriptTestSuite {

private final long timeout;

private TestEnvironment testEnvironment;

public JavaScriptTestSuite() {
this.timeout = Math.max(0, Long.getLong("js.test.timeout", 0));
this.driverSupplier = new DriverSupplier();
Expand All @@ -59,16 +54,8 @@ private static boolean isBazel() {
return InProject.findRunfilesRoot() != null;
}

@BeforeEach
public void setup() {
testEnvironment = GlobalTestEnvironment.getOrCreate(InProcessTestEnvironment::new);
}

@AfterEach
public void teardown() throws IOException {
if (testEnvironment != null) {
testEnvironment.stop();
}
if (driverSupplier != null) {
((Closeable) driverSupplier).close();
}
Expand Down

0 comments on commit 53836c7

Please sign in to comment.