1717
1818package org .openqa .selenium .bidi .browsingcontext ;
1919
20- import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
21- import static org .assertj .core .api .AssertionsForClassTypes . assertThatExceptionOfType ;
20+ import static org .assertj .core .api .Assertions .assertThat ;
21+ import static org .assertj .core .api .Assertions . assertThatThrownBy ;
2222import static org .openqa .selenium .support .ui .ExpectedConditions .alertIsPresent ;
2323import static org .openqa .selenium .support .ui .ExpectedConditions .titleIs ;
2424import static org .openqa .selenium .support .ui .ExpectedConditions .visibilityOfElementLocated ;
2525
2626import java .util .List ;
2727import org .junit .jupiter .api .Test ;
2828import org .openqa .selenium .By ;
29+ import org .openqa .selenium .Dimension ;
2930import org .openqa .selenium .JavascriptExecutor ;
3031import org .openqa .selenium .Rectangle ;
3132import org .openqa .selenium .WebElement ;
@@ -136,9 +137,9 @@ void canGetTreeWithAChild() {
136137
137138 List <BrowsingContextInfo > contextInfoList = parentWindow .getTree ();
138139
139- assertThat (contextInfoList . size ()). isEqualTo (1 );
140+ assertThat (contextInfoList ). hasSize (1 );
140141 BrowsingContextInfo info = contextInfoList .get (0 );
141- assertThat (info .getChildren (). size ()). isEqualTo (1 );
142+ assertThat (info .getChildren ()). hasSize (1 );
142143 assertThat (info .getId ()).isEqualTo (referenceContextId );
143144 assertThat (info .getChildren ().get (0 ).getUrl ()).contains ("formPage.html" );
144145 }
@@ -155,7 +156,7 @@ void canGetTreeWithDepth() {
155156
156157 List <BrowsingContextInfo > contextInfoList = parentWindow .getTree (0 );
157158
158- assertThat (contextInfoList . size ()). isEqualTo (1 );
159+ assertThat (contextInfoList ). hasSize (1 );
159160 BrowsingContextInfo info = contextInfoList .get (0 );
160161 assertThat (info .getChildren ()).isNull (); // since depth is 0
161162 assertThat (info .getId ()).isEqualTo (referenceContextId );
@@ -176,7 +177,7 @@ void canGetTreeWithRootAndDepth() {
176177
177178 List <BrowsingContextInfo > contextInfoList = parentWindow .getTree (referenceContextId , 1 );
178179
179- assertThat (contextInfoList . size ()). isEqualTo (1 );
180+ assertThat (contextInfoList ). hasSize (1 );
180181 BrowsingContextInfo info = contextInfoList .get (0 );
181182 assertThat (info .getChildren ()).isNotNull (); // since depth is 1
182183 assertThat (info .getId ()).isEqualTo (referenceContextId );
@@ -199,7 +200,7 @@ void canGetTreeWithRoot() {
199200
200201 List <BrowsingContextInfo > contextInfoList = parentWindow .getTree (tab .getId ());
201202
202- assertThat (contextInfoList . size ()). isEqualTo (1 );
203+ assertThat (contextInfoList ). hasSize (1 );
203204 BrowsingContextInfo info = contextInfoList .get (0 );
204205 assertThat (info .getId ()).isEqualTo (tab .getId ());
205206 assertThat (info .getOriginalOpener ()).isNull ();
@@ -215,7 +216,7 @@ void canGetAllTopLevelContexts() {
215216
216217 List <BrowsingContextInfo > contextInfoList = window1 .getTopLevelContexts ();
217218
218- assertThat (contextInfoList . size ()). isEqualTo (2 );
219+ assertThat (contextInfoList ). hasSize (2 );
219220 }
220221
221222 @ Test
@@ -226,7 +227,9 @@ void canCloseAWindow() {
226227
227228 window2 .close ();
228229
229- assertThatExceptionOfType (BiDiException .class ).isThrownBy (window2 ::getTree );
230+ assertThatThrownBy (window2 ::getTree )
231+ .isInstanceOf (BiDiException .class )
232+ .hasMessageContaining ("not found" );
230233 }
231234
232235 @ Test
@@ -237,7 +240,9 @@ void canCloseATab() {
237240
238241 tab2 .close ();
239242
240- assertThatExceptionOfType (BiDiException .class ).isThrownBy (tab2 ::getTree );
243+ assertThatThrownBy (tab2 ::getTree )
244+ .isInstanceOf (BiDiException .class )
245+ .hasMessageContaining ("not found" );
241246 }
242247
243248 @ Test
@@ -397,7 +402,7 @@ void canCaptureScreenshot() {
397402
398403 String screenshot = browsingContext .captureScreenshot ();
399404
400- assertThat (screenshot . length ()). isPositive ();
405+ assertThat (screenshot ). isNotEmpty ();
401406 }
402407
403408 @ Test
@@ -423,7 +428,7 @@ void canCaptureScreenshotWithAllParameters() {
423428 .origin (CaptureScreenshotParameters .Origin .DOCUMENT )
424429 .clipRectangle (clipRectangle ));
425430
426- assertThat (screenshot . length ()). isPositive ();
431+ assertThat (screenshot ). isNotEmpty ();
427432 }
428433
429434 @ Test
@@ -440,7 +445,7 @@ void canCaptureScreenshotOfViewport() {
440445 browsingContext .captureBoxScreenshot (
441446 elementRectangle .getX (), elementRectangle .getY (), 5 , 5 );
442447
443- assertThat (screenshot . length ()). isPositive ();
448+ assertThat (screenshot ). isNotEmpty ();
444449 }
445450
446451 @ Test
@@ -455,46 +460,41 @@ void canCaptureElementScreenshot() {
455460 String screenshot =
456461 browsingContext .captureElementScreenshot (((RemoteWebElement ) element ).getId ());
457462
458- assertThat (screenshot . length ()). isPositive ();
463+ assertThat (screenshot ). isNotEmpty ();
459464 }
460465
461466 @ Test
462467 @ NeedsFreshDriver
463468 void canSetViewport () {
469+ Dimension initialViewportSize = getViewportSize ();
470+
464471 BrowsingContext browsingContext = new BrowsingContext (driver , driver .getWindowHandle ());
465472 driver .get (appServer .whereIs ("formPage.html" ));
466473
467474 browsingContext .setViewport (250 , 300 );
475+ assertThat (getViewportSize ()).isEqualTo (new Dimension (250 , 300 ));
468476
469- List <Long > newViewportSize =
470- (List <Long >)
471- ((JavascriptExecutor ) driver )
472- .executeScript ("return [window.innerWidth, window.innerHeight];" );
473-
474- assertThat (newViewportSize .get (0 )).isEqualTo (250 );
475- assertThat (newViewportSize .get (1 )).isEqualTo (300 );
477+ browsingContext .setViewport (null , null );
478+ assertThat (getViewportSize ()).isEqualTo (initialViewportSize );
476479 }
477480
478481 @ Test
479482 @ NeedsFreshDriver
480483 void canSetViewportWithDevicePixelRatio () {
484+ Dimension initialViewportSize = getViewportSize ();
485+ double initialPixelRation = getDevicePixelRatio ();
486+
481487 BrowsingContext browsingContext = new BrowsingContext (driver , driver .getWindowHandle ());
482488 driver .get (appServer .whereIs ("formPage.html" ));
483489
484- browsingContext .setViewport (250 , 300 , 5 );
490+ browsingContext .setViewport (250 , 300 , 5.5 );
485491
486- List <Long > newViewportSize =
487- (List <Long >)
488- ((JavascriptExecutor ) driver )
489- .executeScript ("return [window.innerWidth, window.innerHeight];" );
492+ assertThat (getViewportSize ()).isEqualTo (new Dimension (250 , 300 ));
493+ assertThat (getDevicePixelRatio ()).isEqualTo (5.5 );
490494
491- assertThat (newViewportSize .get (0 )).isEqualTo (250 );
492- assertThat (newViewportSize .get (1 )).isEqualTo (300 );
493-
494- Long newDevicePixelRatio =
495- (Long ) ((JavascriptExecutor ) driver ).executeScript ("return window.devicePixelRatio" );
496-
497- assertThat (newDevicePixelRatio ).isEqualTo (5 );
495+ browsingContext .setViewport (null , null , null );
496+ assertThat (getViewportSize ()).isEqualTo (initialViewportSize );
497+ assertThat (getDevicePixelRatio ()).isEqualTo (initialPixelRation );
498498 }
499499
500500 @ Test
@@ -507,7 +507,7 @@ void canPrintPage() {
507507
508508 String printPage = browsingContext .print (printOptions );
509509
510- assertThat (printPage . length ()). isPositive ();
510+ assertThat (printPage ). isNotEmpty ();
511511 // Comparing expected PDF is a hard problem.
512512 // As long as we are sending the parameters correctly it should be fine.
513513 // Trusting the browsers to do the right thing.
@@ -568,7 +568,20 @@ private String promptPage() {
568568 "<p id=\" result\" ></p>" ));
569569 }
570570
571+ private <T > T executeScript (String js ) {
572+ return (T ) ((JavascriptExecutor ) driver ).executeScript (js );
573+ }
574+
571575 private boolean getDocumentFocus () {
572- return (boolean ) ((JavascriptExecutor ) driver ).executeScript ("return document.hasFocus();" );
576+ return executeScript ("return document.hasFocus();" );
577+ }
578+
579+ private Dimension getViewportSize () {
580+ List <Number > dimensions = executeScript ("return [window.innerWidth, window.innerHeight];" );
581+ return new Dimension (dimensions .get (0 ).intValue (), dimensions .get (1 ).intValue ());
582+ }
583+
584+ private double getDevicePixelRatio () {
585+ return ((Number ) executeScript ("return window.devicePixelRatio" )).doubleValue ();
573586 }
574587}
0 commit comments