Thursday, February 13, 2014

Selenium : TakeScreenshot of only Particular element

TakeScreenshot of only Particular element


CODE HERE :
 
/* sPath - Path where the screenshot needs to be saved
oElement - Element object contains the element which is under consideration    */

public static void element_screenshot(WebDriver driver,WebElement oElement,String sPath){
        try{
            File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            BufferedImage  fullImg = ImageIO.read(screenshot);
                    //Get the location of element on the page
            Point point = oElement.getLocation();
                    //Get width and height of the element
            int eleWidth = oElement.getSize().getWidth();
            int eleHeight = oElement.getSize().getHeight();
                    //Crop the entire page screenshot to get only element screenshot
            BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
            ImageIO.write(eleScreenshot, "png", screenshot);
                    //Copy the element screenshot to disk
            FileUtils.copyFile(screenshot, new File(sPath));
        }catch(Throwable t)
        {
            System.out.println("Webelement not found :"+t.getMessage());
        }            
    }

No comments:

Post a Comment