src/main/java/com/github/webdriverextensions/Bot.java
/**
     * Takes a screenshot of the page and crops the
     * target webElement out of it as standalone image
     * @param fileName the filename of the screenshot file without the file extension
     */
Takes a screenshot of the page and crops the
target webElement out of it as standalone image
@param fileName the filename of the screenshot file without the file extension
Takes a screenshot of the page and crops the target webElement out of it as standalone image @param fileName the filename of the screenshot file without the file extension
public static void takeScreenshotOf(WebElement element, String fileName) {
        scrollToCenter(element);
        BufferedImage windowImage = null;
        byte[] bytes = ((TakesScreenshot) driver()).getScreenshotAs(OutputType.BYTES);
        try {
            windowImage = ImageIO.read(new ByteArrayInputStream(bytes));
        } catch (IOException ex) {
            throw new WebDriverExtensionException("Failed to read screenshot into memory for highlighting element", ex);
        }
        Rectangle elementRect = new Rectangle(element.getLocation(), element.getSize());
        int viewportOffset = (int) (long) executeJavascript("return window.pageYOffset");
        BufferedImage elementImage = windowImage.getSubimage(
                elementRect.x,
                elementRect.y - viewportOffset,
                elementRect.width,
                elementRect.height);
        String filePath = getScreenshotFilePath(fileName);
        File file = new File(filePath);
        file.mkdirs(); 
        try {
            ImageIO.write(elementImage, "png", file);
        } catch (IOException ex) {
            throw new WebDriverExtensionException("Failed to save screenshot of element to " + quote(filePath), ex);
        }
    }
public static void takeScreenshotOf ( WebElement element , String fileName ) { scrollToCenter ( element ) ; BufferedImage windowImage = null ; byte [ ] bytes = ( ( TakesScreenshot ) driver ( ) ) . getScreenshotAs ( OutputType . BYTES ) ; try { windowImage = ImageIO . read ( new ByteArrayInputStream ( bytes ) ) ; } catch ( IOException ex ) { throw new WebDriverExtensionException ( "Failed to read screenshot into memory for highlighting element" , ex ) ; } Rectangle elementRect = new Rectangle ( element . getLocation ( ) , element . getSize ( ) ) ; int viewportOffset = ( int ) ( long ) executeJavascript ( "return window.pageYOffset" ) ; BufferedImage elementImage = windowImage . getSubimage ( elementRect . x , elementRect . y - viewportOffset , elementRect . width , elementRect . height ) ; String filePath = getScreenshotFilePath ( fileName ) ; File file = new File ( filePath ) ; file . mkdirs ( ) ; try { ImageIO . write ( elementImage , "png" , file ) ; } catch ( IOException ex ) { throw new WebDriverExtensionException ( "Failed to save screenshot of element to " + quote ( filePath ) , ex ) ; } }
Takes a screenshot of the page and crops the
 target webElement out of it as standalone image
 @param fileName the filename of the screenshot file without the file extension
Takes a screenshot of the page and crops the target webElement out of it as standalone image @param fileName the filename of the screenshot file without the file extension
// bytes should be well formed at this point // make target direcory tree if doesn't exists