Created
August 20, 2020 09:39
-
-
Save angelovstanton/724f1c0f71ad6432da6da00f22e8d952 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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