Skip to content

Commit

Permalink
Use UiTestUtils for taking screenshots in LayoutCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
martingrossmann committed Sep 19, 2024
1 parent e0ee3d7 commit 4c35453
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
import eu.tsystems.mms.tic.testframework.report.utils.IExecutionContextController;
import eu.tsystems.mms.tic.testframework.testing.AssertProvider;
import eu.tsystems.mms.tic.testframework.utils.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import eu.tsystems.mms.tic.testframework.utils.UITestUtils;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -195,9 +194,10 @@ private static MatchStep prepare(
/**
* Matches image pixels and returns an absolute distance value
*/
public static MatchStep matchPixels(final TakesScreenshot takesScreenshot, final String targetImageName) {
final File screenshot = takesScreenshot.getScreenshotAs(OutputType.FILE);
return matchPixels(screenshot, targetImageName);
public static MatchStep matchPixels(WebDriver webDriver, final String targetImageName) {
Screenshot screenshot = new Screenshot();
UITestUtils.takeWebDriverScreenshotToFile(webDriver, screenshot.getScreenshotFile());
return matchPixels(screenshot.getScreenshotFile(), targetImageName);
}

public static MatchStep matchPixels(final File screenshot, final String targetImageName) {
Expand Down Expand Up @@ -483,7 +483,7 @@ public static boolean isMatchDimensions(final MatchStep step) {
public static void assertScreenshot(WebDriver webDriver, String targetImageName, double confidenceThreshold) {
final String assertMessage = String.format("pixel distance (%%) of WebDriver screenshot to image '%s'", targetImageName);

LayoutCheck.MatchStep matchStep = LayoutCheck.matchPixels((TakesScreenshot) webDriver, targetImageName);
LayoutCheck.MatchStep matchStep = LayoutCheck.matchPixels(webDriver, targetImageName);
assertWithLayoutCheck(matchStep, confidenceThreshold, assertMessage);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public class UITestUtils implements WebDriverManagerProvider {
private static final DateFormat FILES_DATE_FORMAT = new SimpleDateFormat("dd_MM_yyyy__HH_mm_ss");
private static final Report report = Testerra.getInjector().getInstance(Report.class);
private static final IExecutionContextController executionContextController = Testerra.getInjector().getInstance(IExecutionContextController.class);
private static final int IE_SCREENSHOT_LIMIT = 1200;

@Deprecated
private UITestUtils() {
Expand Down Expand Up @@ -208,21 +207,6 @@ private static Screenshot takeScreenshot(WebDriver decoratedWebDriver, String or
}

public static void takeWebDriverScreenshotToFile(WebDriver decoratedWebDriver, File screenShotTargetFile) {
/*
* The ChromeDriver doesn't support Screenshots of a large Site currently (Selenium 2.44), only the viewport ist
* captured. To allow full-page screenshots, we stitch several viewport-screenshots together.
* If this is eventually supported by WebDriver, this special branch can be removed.
*/
if (Browsers.ie.equalsIgnoreCase(WEB_DRIVER_MANAGER.getRequestedBrowser(decoratedWebDriver).orElse(null))) {
Rectangle viewport = new JSUtils().getViewport(decoratedWebDriver);

if (viewport.height > IE_SCREENSHOT_LIMIT) {
LOGGER.warn("IE: Not taking screenshot because screen size is larger than height limit of " + IE_SCREENSHOT_LIMIT);
return;
}
}

// take screenshot
makeSimpleScreenshot(decoratedWebDriver, screenShotTargetFile);
}

Expand Down

0 comments on commit 4c35453

Please sign in to comment.