Skip to content

Instantly share code, notes, and snippets.

@angelovstanton
Created August 20, 2020 09:39
Show Gist options
  • Save angelovstanton/724f1c0f71ad6432da6da00f22e8d952 to your computer and use it in GitHub Desktop.
Save angelovstanton/724f1c0f71ad6432da6da00f22e8d952 to your computer and use it in GitHub Desktop.
public void takeScreenshotOfElement(WebElement element, String fileName) throws Exception {
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshotFile);
Point point = element.getLocation();
int elementWidth = element.getSize().getWidth();
int elementHeight = element.getSize().getHeight();
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), elementWidth, elementHeight);
ImageIO.write(eleScreenshot, "png", screenshotFile);
String tempDir = getProperty("java.io.tmpdir");
File destFile = new File(Paths.get(tempDir, fileName + ".png").toString());
FileUtils.getFileUtils().copyFile(screenshotFile, destFile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment