From c0daa55c9f05969cd93bb5436a56c0d56be65734 Mon Sep 17 00:00:00 2001 From: Manuel Sebastian Blanco Date: Wed, 5 Nov 2025 21:57:34 +0100 Subject: [PATCH 1/2] Move scanActualColors to JupiterTestBase and remove duplicates Extract scanActualColors(BufferedImage, int, int) into the test base class JupiterTestBase and remove duplicate implementations from TakesScreenshotTest and org.openqa.selenium.firefox.TakesFullPageScreenshotTest. This centralizes the utility used by screenshot tests so subclasses reuse a single, well-documented implementation and reduces code duplication. Adjusted imports and replaced the local implementations with a short comment pointing to the base method. --- .../openqa/selenium/TakesScreenshotTest.java | 37 ---------------- .../firefox/TakesFullPageScreenshotTest.java | 41 +---------------- .../selenium/testing/JupiterTestBase.java | 44 +++++++++++++++++++ 3 files changed, 45 insertions(+), 77 deletions(-) diff --git a/java/test/org/openqa/selenium/TakesScreenshotTest.java b/java/test/org/openqa/selenium/TakesScreenshotTest.java index 470706aea5590..8f799415972e6 100644 --- a/java/test/org/openqa/selenium/TakesScreenshotTest.java +++ b/java/test/org/openqa/selenium/TakesScreenshotTest.java @@ -326,43 +326,6 @@ private Set generateExpectedColors( return colors; } - /** - * Get colors from image from each point at grid defined by stepX/stepY. - * - * @param image - image - * @param stepX - interval in pixels b/w point in X dimension - * @param stepY - interval in pixels b/w point in Y dimension - * @return set of colors in string hex presentation - */ - private Set scanActualColors(BufferedImage image, final int stepX, final int stepY) { - Set colors = new TreeSet<>(); - - try { - int height = image.getHeight(); - int width = image.getWidth(); - assertThat(width > 0).isTrue(); - assertThat(height > 0).isTrue(); - - Raster raster = image.getRaster(); - for (int i = 0; i < width; i = i + stepX) { - for (int j = 0; j < height; j = j + stepY) { - String hex = - String.format( - "#%02x%02x%02x", - (raster.getSample(i, j, 0)), - (raster.getSample(i, j, 1)), - (raster.getSample(i, j, 2))); - colors.add(hex); - } - } - } catch (Exception e) { - fail("Unable to get actual colors from screenshot: " + e.getMessage()); - } - - assertThat(colors).isNotEmpty(); - - return colors; - } /** * Compares sets of colors are same. diff --git a/java/test/org/openqa/selenium/firefox/TakesFullPageScreenshotTest.java b/java/test/org/openqa/selenium/firefox/TakesFullPageScreenshotTest.java index 978cbc6540f5a..ce487b4858192 100644 --- a/java/test/org/openqa/selenium/firefox/TakesFullPageScreenshotTest.java +++ b/java/test/org/openqa/selenium/firefox/TakesFullPageScreenshotTest.java @@ -23,7 +23,6 @@ import com.google.common.collect.Sets; import java.awt.image.BufferedImage; -import java.awt.image.Raster; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; @@ -47,7 +46,7 @@ * coloured areas - take screenshot - calculate expected colors as in tested HTML page - scan * screenshot for actual colors * compare * - * @see org.openqa.selenium.TakesScreenshotTest + * (See related screenshot tests in the parent package.) */ // TODO(user): verify expected behaviour after frame switching @@ -184,44 +183,6 @@ private Set generateExpectedColors( return colors; } - /** - * Get colors from image from each point at grid defined by stepX/stepY. - * - * @param image - image - * @param stepX - interval in pixels b/w point in X dimension - * @param stepY - interval in pixels b/w point in Y dimension - * @return set of colors in string hex presentation - */ - private Set scanActualColors(BufferedImage image, final int stepX, final int stepY) { - Set colors = new TreeSet<>(); - - try { - int height = image.getHeight(); - int width = image.getWidth(); - assertThat(width > 0).isTrue(); - assertThat(height > 0).isTrue(); - - Raster raster = image.getRaster(); - for (int i = 0; i < width; i = i + stepX) { - for (int j = 0; j < height; j = j + stepY) { - String hex = - String.format( - "#%02x%02x%02x", - (raster.getSample(i, j, 0)), - (raster.getSample(i, j, 1)), - (raster.getSample(i, j, 2))); - colors.add(hex); - } - } - } catch (Exception e) { - fail("Unable to get actual colors from screenshot: " + e.getMessage()); - } - - assertThat(colors).isNotEmpty(); - - return colors; - } - /** * Compares sets of colors are same. * diff --git a/java/test/org/openqa/selenium/testing/JupiterTestBase.java b/java/test/org/openqa/selenium/testing/JupiterTestBase.java index a540bafb7c849..7b9f8cfe7cff5 100644 --- a/java/test/org/openqa/selenium/testing/JupiterTestBase.java +++ b/java/test/org/openqa/selenium/testing/JupiterTestBase.java @@ -18,11 +18,17 @@ package org.openqa.selenium.testing; import static org.assertj.core.api.Assumptions.assumeThat; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.fail; +import java.awt.image.BufferedImage; +import java.awt.image.Raster; import java.net.MalformedURLException; import java.net.URL; import java.time.Duration; import java.util.Optional; +import java.util.Set; +import java.util.TreeSet; import java.util.logging.Logger; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; @@ -116,4 +122,42 @@ public String toLocalUrl(String url) { protected WebDriverWait wait(WebDriver driver) { return new WebDriverWait(driver, Duration.ofSeconds(10)); } + + /** + * Get colors from image from each point at grid defined by stepX/stepY. + * + * @param image - image + * @param stepX - interval in pixels b/w point in X dimension + * @param stepY - interval in pixels b/w point in Y dimension + * @return set of colors in string hex presentation + */ + protected final Set scanActualColors(BufferedImage image, final int stepX, final int stepY) { + Set colors = new TreeSet<>(); + + try { + int height = image.getHeight(); + int width = image.getWidth(); + assertThat(width > 0).isTrue(); + assertThat(height > 0).isTrue(); + + Raster raster = image.getRaster(); + for (int i = 0; i < width; i = i + stepX) { + for (int j = 0; j < height; j = j + stepY) { + String hex = + String.format( + "#%02x%02x%02x", + (raster.getSample(i, j, 0)), + (raster.getSample(i, j, 1)), + (raster.getSample(i, j, 2))); + colors.add(hex); + } + } + } catch (Exception e) { + fail("Unable to get actual colors from screenshot: " + e.getMessage()); + } + + assertThat(colors).isNotEmpty(); + + return colors; + } } From b521ca8869a81f8493bfe4019eea84425ceb2dfb Mon Sep 17 00:00:00 2001 From: Manuel Blanco Date: Thu, 6 Nov 2025 12:07:10 +0100 Subject: [PATCH 2/2] new commit to format.sh the code. --- java/test/org/openqa/selenium/TakesScreenshotTest.java | 1 - .../openqa/selenium/firefox/TakesFullPageScreenshotTest.java | 2 +- java/test/org/openqa/selenium/testing/JupiterTestBase.java | 5 +++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/java/test/org/openqa/selenium/TakesScreenshotTest.java b/java/test/org/openqa/selenium/TakesScreenshotTest.java index 8f799415972e6..abd694134bc74 100644 --- a/java/test/org/openqa/selenium/TakesScreenshotTest.java +++ b/java/test/org/openqa/selenium/TakesScreenshotTest.java @@ -326,7 +326,6 @@ private Set generateExpectedColors( return colors; } - /** * Compares sets of colors are same. * diff --git a/java/test/org/openqa/selenium/firefox/TakesFullPageScreenshotTest.java b/java/test/org/openqa/selenium/firefox/TakesFullPageScreenshotTest.java index ce487b4858192..bb9c05e5edba1 100644 --- a/java/test/org/openqa/selenium/firefox/TakesFullPageScreenshotTest.java +++ b/java/test/org/openqa/selenium/firefox/TakesFullPageScreenshotTest.java @@ -46,7 +46,7 @@ * coloured areas - take screenshot - calculate expected colors as in tested HTML page - scan * screenshot for actual colors * compare * - * (See related screenshot tests in the parent package.) + *

(See related screenshot tests in the parent package.) */ // TODO(user): verify expected behaviour after frame switching diff --git a/java/test/org/openqa/selenium/testing/JupiterTestBase.java b/java/test/org/openqa/selenium/testing/JupiterTestBase.java index 7b9f8cfe7cff5..d3f74095c273c 100644 --- a/java/test/org/openqa/selenium/testing/JupiterTestBase.java +++ b/java/test/org/openqa/selenium/testing/JupiterTestBase.java @@ -17,8 +17,8 @@ package org.openqa.selenium.testing; -import static org.assertj.core.api.Assumptions.assumeThat; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assumptions.assumeThat; import static org.junit.jupiter.api.Assertions.fail; import java.awt.image.BufferedImage; @@ -131,7 +131,8 @@ protected WebDriverWait wait(WebDriver driver) { * @param stepY - interval in pixels b/w point in Y dimension * @return set of colors in string hex presentation */ - protected final Set scanActualColors(BufferedImage image, final int stepX, final int stepY) { + protected final Set scanActualColors( + BufferedImage image, final int stepX, final int stepY) { Set colors = new TreeSet<>(); try {