Skip to content

Commit

Permalink
Java use Base Test class where possible (#1384)
Browse files Browse the repository at this point in the history
* use BaseTest in Java example code where possible

* fix CI failure by adding wait

[deploy site]
  • Loading branch information
titusfortner committed May 15, 2023
1 parent 49b073e commit 1722cc5
Show file tree
Hide file tree
Showing 17 changed files with 408 additions and 210 deletions.
21 changes: 20 additions & 1 deletion examples/java/src/test/java/dev/selenium/BaseTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
package dev.selenium;

import java.time.Duration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.GeckoDriverService;
import org.openqa.selenium.remote.service.DriverService;
import org.openqa.selenium.support.ui.WebDriverWait;

public class BaseTest {
public WebDriver driver;

@BeforeAll
public static void defaultLogging() {
System.setProperty(GeckoDriverService.GECKO_DRIVER_LOG_PROPERTY, DriverService.LOG_NULL);
}

public WebElement getLocatedElement(WebDriver driver, By by) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
return wait.until(d -> driver.findElement(by));
}

@AfterEach
public void quit() {
driver.quit();
if (driver != null) {
driver.quit();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package dev.selenium.bidirectional.browsingcontext;

import org.junit.jupiter.api.AfterEach;
import dev.selenium.BaseTest;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.bidi.BiDiException;
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
Expand All @@ -14,11 +14,7 @@
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

import java.util.List;

class BrowsingContextTest {

private WebDriver driver;
class BrowsingContextTest extends BaseTest {

@BeforeEach
public void setup() {
Expand All @@ -27,11 +23,6 @@ public void setup() {
driver = new FirefoxDriver(options);
}

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

@Test
void testCreateABrowsingContextForGivenId() {
String id = driver.getWindowHandle();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package dev.selenium.bidirectional.log;

import org.junit.jupiter.api.AfterEach;
import dev.selenium.BaseTest;
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.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.bidi.LogInspector;
import org.openqa.selenium.bidi.log.ConsoleLogEntry;
import org.openqa.selenium.bidi.log.JavascriptLogEntry;
Expand All @@ -14,14 +17,7 @@
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

class LogInspectorTest {

private WebDriver driver;
class LogInspectorTest extends BaseTest {

@BeforeEach
public void setup() {
Expand All @@ -30,16 +26,11 @@ public void setup() {
driver = new FirefoxDriver(options);
}

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

@Test
void testListenToConsoleLog() throws ExecutionException, InterruptedException, TimeoutException {
try (LogInspector logInspector = new LogInspector(driver)) {
CompletableFuture<ConsoleLogEntry> future = new CompletableFuture<>();
logInspector.onConsoleLog(future::complete);
logInspector.onConsoleEntry(future::complete);

driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
driver.findElement(By.id("consoleLog")).click();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package dev.selenium.browsers;

import dev.selenium.BaseTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

import java.nio.file.Path;
import java.nio.file.Paths;

public class FirefoxTest {
public class FirefoxTest extends BaseTest {
public FirefoxDriver driver;

@AfterEach
Expand Down Expand Up @@ -54,7 +54,7 @@ public void installUnsignedAddonPath() {
driver.installExtension(path, true);

driver.get("https://www.selenium.dev/selenium/web/blank.html");
WebElement injected = driver.findElement(By.id("webextensions-selenium-example"));
WebElement injected = getLocatedElement(driver, By.id("webextensions-selenium-example"));
Assertions.assertEquals("Content injected by webextensions-selenium-example", injected.getText());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package dev.selenium.virtual_authenticator;

import dev.selenium.BaseChromeTest;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.util.List;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.InvalidArgumentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.virtualauthenticator.Credential;
import org.openqa.selenium.virtualauthenticator.HasVirtualAuthenticator;
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticator;
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticatorOptions;

public class VirtualAuthenticatorTest {
public class VirtualAuthenticatorTest extends BaseChromeTest {

/**
* A pkcs#8 encoded encrypted RSA private key as a base64url string.
Expand Down Expand Up @@ -54,21 +50,8 @@ public class VirtualAuthenticatorTest {
PKCS8EncodedKeySpec ec256PrivateKey =
new PKCS8EncodedKeySpec(Base64.getUrlDecoder().decode(base64EncodedEC256PK));

public WebDriver driver;

@BeforeEach
public void setup() {
driver = new ChromeDriver();
}

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

@Test
public void testVirtualOptions() {
// Create virtual authenticator options
VirtualAuthenticatorOptions options = new VirtualAuthenticatorOptions()
.setIsUserVerified(true)
.setHasUserVerification(true)
Expand All @@ -82,12 +65,10 @@ public void testVirtualOptions() {

@Test
public void testCreateAuthenticator() {
// Create virtual authenticator options
VirtualAuthenticatorOptions options = new VirtualAuthenticatorOptions()
.setProtocol(VirtualAuthenticatorOptions.Protocol.U2F)
.setHasResidentKey(false);

// Register a virtual authenticator
VirtualAuthenticator authenticator =
((HasVirtualAuthenticator) driver).addVirtualAuthenticator(options);

Expand All @@ -104,7 +85,6 @@ public void testRemoveAuthenticator() {

((HasVirtualAuthenticator) driver).removeVirtualAuthenticator(authenticator);

// Since the authenticator was removed, any operation using it will throw an error
Assertions.assertThrows(InvalidArgumentException.class, authenticator::getCredentials);
}

Expand Down
Loading

0 comments on commit 1722cc5

Please sign in to comment.