src/main/java/com/github/webdriverextensions/Bot.java
/**
     * This function will try to center target webElement in the viewport.
     * @param borderWidth determines how thicks the surrounding rectangle will be
     * @param highlightColor determines color of surrounding rectangle
     * @param borderOffset determines how much bigger/smaller the surrounding rectangle will be
     * @param fileName the filename of the screenshot file without the file extension
     */
This function will try to center target webElement in the viewport.
@param borderWidth determines how thicks the surrounding rectangle will be
@param highlightColor determines color of surrounding rectangle
@param borderOffset determines how much bigger/smaller the surrounding rectangle will be
@param fileName the filename of the screenshot file without the file extension
This function will try to center target webElement in the viewport . @param borderWidth determines how thicks the surrounding rectangle will be @param highlightColor determines color of surrounding rectangle @param borderOffset determines how much bigger / smaller the surrounding rectangle will be @param fileName the filename of the screenshot file without the file extension
public static void takeScreenshotWithHighlight(WebElement element, int borderWidth, Color highlightColor, int borderOffset, String fileName) {
        scrollToCenter(element);
        BufferedImage windowImage;
        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 extracting element image. ", ex);
        }
        Rectangle elementRect = new Rectangle(element.getLocation(), element.getSize());
        int viewportOffset = (int) (long) executeJavascript("return window.pageYOffset");
        Graphics2D graph = windowImage.createGraphics();
        graph.setStroke(new BasicStroke(borderWidth));
        graph.setColor(highlightColor);
        graph.drawRect(
                elementRect.x - borderOffset,
                elementRect.y - viewportOffset - borderOffset,
                elementRect.width + (2 * borderOffset),
                elementRect.height + (2 * borderOffset));
        graph.dispose();
        String filePath = getScreenshotFilePath(fileName);
        File file = new File(filePath);
        file.mkdirs(); 
        try {
            ImageIO.write(windowImage, "png", file);
        } catch (IOException ex) {
            throw new WebDriverExtensionException("Failed to save screenshot with hightlighted element to " + quote(filePath), ex);
        }
    }
public static void takeScreenshotWithHighlight ( WebElement element , int borderWidth , Color highlightColor , int borderOffset , String fileName ) { scrollToCenter ( element ) ; BufferedImage windowImage ; 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 extracting element image. " , ex ) ; } Rectangle elementRect = new Rectangle ( element . getLocation ( ) , element . getSize ( ) ) ; int viewportOffset = ( int ) ( long ) executeJavascript ( "return window.pageYOffset" ) ; Graphics2D graph = windowImage . createGraphics ( ) ; graph . setStroke ( new BasicStroke ( borderWidth ) ) ; graph . setColor ( highlightColor ) ; graph . drawRect ( elementRect . x - borderOffset , elementRect . y - viewportOffset - borderOffset , elementRect . width + ( 2 * borderOffset ) , elementRect . height + ( 2 * borderOffset ) ) ; graph . dispose ( ) ; String filePath = getScreenshotFilePath ( fileName ) ; File file = new File ( filePath ) ; file . mkdirs ( ) ; try { ImageIO . write ( windowImage , "png" , file ) ; } catch ( IOException ex ) { throw new WebDriverExtensionException ( "Failed to save screenshot with hightlighted element to " + quote ( filePath ) , ex ) ; } }
This function will try to center target webElement in the viewport.
This function will try to center target webElement in the viewport .
// should must be well formed at this point // make target direcory tree if doesn't exists