Skip to content

Commit 53836c7

Browse files
committed
[java] use annotations to restart the driver in unittests
1 parent 97c5118 commit 53836c7

File tree

3 files changed

+32
-59
lines changed

3 files changed

+32
-59
lines changed

java/test/org/openqa/selenium/WebNetworkTest.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,34 @@
2222

2323
import java.net.URI;
2424
import java.util.function.Predicate;
25-
import org.junit.jupiter.api.AfterEach;
26-
import org.junit.jupiter.api.BeforeEach;
2725
import org.junit.jupiter.api.Test;
28-
import org.openqa.selenium.environment.webserver.AppServer;
29-
import org.openqa.selenium.environment.webserver.NettyAppServer;
3026
import org.openqa.selenium.remote.RemoteWebDriver;
3127
import org.openqa.selenium.testing.Ignore;
3228
import org.openqa.selenium.testing.JupiterTestBase;
29+
import org.openqa.selenium.testing.NeedsFreshDriver;
3330
import org.openqa.selenium.testing.drivers.Browser;
3431

3532
class WebNetworkTest extends JupiterTestBase {
3633

3734
private String page;
38-
private AppServer server;
39-
40-
@BeforeEach
41-
public void setUp() {
42-
server = new NettyAppServer();
43-
server.start();
44-
}
45-
46-
@AfterEach
47-
public void cleanUp() {
48-
driver.quit();
49-
}
5035

5136
@Test
37+
@NeedsFreshDriver
5238
@Ignore(Browser.CHROME)
5339
@Ignore(Browser.EDGE)
5440
void canAddAuthenticationHandler() {
5541
((RemoteWebDriver) driver)
5642
.network()
5743
.addAuthenticationHandler(new UsernameAndPassword("test", "test"));
5844

59-
page = server.whereIs("basicAuth");
45+
page = appServer.whereIs("basicAuth");
6046
driver.get(page);
6147

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

6551
@Test
52+
@NeedsFreshDriver
6653
@Ignore(Browser.CHROME)
6754
@Ignore(Browser.EDGE)
6855
void canAddAuthenticationHandlerWithFilter() {
@@ -72,13 +59,14 @@ void canAddAuthenticationHandlerWithFilter() {
7259
.network()
7360
.addAuthenticationHandler(filter, new UsernameAndPassword("test", "test"));
7461

75-
page = server.whereIs("basicAuth");
62+
page = appServer.whereIs("basicAuth");
7663
driver.get(page);
7764

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

8168
@Test
69+
@NeedsFreshDriver
8270
@Ignore(Browser.CHROME)
8371
@Ignore(Browser.EDGE)
8472
void canAddMultipleAuthenticationHandlersWithFilter() {
@@ -92,13 +80,14 @@ void canAddMultipleAuthenticationHandlersWithFilter() {
9280
.addAuthenticationHandler(
9381
uri -> uri.getPath().contains("test"), new UsernameAndPassword("test1", "test1"));
9482

95-
page = server.whereIs("basicAuth");
83+
page = appServer.whereIs("basicAuth");
9684
driver.get(page);
9785

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

10189
@Test
90+
@NeedsFreshDriver
10291
@Ignore(Browser.CHROME)
10392
@Ignore(Browser.EDGE)
10493
void canAddMultipleAuthenticationHandlersWithTheSameFilter() {
@@ -112,13 +101,14 @@ void canAddMultipleAuthenticationHandlersWithTheSameFilter() {
112101
.addAuthenticationHandler(
113102
uri -> uri.getPath().contains("basicAuth"), new UsernameAndPassword("test", "test"));
114103

115-
page = server.whereIs("basicAuth");
104+
page = appServer.whereIs("basicAuth");
116105
driver.get(page);
117106

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

121110
@Test
111+
@NeedsFreshDriver
122112
@Ignore(Browser.CHROME)
123113
@Ignore(Browser.EDGE)
124114
void canRemoveAuthenticationHandler() {
@@ -128,26 +118,28 @@ void canRemoveAuthenticationHandler() {
128118
.addAuthenticationHandler(new UsernameAndPassword("test", "test"));
129119

130120
((RemoteWebDriver) driver).network().removeAuthenticationHandler(id);
131-
page = server.whereIs("basicAuth");
121+
page = appServer.whereIs("basicAuth");
132122
driver.get(page);
133123

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

138128
@Test
129+
@NeedsFreshDriver
139130
@Ignore(Browser.CHROME)
140131
@Ignore(Browser.EDGE)
141132
void canRemoveAuthenticationHandlerThatDoesNotExist() {
142133
((RemoteWebDriver) driver).network().removeAuthenticationHandler(5);
143-
page = server.whereIs("basicAuth");
134+
page = appServer.whereIs("basicAuth");
144135
driver.get(page);
145136

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

150141
@Test
142+
@NeedsFreshDriver
151143
@Ignore(Browser.CHROME)
152144
@Ignore(Browser.EDGE)
153145
void canClearAuthenticationHandlers() {
@@ -165,7 +157,7 @@ void canClearAuthenticationHandlers() {
165157
.addAuthenticationHandler(new UsernameAndPassword("test1", "test1"));
166158

167159
((RemoteWebDriver) driver).network().clearAuthenticationHandlers();
168-
page = server.whereIs("basicAuth");
160+
page = appServer.whereIs("basicAuth");
169161
driver.get(page);
170162

171163
assertThatExceptionOfType(UnhandledAlertException.class)

java/test/org/openqa/selenium/WebScriptTest.java

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,43 +28,29 @@
2828
import java.util.concurrent.atomic.AtomicReference;
2929
import java.util.function.Consumer;
3030
import org.assertj.core.api.Assertions;
31-
import org.junit.jupiter.api.AfterEach;
32-
import org.junit.jupiter.api.BeforeEach;
3331
import org.junit.jupiter.api.Test;
3432
import org.openqa.selenium.bidi.log.ConsoleLogEntry;
3533
import org.openqa.selenium.bidi.log.JavascriptLogEntry;
3634
import org.openqa.selenium.bidi.log.LogLevel;
37-
import org.openqa.selenium.environment.webserver.AppServer;
38-
import org.openqa.selenium.environment.webserver.NettyAppServer;
3935
import org.openqa.selenium.remote.DomMutation;
4036
import org.openqa.selenium.remote.RemoteWebDriver;
4137
import org.openqa.selenium.support.ui.WebDriverWait;
4238
import org.openqa.selenium.testing.JupiterTestBase;
39+
import org.openqa.selenium.testing.NeedsFreshDriver;
4340

4441
class WebScriptTest extends JupiterTestBase {
4542

4643
String page;
47-
private AppServer server;
48-
49-
@BeforeEach
50-
public void setUp() {
51-
server = new NettyAppServer();
52-
server.start();
53-
}
54-
55-
@AfterEach
56-
public void cleanUp() {
57-
driver.quit();
58-
}
5944

6045
@Test
46+
@NeedsFreshDriver
6147
void canAddConsoleMessageHandler()
6248
throws ExecutionException, InterruptedException, TimeoutException {
6349
CompletableFuture<ConsoleLogEntry> future = new CompletableFuture<>();
6450

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

67-
page = server.whereIs("/bidi/logEntryAdded.html");
53+
page = appServer.whereIs("/bidi/logEntryAdded.html");
6854
driver.get(page);
6955
driver.findElement(By.id("consoleLog")).click();
7056

@@ -81,6 +67,7 @@ void canAddConsoleMessageHandler()
8167
}
8268

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

99-
page = server.whereIs("/bidi/logEntryAdded.html");
86+
page = appServer.whereIs("/bidi/logEntryAdded.html");
10087
driver.get(page);
10188
driver.findElement(By.id("consoleLog")).click();
10289

@@ -113,12 +100,13 @@ void canRemoveConsoleMessageHandler()
113100
}
114101

115102
@Test
103+
@NeedsFreshDriver
116104
void canAddJsErrorHandler() throws ExecutionException, InterruptedException, TimeoutException {
117105
CompletableFuture<JavascriptLogEntry> future = new CompletableFuture<>();
118106

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

121-
page = server.whereIs("/bidi/logEntryAdded.html");
109+
page = appServer.whereIs("/bidi/logEntryAdded.html");
122110
driver.get(page);
123111
driver.findElement(By.id("jsException")).click();
124112

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

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

149-
page = server.whereIs("/bidi/logEntryAdded.html");
138+
page = appServer.whereIs("/bidi/logEntryAdded.html");
150139
driver.get(page);
151140
driver.findElement(By.id("jsException")).click();
152141

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

168157
@Test
158+
@NeedsFreshDriver
169159
void canAddMultipleHandlers() throws ExecutionException, InterruptedException, TimeoutException {
170160
CompletableFuture<JavascriptLogEntry> future1 = new CompletableFuture<>();
171161
CompletableFuture<JavascriptLogEntry> future2 = new CompletableFuture<>();
@@ -177,7 +167,7 @@ void canAddMultipleHandlers() throws ExecutionException, InterruptedException, T
177167
long id1 = ((RemoteWebDriver) driver).script().addJavaScriptErrorHandler(consumer1);
178168
long id2 = ((RemoteWebDriver) driver).script().addJavaScriptErrorHandler(consumer2);
179169

180-
page = server.whereIs("/bidi/logEntryAdded.html");
170+
page = appServer.whereIs("/bidi/logEntryAdded.html");
181171
driver.get(page);
182172
driver.findElement(By.id("jsException")).click();
183173

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

195185
@Test
186+
@NeedsFreshDriver
196187
void canAddDomMutationHandler() throws InterruptedException {
197188
AtomicReference<DomMutation> seen = new AtomicReference<>();
198189
CountDownLatch latch = new CountDownLatch(1);
@@ -220,6 +211,7 @@ void canAddDomMutationHandler() throws InterruptedException {
220211
}
221212

222213
@Test
214+
@NeedsFreshDriver
223215
void canRemoveDomMutationHandler() throws InterruptedException {
224216
AtomicReference<DomMutation> seen = new AtomicReference<>();
225217
CountDownLatch latch = new CountDownLatch(1);
@@ -247,14 +239,15 @@ void canRemoveDomMutationHandler() throws InterruptedException {
247239
}
248240

249241
@Test
242+
@NeedsFreshDriver
250243
void canPinScript() throws ExecutionException, InterruptedException, TimeoutException {
251244
CompletableFuture<ConsoleLogEntry> future = new CompletableFuture<>();
252245

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

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

257-
page = server.whereIs("/bidi/logEntryAdded.html");
250+
page = appServer.whereIs("/bidi/logEntryAdded.html");
258251
driver.get(page);
259252

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

267260
@Test
261+
@NeedsFreshDriver
268262
void canUnpinScript() throws ExecutionException, InterruptedException, TimeoutException {
269263
CountDownLatch latch = new CountDownLatch(2);
270264

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

279-
page = server.whereIs("/bidi/logEntryAdded.html");
273+
page = appServer.whereIs("/bidi/logEntryAdded.html");
280274

281275
driver.get(page);
282276

java/test/org/openqa/selenium/javascript/JavaScriptTestSuite.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,11 @@
3030
import java.util.function.Function;
3131
import java.util.function.Supplier;
3232
import org.junit.jupiter.api.AfterEach;
33-
import org.junit.jupiter.api.BeforeEach;
3433
import org.junit.jupiter.api.DynamicTest;
3534
import org.junit.jupiter.api.TestFactory;
3635
import org.openqa.selenium.WebDriver;
3736
import org.openqa.selenium.build.InProject;
3837
import org.openqa.selenium.environment.GlobalTestEnvironment;
39-
import org.openqa.selenium.environment.InProcessTestEnvironment;
40-
import org.openqa.selenium.environment.TestEnvironment;
4138
import org.openqa.selenium.environment.webserver.AppServer;
4239
import org.openqa.selenium.testing.drivers.WebDriverBuilder;
4340

@@ -48,8 +45,6 @@ class JavaScriptTestSuite {
4845

4946
private final long timeout;
5047

51-
private TestEnvironment testEnvironment;
52-
5348
public JavaScriptTestSuite() {
5449
this.timeout = Math.max(0, Long.getLong("js.test.timeout", 0));
5550
this.driverSupplier = new DriverSupplier();
@@ -59,16 +54,8 @@ private static boolean isBazel() {
5954
return InProject.findRunfilesRoot() != null;
6055
}
6156

62-
@BeforeEach
63-
public void setup() {
64-
testEnvironment = GlobalTestEnvironment.getOrCreate(InProcessTestEnvironment::new);
65-
}
66-
6757
@AfterEach
6858
public void teardown() throws IOException {
69-
if (testEnvironment != null) {
70-
testEnvironment.stop();
71-
}
7259
if (driverSupplier != null) {
7360
((Closeable) driverSupplier).close();
7461
}

0 commit comments

Comments
 (0)