diff --git a/src/main/java/com/xceptance/neodymium/common/ScreenshotWriter.java b/src/main/java/com/xceptance/neodymium/common/ScreenshotWriter.java index 72bb74bd..48c9c5b8 100644 --- a/src/main/java/com/xceptance/neodymium/common/ScreenshotWriter.java +++ b/src/main/java/com/xceptance/neodymium/common/ScreenshotWriter.java @@ -18,6 +18,7 @@ import org.openqa.selenium.Dimension; import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.Point; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; @@ -138,13 +139,22 @@ public static boolean doScreenshot(String filename, String pathname) throws IOEx image = ImageProcessor.blurExceptArea(image, coords); image = highlightScreenShot(image, coords, Color.decode(Neodymium.configuration().fullScreenHighlightColor())); } - if (Neodymium.configuration().enableHighlightLastElement() && Neodymium.getLastUsedElement() != null) + if (Neodymium.configuration().enableHighlightLastElement() && Neodymium.hasLastUsedElement()) { - double devicePixelRatio = Double.parseDouble("" + ((JavascriptExecutor) driver).executeScript("return window.devicePixelRatio")); - image = highlightScreenShot(image, new Coordinates(Neodymium.getLastUsedElement(), devicePixelRatio), - Color.decode(Neodymium.configuration().screenshotElementHighlightColor())); + try + { + double devicePixelRatio = Double.parseDouble("" + ((JavascriptExecutor) driver).executeScript("return window.devicePixelRatio")); + image = highlightScreenShot(image, new Coordinates(Neodymium.getLastUsedElement(), devicePixelRatio), + Color.decode(Neodymium.configuration().screenshotElementHighlightColor())); + } + catch (NoSuchElementException e) + { + // If the test is breaking because we can't find an element, we also can't highlight this element... so + // a NoSuchElementException is expected and can be ignored. + } } log.info("captured Screenshot to: " + imagePath); + boolean result = ImageIO.write(image, "png", outputfile); if (result) { diff --git a/src/main/java/com/xceptance/neodymium/util/Neodymium.java b/src/main/java/com/xceptance/neodymium/util/Neodymium.java index eaadcebd..43a7c698 100644 --- a/src/main/java/com/xceptance/neodymium/util/Neodymium.java +++ b/src/main/java/com/xceptance/neodymium/util/Neodymium.java @@ -583,4 +583,26 @@ else if (getContext().lastLocator != null) return null; } } + + /** + * Checks if the test already looked up any element. + * + * return whether there is a last element stored + * + */ + public static boolean hasLastUsedElement() + { + if (getContext().lastUsedElement != null && getContext().lastLocator != null) + { + return true; + } + else if (getContext().lastLocator != null) + { + return true; + } + else + { + return false; + } + } }