1919
2020import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
2121import static org .assertj .core .api .AssertionsForClassTypes .assertThatThrownBy ;
22+ import static org .junit .jupiter .api .Assertions .fail ;
2223import static org .openqa .selenium .testing .drivers .Browser .*;
2324
2425import java .time .Duration ;
2526import java .time .temporal .ChronoUnit ;
2627import java .util .Collections ;
2728import java .util .concurrent .CountDownLatch ;
2829import java .util .concurrent .TimeUnit ;
30+ import java .util .concurrent .atomic .AtomicInteger ;
2931import org .junit .jupiter .api .Disabled ;
3032import org .junit .jupiter .api .Test ;
31- import org .openqa .selenium .Alert ;
3233import org .openqa .selenium .By ;
33- import org .openqa .selenium .TimeoutException ;
3434import org .openqa .selenium .UsernameAndPassword ;
3535import org .openqa .selenium .WebDriverException ;
3636import org .openqa .selenium .WindowType ;
3737import org .openqa .selenium .bidi .BiDiException ;
3838import org .openqa .selenium .bidi .browsingcontext .BrowsingContext ;
39+ import org .openqa .selenium .bidi .browsingcontext .ReadinessState ;
3940import org .openqa .selenium .bidi .module .Network ;
40- import org .openqa .selenium .support .ui .ExpectedConditions ;
4141import org .openqa .selenium .testing .JupiterTestBase ;
4242import org .openqa .selenium .testing .NeedsFreshDriver ;
4343import org .openqa .selenium .testing .NotYetImplemented ;
@@ -47,8 +47,6 @@ class NetworkCommandsTest extends JupiterTestBase {
4747
4848 @ Test
4949 @ NeedsFreshDriver
50- @ NotYetImplemented (EDGE )
51- @ NotYetImplemented (CHROME )
5250 void canAddIntercept () {
5351 try (Network network = new Network (driver )) {
5452 String intercept =
@@ -60,7 +58,6 @@ void canAddIntercept() {
6058 @ Test
6159 @ NeedsFreshDriver
6260 @ NotYetImplemented (EDGE )
63- @ NotYetImplemented (CHROME )
6461 void canContinueRequest () throws InterruptedException {
6562 try (Network network = new Network (driver )) {
6663 String intercept =
@@ -84,17 +81,17 @@ void canContinueRequest() throws InterruptedException {
8481
8582 assertThat (intercept ).isNotNull ();
8683
87- driver . get ( appServer . whereIs ( "/bidi/logEntryAdded.html" ));
84+ BrowsingContext browsingContext = new BrowsingContext ( driver , driver . getWindowHandle ( ));
8885
86+ browsingContext .navigate (
87+ appServer .whereIs ("/bidi/logEntryAdded.html" ), ReadinessState .COMPLETE );
8988 boolean countdown = latch .await (5 , TimeUnit .SECONDS );
9089 assertThat (countdown ).isTrue ();
9190 }
9291 }
9392
9493 @ Test
9594 @ NeedsFreshDriver
96- @ NotYetImplemented (EDGE )
97- @ NotYetImplemented (CHROME )
9895 void canContinueResponse () throws InterruptedException {
9996 try (Network network = new Network (driver )) {
10097 String intercept =
@@ -113,7 +110,10 @@ void canContinueResponse() throws InterruptedException {
113110
114111 assertThat (intercept ).isNotNull ();
115112
116- driver .get (appServer .whereIs ("/bidi/logEntryAdded.html" ));
113+ BrowsingContext browsingContext = new BrowsingContext (driver , driver .getWindowHandle ());
114+
115+ browsingContext .navigate (
116+ appServer .whereIs ("/bidi/logEntryAdded.html" ), ReadinessState .COMPLETE );
117117
118118 boolean countdown = latch .await (5 , TimeUnit .SECONDS );
119119 assertThat (countdown ).isTrue ();
@@ -122,8 +122,6 @@ void canContinueResponse() throws InterruptedException {
122122
123123 @ Test
124124 @ NeedsFreshDriver
125- @ NotYetImplemented (EDGE )
126- @ NotYetImplemented (CHROME )
127125 void canProvideResponse () throws InterruptedException {
128126 try (Network network = new Network (driver )) {
129127 String intercept =
@@ -141,7 +139,10 @@ void canProvideResponse() throws InterruptedException {
141139
142140 assertThat (intercept ).isNotNull ();
143141
144- driver .get (appServer .whereIs ("/bidi/logEntryAdded.html" ));
142+ BrowsingContext browsingContext = new BrowsingContext (driver , driver .getWindowHandle ());
143+
144+ browsingContext .navigate (
145+ appServer .whereIs ("/bidi/logEntryAdded.html" ), ReadinessState .COMPLETE );
145146
146147 boolean countdown = latch .await (5 , TimeUnit .SECONDS );
147148 assertThat (countdown ).isTrue ();
@@ -174,7 +175,10 @@ void canProvideResponseWithAllParameters() throws InterruptedException {
174175
175176 assertThat (intercept ).isNotNull ();
176177
177- driver .get (appServer .whereIs ("/bidi/logEntryAdded.html" ));
178+ BrowsingContext browsingContext = new BrowsingContext (driver , driver .getWindowHandle ());
179+
180+ browsingContext .navigate (
181+ appServer .whereIs ("/bidi/logEntryAdded.html" ), ReadinessState .COMPLETE );
178182
179183 boolean countdown = latch .await (5 , TimeUnit .SECONDS );
180184 assertThat (countdown ).isTrue ();
@@ -185,8 +189,6 @@ void canProvideResponseWithAllParameters() throws InterruptedException {
185189
186190 @ Test
187191 @ NeedsFreshDriver
188- @ NotYetImplemented (EDGE )
189- @ NotYetImplemented (CHROME )
190192 void canRemoveIntercept () {
191193 try (Network network = new Network (driver )) {
192194 String intercept =
@@ -199,8 +201,6 @@ void canRemoveIntercept() {
199201
200202 @ Test
201203 @ NeedsFreshDriver
202- @ NotYetImplemented (EDGE )
203- @ NotYetImplemented (CHROME )
204204 void canContinueWithAuthCredentials () {
205205 try (Network network = new Network (driver )) {
206206 network .addIntercept (new AddInterceptParameters (InterceptPhase .AUTH_REQUIRED ));
@@ -211,52 +211,79 @@ void canContinueWithAuthCredentials() {
211211 new UsernameAndPassword ("test" , "test" )));
212212
213213 page = appServer .whereIs ("basicAuth" );
214- driver .get (page );
214+ BrowsingContext browsingContext = new BrowsingContext (driver , driver .getWindowHandle ());
215+
216+ browsingContext .navigate (page , ReadinessState .COMPLETE );
217+
215218 assertThat (driver .findElement (By .tagName ("h1" )).getText ()).isEqualTo ("authorized" );
216219 }
217220 }
218221
219222 @ Test
220223 @ NeedsFreshDriver
221- @ NotYetImplemented (EDGE )
222- @ NotYetImplemented (CHROME )
223224 void canContinueWithoutAuthCredentials () {
224225 try (Network network = new Network (driver )) {
225226 network .addIntercept (new AddInterceptParameters (InterceptPhase .AUTH_REQUIRED ));
226227 network .onAuthRequired (
227- responseDetails ->
228- // Does not handle the alert
229- network .continueWithAuthNoCredentials (responseDetails .getRequest ().getRequestId ()));
228+ responseDetails -> {
229+ if (responseDetails .getRequest ().getUrl ().contains ("basicAuth" )) {
230+ network .continueWithAuthNoCredentials (responseDetails .getRequest ().getRequestId ());
231+ }
232+ });
230233 page = appServer .whereIs ("basicAuth" );
231- driver .get (page );
232- // This would fail if alert was handled
233- Alert alert = wait .until (ExpectedConditions .alertIsPresent ());
234- alert .dismiss ();
234+ BrowsingContext browsingContext = new BrowsingContext (driver , driver .getWindowHandle ());
235+
236+ try {
237+ browsingContext .navigate (page , ReadinessState .COMPLETE );
238+ fail ("Exception should be thrown" );
239+ } catch (Exception e ) {
240+ assertThat (e ).isInstanceOf (WebDriverException .class );
241+ }
235242 }
236243 }
237244
238245 @ Test
239246 @ NeedsFreshDriver
240- @ NotYetImplemented (EDGE )
241- @ NotYetImplemented (CHROME )
242- void canCancelAuth () {
247+ void canCancelAuth () throws InterruptedException {
243248 try (Network network = new Network (driver )) {
244249 network .addIntercept (new AddInterceptParameters (InterceptPhase .AUTH_REQUIRED ));
245250 network .onAuthRequired (
246- responseDetails ->
251+ responseDetails -> {
252+ if (responseDetails .getRequest ().getUrl ().contains ("basicAuth" )) {
247253 // Does not handle the alert
248- network .cancelAuth (responseDetails .getRequest ().getRequestId ()));
254+ network .cancelAuth (responseDetails .getRequest ().getRequestId ());
255+ }
256+ });
257+
258+ AtomicInteger status = new AtomicInteger ();
259+ CountDownLatch latch = new CountDownLatch (1 );
260+
261+ network .onResponseCompleted (
262+ responseDetails -> {
263+ if (responseDetails .getRequest ().getUrl ().contains ("basicAuth" )) {
264+ status .set (responseDetails .getResponseData ().getStatus ());
265+ latch .countDown ();
266+ }
267+ });
268+
249269 page = appServer .whereIs ("basicAuth" );
250- driver .get (page );
251- assertThatThrownBy (() -> wait .until (ExpectedConditions .alertIsPresent ()))
252- .isInstanceOf (TimeoutException .class );
270+ BrowsingContext browsingContext = new BrowsingContext (driver , driver .getWindowHandle ());
271+
272+ try {
273+ browsingContext .navigate (page , ReadinessState .COMPLETE );
274+ } catch (Exception BiDiException ) {
275+ // Ignore
276+ // Only Chromium browsers throw an error because the navigation did not complete as
277+ // expected.
278+ }
279+
280+ latch .await (10 , TimeUnit .SECONDS );
281+ assertThat (status .get ()).isEqualTo (401 );
253282 }
254283 }
255284
256285 @ Test
257286 @ NeedsFreshDriver
258- @ NotYetImplemented (EDGE )
259- @ NotYetImplemented (CHROME )
260287 void canFailRequest () {
261288 try (Network network = new Network (driver )) {
262289 network .addIntercept (new AddInterceptParameters (InterceptPhase .BEFORE_REQUEST_SENT ));
@@ -265,7 +292,14 @@ void canFailRequest() {
265292 page = appServer .whereIs ("basicAuth" );
266293 driver .manage ().timeouts ().pageLoadTimeout (Duration .of (5 , ChronoUnit .SECONDS ));
267294
268- assertThatThrownBy (() -> driver .get (page )).isInstanceOf (WebDriverException .class );
295+ assertThatThrownBy (
296+ () -> {
297+ BrowsingContext browsingContext =
298+ new BrowsingContext (driver , driver .getWindowHandle ());
299+ browsingContext .navigate (
300+ appServer .whereIs ("/bidi/logEntryAdded.html" ), ReadinessState .COMPLETE );
301+ })
302+ .isInstanceOf (WebDriverException .class );
269303 }
270304 }
271305
0 commit comments