1818package org .openqa .selenium .bidi .browsingcontext ;
1919
2020import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
21- import static org .openqa .selenium .testing .Safely .safelyCall ;
22- import static org .openqa .selenium .testing .drivers .Browser .*;
2321
2422import java .util .concurrent .CompletableFuture ;
2523import java .util .concurrent .ExecutionException ;
2624import java .util .concurrent .TimeUnit ;
2725import java .util .concurrent .TimeoutException ;
28- import org .junit .jupiter .api .AfterEach ;
29- import org .junit .jupiter .api .BeforeEach ;
3026import org .junit .jupiter .api .Test ;
3127import org .openqa .selenium .By ;
3228import org .openqa .selenium .WindowType ;
3329import org .openqa .selenium .bidi .module .BrowsingContextInspector ;
34- import org .openqa .selenium .environment .webserver .AppServer ;
35- import org .openqa .selenium .environment .webserver .NettyAppServer ;
3630import org .openqa .selenium .testing .JupiterTestBase ;
31+ import org .openqa .selenium .testing .NeedsFreshDriver ;
3732
3833class BrowsingContextInspectorTest extends JupiterTestBase {
3934
40- private AppServer server ;
41-
42- @ BeforeEach
43- public void setUp () {
44- server = new NettyAppServer ();
45- server .start ();
46- }
47-
4835 @ Test
36+ @ NeedsFreshDriver
4937 void canListenToWindowBrowsingContextCreatedEvent ()
5038 throws ExecutionException , InterruptedException , TimeoutException {
5139 try (BrowsingContextInspector inspector = new BrowsingContextInspector (driver )) {
@@ -66,6 +54,7 @@ void canListenToWindowBrowsingContextCreatedEvent()
6654 }
6755
6856 @ Test
57+ @ NeedsFreshDriver
6958 void canListenToBrowsingContextDestroyedEvent ()
7059 throws ExecutionException , InterruptedException , TimeoutException {
7160 try (BrowsingContextInspector inspector = new BrowsingContextInspector (driver )) {
@@ -87,6 +76,7 @@ void canListenToBrowsingContextDestroyedEvent()
8776 }
8877
8978 @ Test
79+ @ NeedsFreshDriver
9080 void canListenToTabBrowsingContextCreatedEvent ()
9181 throws ExecutionException , InterruptedException , TimeoutException {
9282 try (BrowsingContextInspector inspector = new BrowsingContextInspector (driver )) {
@@ -106,14 +96,15 @@ void canListenToTabBrowsingContextCreatedEvent()
10696 }
10797
10898 @ Test
99+ @ NeedsFreshDriver
109100 void canListenToDomContentLoadedEvent ()
110101 throws ExecutionException , InterruptedException , TimeoutException {
111102 try (BrowsingContextInspector inspector = new BrowsingContextInspector (driver )) {
112103 CompletableFuture <NavigationInfo > future = new CompletableFuture <>();
113104 inspector .onDomContentLoaded (future ::complete );
114105
115106 BrowsingContext context = new BrowsingContext (driver , driver .getWindowHandle ());
116- context .navigate (server .whereIs ("/bidi/logEntryAdded.html" ), ReadinessState .COMPLETE );
107+ context .navigate (appServer .whereIs ("/bidi/logEntryAdded.html" ), ReadinessState .COMPLETE );
117108
118109 NavigationInfo navigationInfo = future .get (5 , TimeUnit .SECONDS );
119110 assertThat (navigationInfo .getBrowsingContextId ()).isEqualTo (context .getId ());
@@ -122,14 +113,15 @@ void canListenToDomContentLoadedEvent()
122113 }
123114
124115 @ Test
116+ @ NeedsFreshDriver
125117 void canListenToBrowsingContextLoadedEvent ()
126118 throws ExecutionException , InterruptedException , TimeoutException {
127119 try (BrowsingContextInspector inspector = new BrowsingContextInspector (driver )) {
128120 CompletableFuture <NavigationInfo > future = new CompletableFuture <>();
129121 inspector .onBrowsingContextLoaded (future ::complete );
130122
131123 BrowsingContext context = new BrowsingContext (driver , driver .getWindowHandle ());
132- context .navigate (server .whereIs ("/bidi/logEntryAdded.html" ), ReadinessState .COMPLETE );
124+ context .navigate (appServer .whereIs ("/bidi/logEntryAdded.html" ), ReadinessState .COMPLETE );
133125
134126 NavigationInfo navigationInfo = future .get (5 , TimeUnit .SECONDS );
135127 assertThat (navigationInfo .getBrowsingContextId ()).isEqualTo (context .getId ());
@@ -138,14 +130,15 @@ void canListenToBrowsingContextLoadedEvent()
138130 }
139131
140132 @ Test
133+ @ NeedsFreshDriver
141134 void canListenToNavigationStartedEvent ()
142135 throws ExecutionException , InterruptedException , TimeoutException {
143136 try (BrowsingContextInspector inspector = new BrowsingContextInspector (driver )) {
144137 CompletableFuture <NavigationInfo > future = new CompletableFuture <>();
145138 inspector .onNavigationStarted (future ::complete );
146139
147140 BrowsingContext context = new BrowsingContext (driver , driver .getWindowHandle ());
148- context .navigate (server .whereIs ("/bidi/logEntryAdded.html" ), ReadinessState .COMPLETE );
141+ context .navigate (appServer .whereIs ("/bidi/logEntryAdded.html" ), ReadinessState .COMPLETE );
149142
150143 NavigationInfo navigationInfo = future .get (5 , TimeUnit .SECONDS );
151144 assertThat (navigationInfo .getBrowsingContextId ()).isEqualTo (context .getId ());
@@ -154,18 +147,19 @@ void canListenToNavigationStartedEvent()
154147 }
155148
156149 @ Test
150+ @ NeedsFreshDriver
157151 void canListenToFragmentNavigatedEvent ()
158152 throws ExecutionException , InterruptedException , TimeoutException {
159153 try (BrowsingContextInspector inspector = new BrowsingContextInspector (driver )) {
160154 CompletableFuture <NavigationInfo > future = new CompletableFuture <>();
161155
162156 BrowsingContext context = new BrowsingContext (driver , driver .getWindowHandle ());
163- context .navigate (server .whereIs ("/linked_image.html" ), ReadinessState .COMPLETE );
157+ context .navigate (appServer .whereIs ("/linked_image.html" ), ReadinessState .COMPLETE );
164158
165159 inspector .onFragmentNavigated (future ::complete );
166160
167161 context .navigate (
168- server .whereIs ("/linked_image.html#linkToAnchorOnThisPage" ), ReadinessState .COMPLETE );
162+ appServer .whereIs ("/linked_image.html#linkToAnchorOnThisPage" ), ReadinessState .COMPLETE );
169163
170164 NavigationInfo navigationInfo = future .get (5 , TimeUnit .SECONDS );
171165 assertThat (navigationInfo .getBrowsingContextId ()).isEqualTo (context .getId ());
@@ -174,6 +168,7 @@ void canListenToFragmentNavigatedEvent()
174168 }
175169
176170 @ Test
171+ @ NeedsFreshDriver
177172 void canListenToUserPromptOpenedEvent ()
178173 throws ExecutionException , InterruptedException , TimeoutException {
179174 try (BrowsingContextInspector inspector = new BrowsingContextInspector (driver )) {
@@ -182,7 +177,7 @@ void canListenToUserPromptOpenedEvent()
182177 BrowsingContext context = new BrowsingContext (driver , driver .getWindowHandle ());
183178 inspector .onUserPromptOpened (future ::complete );
184179
185- driver .get (server .whereIs ("/alerts.html" ));
180+ driver .get (appServer .whereIs ("/alerts.html" ));
186181
187182 driver .findElement (By .id ("alert" )).click ();
188183
@@ -193,6 +188,7 @@ void canListenToUserPromptOpenedEvent()
193188 }
194189
195190 @ Test
191+ @ NeedsFreshDriver
196192 // TODO: This test is flaky for comparing the browsing context id for Chrome and Edge. Fix flaky
197193 // test.
198194 void canListenToUserPromptClosedEvent ()
@@ -203,7 +199,7 @@ void canListenToUserPromptClosedEvent()
203199 BrowsingContext context = new BrowsingContext (driver , driver .getWindowHandle ());
204200 inspector .onUserPromptClosed (future ::complete );
205201
206- driver .get (server .whereIs ("/alerts.html" ));
202+ driver .get (appServer .whereIs ("/alerts.html" ));
207203
208204 driver .findElement (By .id ("prompt" )).click ();
209205
@@ -216,12 +212,4 @@ void canListenToUserPromptClosedEvent()
216212 assertThat (userPromptClosed .getAccepted ()).isTrue ();
217213 }
218214 }
219-
220- @ AfterEach
221- public void quitDriver () {
222- if (driver != null ) {
223- driver .quit ();
224- }
225- safelyCall (server ::stop );
226- }
227215}
0 commit comments