Skip to content

Commit 659f6c9

Browse files
committed
fix closing WebDriver after tests
* always wrap `driver.quit()` into `if (driver != null)` to avoid NPE if the browser hadn't been started due to some error * mark the closing @AfterEach method as final to avoid occasional overriding in subclasses * No need to call `driver.quit()` in `BaseTest` subclasses because it's already done by `BaseTest`.
1 parent 5036723 commit 659f6c9

11 files changed

Lines changed: 154 additions & 183 deletions

File tree

examples/java/src/test/java/dev/selenium/BaseTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,11 @@ protected ChromeDriver startChromeDriver(ChromeOptions options) {
5959
}
6060

6161
protected static ChromeOptions getDefaultChromeOptions() {
62-
ChromeOptions options = new ChromeOptions();
63-
options.addArguments("--no-sandbox");
64-
return options;
62+
return new ChromeOptions().addArguments("--no-sandbox");
6563
}
6664

6765
protected static EdgeOptions getDefaultEdgeOptions() {
68-
EdgeOptions options = new EdgeOptions();
69-
options.addArguments("--no-sandbox");
70-
return options;
66+
return new EdgeOptions().addArguments("--no-sandbox");
7167
}
7268

7369
protected File getTempDirectory(String prefix) {
@@ -156,7 +152,7 @@ protected void enableLogging() {
156152
}
157153

158154
@AfterEach
159-
public void quit() {
155+
public final void closeBrowser() {
160156
if (driver != null) {
161157
driver.quit();
162158
}

examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ void canHaveTheDefaultBackgroundColor() {
128128

129129
@AfterAll
130130
public static void cleanup() {
131-
driver.quit();
131+
if (driver != null) {
132+
driver.quit();
133+
driver = null;
134+
}
132135
}
133136
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ public void setNetworkConditions() {
218218
() -> Assertions.assertEquals(networkConditions.getUploadThroughput(), actualConditions.getUploadThroughput())
219219
);
220220
((EdgeDriver) driver).deleteNetworkConditions();
221-
driver.quit();
222221
}
223222

224223
@Test

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@
1818
import org.openqa.selenium.firefox.*;
1919
import org.openqa.selenium.remote.service.DriverFinder;
2020

21-
22-
23-
24-
2521
public class FirefoxTest extends BaseTest {
2622
private FirefoxDriver driver;
2723

2824
@AfterEach
2925
public void clearProperties() {
3026
System.clearProperty(GeckoDriverService.GECKO_DRIVER_LOG_PROPERTY);
31-
System.clearProperty(GeckoDriverService.GECKO_DRIVER_LOG_LEVEL_PROPERTY);driver.quit();
27+
System.clearProperty(GeckoDriverService.GECKO_DRIVER_LOG_LEVEL_PROPERTY);
28+
if (driver != null) {
29+
driver.quit();
30+
}
3231
}
3332

3433
@Test
@@ -186,24 +185,20 @@ public void fullPageScreenshot() throws Exception {
186185
// Verify the screenshot file exists
187186
Assertions.assertTrue(targetFile.exists(), "The full page screenshot file should exist");
188187
Files.deleteIfExists(targetFile.toPath());
189-
190-
driver.quit();
191188
}
192189

193190
@Test
194191
public void setContext() {
195192
driver = startFirefoxDriver(new FirefoxOptions().addArguments("-remote-allow-system-access"));
196193

197-
((HasContext) driver).setContext(FirefoxCommandContext.CHROME);
194+
driver.setContext(FirefoxCommandContext.CHROME);
198195
driver.executeScript("console.log('Inside Chrome context');");
199196

200197
// Verify the context is back to "content"
201198
Assertions.assertEquals(
202-
FirefoxCommandContext.CHROME, ((HasContext) driver).getContext(),
199+
FirefoxCommandContext.CHROME, driver.getContext(),
203200
"The context should be 'chrome'"
204201
);
205-
206-
driver.quit();
207202
}
208203

209204
@Test
@@ -214,7 +209,5 @@ public void firefoxProfile() {
214209
options.setProfile(profile);
215210

216211
driver = new FirefoxDriver(options);
217-
218-
driver.quit();
219212
}
220213
}

examples/java/src/test/java/dev/selenium/drivers/HttpClientTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public void remoteWebDriverWithClientConfig() throws Exception {
4848
.address(gridUrl)
4949
.config(clientConfig)
5050
.build();
51-
driver.quit();
5251
}
5352

5453
@Test
@@ -67,7 +66,6 @@ public void remoteWebDriverIgnoreSSL() throws Exception {
6766
.address(gridUrl)
6867
.config(clientConfig)
6968
.build();
70-
driver.quit();
7169
}
7270

7371
@Test
@@ -85,7 +83,6 @@ public void remoteWebDriverWithEmbedAuthUrl() throws Exception {
8583
.address(embedAuthToUrl(gridUrl, "admin", "myStrongPassword"))
8684
.config(clientConfig)
8785
.build();
88-
driver.quit();
8986
}
9087

9188
private URL embedAuthToUrl(URL url, String username, String password) throws Exception {

examples/java/src/test/java/dev/selenium/elements/InformationTest.java

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import org.openqa.selenium.WebDriver;
77
import org.openqa.selenium.WebElement;
88
import org.openqa.selenium.chrome.ChromeDriver;
9+
910
import java.time.Duration;
11+
1012
import static org.junit.jupiter.api.Assertions.assertEquals;
1113
import static org.junit.jupiter.api.Assertions.assertTrue;
1214

@@ -15,57 +17,56 @@ public class InformationTest {
1517
@Test
1618
public void informationWithElements() {
1719

18-
WebDriver driver = new ChromeDriver();
19-
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
20-
// Navigate to Url
21-
driver.get("https://www.selenium.dev/selenium/web/inputs.html");
22-
23-
// isDisplayed
24-
// Get boolean value for is element display
25-
boolean isEmailVisible = driver.findElement(By.name("email_input")).isDisplayed();
26-
assertTrue(isEmailVisible);
20+
WebDriver driver = new ChromeDriver();
21+
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
22+
// Navigate to Url
23+
driver.get("https://www.selenium.dev/selenium/web/inputs.html");
2724

28-
// isEnabled
29-
// returns true if element is enabled else returns false
30-
boolean isEnabledButton = driver.findElement(By.name("button_input")).isEnabled();
31-
assertTrue(isEnabledButton);
25+
// isDisplayed
26+
// Get boolean value for is element display
27+
boolean isEmailVisible = driver.findElement(By.name("email_input")).isDisplayed();
28+
assertTrue(isEmailVisible);
3229

33-
// isSelected
34-
// returns true if element is checked else returns false
35-
boolean isSelectedCheck = driver.findElement(By.name("checkbox_input")).isSelected();
36-
assertTrue(isSelectedCheck);
30+
// isEnabled
31+
// returns true if element is enabled else returns false
32+
boolean isEnabledButton = driver.findElement(By.name("button_input")).isEnabled();
33+
assertTrue(isEnabledButton);
3734

38-
// TagName
39-
// returns TagName of the element
40-
String tagNameInp = driver.findElement(By.name("email_input")).getTagName();
41-
assertEquals("input", tagNameInp);
35+
// isSelected
36+
// returns true if element is checked else returns false
37+
boolean isSelectedCheck = driver.findElement(By.name("checkbox_input")).isSelected();
38+
assertTrue(isSelectedCheck);
4239

43-
// GetRect
44-
// Returns height, width, x and y coordinates referenced element
45-
Rectangle res = driver.findElement(By.name("range_input")).getRect();
46-
// Rectangle class provides getX,getY, getWidth, getHeight methods
47-
assertEquals(10, res.getX());
40+
// TagName
41+
// returns TagName of the element
42+
String tagNameInp = driver.findElement(By.name("email_input")).getTagName();
43+
assertEquals("input", tagNameInp);
4844

49-
// Retrieves the computed style property 'font-size' of field
50-
String cssValue = driver.findElement(By.name("color_input")).getCssValue("font-size");
51-
assertEquals(cssValue, "13.3333px");
45+
// GetRect
46+
// Returns height, width, x and y coordinates referenced element
47+
Rectangle res = driver.findElement(By.name("range_input")).getRect();
48+
// Rectangle class provides getX,getY, getWidth, getHeight methods
49+
assertEquals(10, res.getX());
5250

51+
// Retrieves the computed style property 'font-size' of field
52+
String cssValue = driver.findElement(By.name("color_input")).getCssValue("font-size");
53+
assertEquals(cssValue, "13.3333px");
5354

54-
// GetText
55-
// Retrieves the text of the element
56-
String text = driver.findElement(By.tagName("h1")).getText();
57-
assertEquals(text, "Testing Inputs");
5855

56+
// GetText
57+
// Retrieves the text of the element
58+
String text = driver.findElement(By.tagName("h1")).getText();
59+
assertEquals(text, "Testing Inputs");
5960

60-
// FetchAttributes
61-
// identify the email text box
62-
WebElement emailTxt = driver.findElement(By.name(("email_input")));
63-
// fetch the value property associated with the textbox
64-
String valueInfo = emailTxt.getAttribute("value");
65-
assertEquals(valueInfo,"admin@localhost");
6661

62+
// FetchAttributes
63+
// identify the email text box
64+
WebElement emailTxt = driver.findElement(By.name(("email_input")));
65+
// fetch the value property associated with the textbox
66+
String valueInfo = emailTxt.getAttribute("value");
67+
assertEquals(valueInfo, "admin@localhost");
6768

68-
driver.quit();
69+
driver.quit();
6970
}
7071

7172
}

examples/java/src/test/java/dev/selenium/getting_started/UsingSeleniumTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ public void eightComponents() {
4343
}
4444

4545
@AfterEach
46-
public void teardown() {
47-
driver.quit();
46+
public final void teardown() {
47+
if (driver != null) {
48+
driver.quit();
49+
}
4850
}
4951

5052
}

examples/java/src/test/java/dev/selenium/interactions/AlertsTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ public void createSession() {
4444
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
4545
}
4646

47-
@AfterEach
48-
public void endSession() {
49-
driver.quit();
50-
}
51-
5247
@Test
5348
public void alertInformationTest() {
5449
driver.get("https://www.selenium.dev/selenium/web/alerts.html#");

0 commit comments

Comments
 (0)