Skip to content

Commit

Permalink
[#244] quick fix for missing elements with highlight on
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernd Weigel committed Dec 2, 2024
1 parent ec7a832 commit 45b9820
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/main/java/com/xceptance/neodymium/common/ScreenshotWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/xceptance/neodymium/util/Neodymium.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

0 comments on commit 45b9820

Please sign in to comment.