Skip to content

Commit 1722cc5

Browse files
authored
Java use Base Test class where possible (SeleniumHQ#1384)
* use BaseTest in Java example code where possible * fix CI failure by adding wait [deploy site]
1 parent 49b073e commit 1722cc5

File tree

17 files changed

+408
-210
lines changed

17 files changed

+408
-210
lines changed
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
package dev.selenium;
22

3+
import java.time.Duration;
34
import org.junit.jupiter.api.AfterEach;
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.openqa.selenium.By;
47
import org.openqa.selenium.WebDriver;
8+
import org.openqa.selenium.WebElement;
9+
import org.openqa.selenium.firefox.GeckoDriverService;
10+
import org.openqa.selenium.remote.service.DriverService;
11+
import org.openqa.selenium.support.ui.WebDriverWait;
512

613
public class BaseTest {
714
public WebDriver driver;
815

16+
@BeforeAll
17+
public static void defaultLogging() {
18+
System.setProperty(GeckoDriverService.GECKO_DRIVER_LOG_PROPERTY, DriverService.LOG_NULL);
19+
}
20+
21+
public WebElement getLocatedElement(WebDriver driver, By by) {
22+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
23+
return wait.until(d -> driver.findElement(by));
24+
}
25+
926
@AfterEach
1027
public void quit() {
11-
driver.quit();
28+
if (driver != null) {
29+
driver.quit();
30+
}
1231
}
1332
}

examples/java/src/test/java/dev/selenium/bidirectional/browsingcontext/BrowsingContextTest.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package dev.selenium.bidirectional.browsingcontext;
22

3-
import org.junit.jupiter.api.AfterEach;
3+
import dev.selenium.BaseTest;
4+
import java.util.List;
45
import org.junit.jupiter.api.Assertions;
56
import org.junit.jupiter.api.BeforeEach;
67
import org.junit.jupiter.api.Test;
7-
import org.openqa.selenium.WebDriver;
88
import org.openqa.selenium.WindowType;
99
import org.openqa.selenium.bidi.BiDiException;
1010
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
@@ -14,11 +14,7 @@
1414
import org.openqa.selenium.firefox.FirefoxDriver;
1515
import org.openqa.selenium.firefox.FirefoxOptions;
1616

17-
import java.util.List;
18-
19-
class BrowsingContextTest {
20-
21-
private WebDriver driver;
17+
class BrowsingContextTest extends BaseTest {
2218

2319
@BeforeEach
2420
public void setup() {
@@ -27,11 +23,6 @@ public void setup() {
2723
driver = new FirefoxDriver(options);
2824
}
2925

30-
@AfterEach
31-
public void cleanup() {
32-
driver.quit();
33-
}
34-
3526
@Test
3627
void testCreateABrowsingContextForGivenId() {
3728
String id = driver.getWindowHandle();

examples/java/src/test/java/dev/selenium/bidirectional/log/LogInspectorTest.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package dev.selenium.bidirectional.log;
22

3-
import org.junit.jupiter.api.AfterEach;
3+
import dev.selenium.BaseTest;
4+
import java.util.concurrent.CompletableFuture;
5+
import java.util.concurrent.ExecutionException;
6+
import java.util.concurrent.TimeUnit;
7+
import java.util.concurrent.TimeoutException;
48
import org.junit.jupiter.api.Assertions;
59
import org.junit.jupiter.api.BeforeEach;
610
import org.junit.jupiter.api.Test;
711
import org.openqa.selenium.By;
8-
import org.openqa.selenium.WebDriver;
912
import org.openqa.selenium.bidi.LogInspector;
1013
import org.openqa.selenium.bidi.log.ConsoleLogEntry;
1114
import org.openqa.selenium.bidi.log.JavascriptLogEntry;
@@ -14,14 +17,7 @@
1417
import org.openqa.selenium.firefox.FirefoxDriver;
1518
import org.openqa.selenium.firefox.FirefoxOptions;
1619

17-
import java.util.concurrent.CompletableFuture;
18-
import java.util.concurrent.ExecutionException;
19-
import java.util.concurrent.TimeUnit;
20-
import java.util.concurrent.TimeoutException;
21-
22-
class LogInspectorTest {
23-
24-
private WebDriver driver;
20+
class LogInspectorTest extends BaseTest {
2521

2622
@BeforeEach
2723
public void setup() {
@@ -30,16 +26,11 @@ public void setup() {
3026
driver = new FirefoxDriver(options);
3127
}
3228

33-
@AfterEach
34-
public void cleanup() {
35-
driver.quit();
36-
}
37-
3829
@Test
3930
void testListenToConsoleLog() throws ExecutionException, InterruptedException, TimeoutException {
4031
try (LogInspector logInspector = new LogInspector(driver)) {
4132
CompletableFuture<ConsoleLogEntry> future = new CompletableFuture<>();
42-
logInspector.onConsoleLog(future::complete);
33+
logInspector.onConsoleEntry(future::complete);
4334

4435
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
4536
driver.findElement(By.id("consoleLog")).click();

examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package dev.selenium.browsers;
22

3+
import dev.selenium.BaseTest;
34
import org.junit.jupiter.api.AfterEach;
45
import org.junit.jupiter.api.Assertions;
56
import org.junit.jupiter.api.Test;
67
import org.openqa.selenium.By;
78
import org.openqa.selenium.WebElement;
89
import org.openqa.selenium.firefox.FirefoxDriver;
910
import org.openqa.selenium.firefox.FirefoxOptions;
10-
1111
import java.nio.file.Path;
1212
import java.nio.file.Paths;
1313

14-
public class FirefoxTest {
14+
public class FirefoxTest extends BaseTest {
1515
public FirefoxDriver driver;
1616

1717
@AfterEach
@@ -54,7 +54,7 @@ public void installUnsignedAddonPath() {
5454
driver.installExtension(path, true);
5555

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

examples/java/src/test/java/dev/selenium/virtual_authenticator/VirtualAuthenticatorTest.java

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
package dev.selenium.virtual_authenticator;
22

3+
import dev.selenium.BaseChromeTest;
34
import java.security.spec.PKCS8EncodedKeySpec;
45
import java.util.Base64;
56
import java.util.List;
6-
7-
import org.junit.jupiter.api.AfterEach;
87
import org.junit.jupiter.api.Assertions;
9-
import org.junit.jupiter.api.BeforeEach;
108
import org.junit.jupiter.api.Test;
119
import org.openqa.selenium.InvalidArgumentException;
12-
import org.openqa.selenium.WebDriver;
13-
import org.openqa.selenium.chrome.ChromeDriver;
1410
import org.openqa.selenium.virtualauthenticator.Credential;
1511
import org.openqa.selenium.virtualauthenticator.HasVirtualAuthenticator;
1612
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticator;
1713
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticatorOptions;
1814

19-
public class VirtualAuthenticatorTest {
15+
public class VirtualAuthenticatorTest extends BaseChromeTest {
2016

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

57-
public WebDriver driver;
58-
59-
@BeforeEach
60-
public void setup() {
61-
driver = new ChromeDriver();
62-
}
63-
64-
@AfterEach
65-
public void quit() {
66-
driver.quit();
67-
}
68-
6953
@Test
7054
public void testVirtualOptions() {
71-
// Create virtual authenticator options
7255
VirtualAuthenticatorOptions options = new VirtualAuthenticatorOptions()
7356
.setIsUserVerified(true)
7457
.setHasUserVerification(true)
@@ -82,12 +65,10 @@ public void testVirtualOptions() {
8265

8366
@Test
8467
public void testCreateAuthenticator() {
85-
// Create virtual authenticator options
8668
VirtualAuthenticatorOptions options = new VirtualAuthenticatorOptions()
8769
.setProtocol(VirtualAuthenticatorOptions.Protocol.U2F)
8870
.setHasResidentKey(false);
8971

90-
// Register a virtual authenticator
9172
VirtualAuthenticator authenticator =
9273
((HasVirtualAuthenticator) driver).addVirtualAuthenticator(options);
9374

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

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

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

0 commit comments

Comments
 (0)