1818package org .openqa .selenium ;
1919
2020import static org .assertj .core .api .Assertions .assertThat ;
21- import static org .junit .jupiter .api .Assertions .fail ;
2221import static org .junit .jupiter .api .Assumptions .assumeTrue ;
2322import static org .openqa .selenium .support .ui .ExpectedConditions .frameToBeAvailableAndSwitchToIt ;
2423import static org .openqa .selenium .support .ui .ExpectedConditions .titleIs ;
@@ -108,7 +107,7 @@ void testGetScreenshotAsBinary() {
108107
109108 @ Test
110109 @ Ignore (value = CHROME , gitHubActions = true )
111- public void testShouldCaptureScreenshotOfCurrentViewport () {
110+ public void testShouldCaptureScreenshotOfCurrentViewport () throws IOException {
112111 driver .get (appServer .whereIs ("screen/screen.html" ));
113112
114113 BufferedImage screenshot = getImage ();
@@ -147,7 +146,7 @@ void testShouldCaptureScreenshotOfAnElement() throws Exception {
147146
148147 @ Test
149148 @ Ignore (value = CHROME , gitHubActions = true )
150- public void testShouldCaptureScreenshotAtFramePage () {
149+ public void testShouldCaptureScreenshotAtFramePage () throws IOException {
151150 driver .get (appServer .whereIs ("screen/screen_frames.html" ));
152151 wait .until (frameToBeAvailableAndSwitchToIt (By .id ("frame1" )));
153152 wait .until (visibilityOfAllElementsLocatedBy (By .id ("content" )));
@@ -184,7 +183,7 @@ public void testShouldCaptureScreenshotAtFramePage() {
184183 @ Test
185184 @ Ignore (CHROME )
186185 @ Ignore (EDGE )
187- public void testShouldCaptureScreenshotAtIFramePage () {
186+ public void testShouldCaptureScreenshotAtIFramePage () throws IOException {
188187 driver .get (appServer .whereIs ("screen/screen_iframes.html" ));
189188
190189 BufferedImage screenshot = getImage ();
@@ -214,7 +213,7 @@ public void testShouldCaptureScreenshotAtIFramePage() {
214213 @ Test
215214 @ Ignore (FIREFOX )
216215 @ Ignore (value = CHROME , gitHubActions = true )
217- public void testShouldCaptureScreenshotAtFramePageAfterSwitching () {
216+ public void testShouldCaptureScreenshotAtFramePageAfterSwitching () throws IOException {
218217 driver .get (appServer .whereIs ("screen/screen_frames.html" ));
219218
220219 driver .switchTo ().frame (driver .findElement (By .id ("frame2" )));
@@ -248,7 +247,7 @@ public void testShouldCaptureScreenshotAtFramePageAfterSwitching() {
248247 @ Ignore (CHROME )
249248 @ Ignore (EDGE )
250249 @ Ignore (FIREFOX )
251- public void testShouldCaptureScreenshotAtIFramePageAfterSwitching () {
250+ public void testShouldCaptureScreenshotAtIFramePageAfterSwitching () throws IOException {
252251 driver .get (appServer .whereIs ("screen/screen_iframes.html" ));
253252
254253 driver .switchTo ().frame (driver .findElement (By .id ("iframe2" )));
@@ -272,7 +271,7 @@ public void testShouldCaptureScreenshotAtIFramePageAfterSwitching() {
272271 /* grid X size */ 6 ,
273272 /* grid Y size */ 6 ));
274273
275- // expectation is that screenshot at page with Iframes after switching to a Iframe
274+ // expectation is that screenshot at page with Iframes after switching to iframe
276275 // will be taken for full page
277276 compareColors (expectedColors , actualColors );
278277 }
@@ -282,17 +281,12 @@ public void testShouldCaptureScreenshotAtIFramePageAfterSwitching() {
282281 *
283282 * @return Image object
284283 */
285- private BufferedImage getImage () {
286- BufferedImage image = null ;
287- try {
288- byte [] imageData = screenshooter .getScreenshotAs (OutputType .BYTES );
289- assertThat (imageData ).isNotNull ();
290- assertThat (imageData .length ).isPositive ();
291- image = ImageIO .read (new ByteArrayInputStream (imageData ));
292- assertThat (image ).isNotNull ();
293- } catch (IOException e ) {
294- fail ("Image screenshot file is invalid: " + e .getMessage ());
295- }
284+ private BufferedImage getImage () throws IOException {
285+ byte [] imageData = screenshooter .getScreenshotAs (OutputType .BYTES );
286+ assertThat (imageData ).isNotNull ();
287+ assertThat (imageData .length ).isPositive ();
288+ BufferedImage image = ImageIO .read (new ByteArrayInputStream (imageData ));
289+ assertThat (image ).isNotNull ();
296290
297291 // saveImageToTmpFile(image);
298292 return image ;
@@ -337,28 +331,23 @@ private Set<String> generateExpectedColors(
337331 private Set <String > scanActualColors (BufferedImage image , final int stepX , final int stepY ) {
338332 Set <String > colors = new TreeSet <>();
339333
340- try {
341- int height = image .getHeight ();
342- int width = image .getWidth ();
343- assertThat (width > 0 ).isTrue ();
344- assertThat (height > 0 ).isTrue ();
345-
346- Raster raster = image .getRaster ();
347- for (int i = 0 ; i < width ; i = i + stepX ) {
348- for (int j = 0 ; j < height ; j = j + stepY ) {
349- String hex =
350- String .format (
351- "#%02x%02x%02x" ,
352- (raster .getSample (i , j , 0 )),
353- (raster .getSample (i , j , 1 )),
354- (raster .getSample (i , j , 2 )));
355- colors .add (hex );
356- }
334+ int height = image .getHeight ();
335+ int width = image .getWidth ();
336+ assertThat (width > 0 ).isTrue ();
337+ assertThat (height > 0 ).isTrue ();
338+
339+ Raster raster = image .getRaster ();
340+ for (int i = 0 ; i < width ; i = i + stepX ) {
341+ for (int j = 0 ; j < height ; j = j + stepY ) {
342+ String hex =
343+ String .format (
344+ "#%02x%02x%02x" ,
345+ (raster .getSample (i , j , 0 )),
346+ (raster .getSample (i , j , 1 )),
347+ (raster .getSample (i , j , 2 )));
348+ colors .add (hex );
357349 }
358- } catch (Exception e ) {
359- fail ("Unable to get actual colors from screenshot: " + e .getMessage ());
360350 }
361-
362351 assertThat (colors ).isNotEmpty ();
363352
364353 return colors ;
@@ -379,17 +368,7 @@ private void compareColors(Set<String> expectedColors, Set<String> actualColors)
379368 cleanActualColors .remove ("#000000" );
380369 cleanActualColors .remove ("#ffffff" );
381370
382- if (!expectedColors .containsAll (cleanActualColors )) {
383- fail (
384- "There are unexpected colors on the screenshot: "
385- + Sets .difference (cleanActualColors , expectedColors ));
386- }
387-
388- if (!cleanActualColors .containsAll (expectedColors )) {
389- fail (
390- "There are expected colors not present on the screenshot: "
391- + Sets .difference (expectedColors , cleanActualColors ));
392- }
371+ assertThat (cleanActualColors ).containsExactlyInAnyOrderElementsOf (expectedColors );
393372 }
394373
395374 private boolean onlyBlack (Set <String > colors ) {
@@ -406,13 +385,8 @@ private boolean onlyWhite(Set<String> colors) {
406385 * @param im image
407386 */
408387 @ SuppressWarnings ("unused" )
409- private void saveImageToTmpFile (String testMethodName , BufferedImage im ) {
410-
388+ private void saveImageToTmpFile (String testMethodName , BufferedImage im ) throws IOException {
411389 File outputfile = new File (testMethodName + "_image.png" );
412- try {
413- ImageIO .write (im , "png" , outputfile );
414- } catch (IOException e ) {
415- fail ("Unable to write image to file: " + e .getMessage ());
416- }
390+ ImageIO .write (im , "png" , outputfile );
417391 }
418392}
0 commit comments