private String getScreenshot ( ) { WebElement webElement = getWebElement ( ) ; String imageLink = "<b><font class='fail'>No Image Preview</font></b>" ; // capture an image of it try { imageLink = reporter . captureEntirePageScreenshot ( ) ; File image = new File ( reporter . getDirectory ( ) , imageLink . split ( "\"" ) [ 1 ] ) ; BufferedImage fullImg = ImageIO . read ( image ) ; // Get the location of element on the page org . openqa . selenium . Point point = webElement . getLocation ( ) ; // Get width and height of the element int eleWidth = webElement . getSize ( ) . getWidth ( ) ; int eleHeight = webElement . 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" , image ) ; } catch ( WebDriverException | RasterFormatException | IOException e ) { log . error ( e ) ; } return imageLink ; }
Captures an image of the element and returns the html friendly link of it for use in the logging file . If there is a problem capturing the image an error message is returned instead .