Using Selenium Webdriver to take screenshot for specific UI Element as suggested by other post, I am using getSubimage method to capture the screenelement. But, receiving failure exception.
I am unsure about the difference in uielement's getLocation().getX() and getSize().getWidth().getX(). If someone can clarify this, as inside the WritableRaster method's condition it always checks for y+height
File imgSrc=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
Point point = uiElem.getLocation();
Point bottomright = new Point(uiElem.getSize().getWidth(),
uiElem.getSize().getHeight());
int xcord = point.getX();
int ycord = point.getY();
BufferedImage img;
try {
img = ImageIO.read(imgSrc);
BufferedImage dest = img.getSubimage(xcord, ycord, bottomright.getX(), bottomright.getY());
ImageIO.write(dest, "png", imgSrc);
FileUtils.copyFile(imgSrc, new File(".\\Screenshots\\123.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
While debugging, I noticed that the img (width 1366 and height = 613). and getSubImage() has (194,1335,960,15). It will always fail for condition (y+ height) >(this.minY + this.height ) inside createWritableChildMethod. So,can anyone point,where it's going wrong, also it doesn't make sense , as why we adding (y+height)of sub-image is greater?
Found a workaround, just notices it's better to use getScreenshotAs() directly on UIElement without doing alot of manipulation on the element.
public void takeSmallScreenshot(WebDriver driver, WebElement uiElem)
{
File uiElementSrc = uiElem.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(uiElementSrc, new File(".\\Screenshots\\"+uiElem.getText().substring(0,5)+".png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}