public void collect(String filename) throws IOException { File screenShotFile = createScreenShotFile(collectImageDir, filename); File collected = new File(collectOutputImageDir, filename); FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(screenShotFile); out = new FileOutputStream(collected); IOUtils.copy(in, out); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } LOG.html("Screen captured: <br /> <img src='collect/%s'/>", filename); }
private void saveCapturedImage(String options, byte[] bytes, File file) throws IOException { if (options == null) { FileOutputStream out = null; try { LOG.html("Screen captured (%d): <br /> <img src='%s'/>", screenCaptureCtr, file.getName()); out = new FileOutputStream(file); IOUtils.write(bytes, out); } finally { IOUtils.closeQuietly(out); } } else { LOG.html("Screen captured (%d): <br /> <img src='%s'/>", screenCaptureCtr, file.getName()); BufferedImage fullImg = ImageIO.read(new ByteArrayInputStream(bytes)); ImageIO.write(processOption(fullImg, options), "png", file); } }
public void listCollected() throws IOException { FilenameFilter ff = new SuffixFileFilter("png"); File[] files = collectOutputImageDir.listFiles(ff); FileOutputStream fos = new FileOutputStream(new File(collectOutputImageDir, "all.zip")); BufferedOutputStream bos = new BufferedOutputStream(fos); ZipOutputStream zos = new ZipOutputStream(bos); try { for (File file : files) { // not available on BufferedOutputStream zos.putNextEntry(new ZipEntry(file.getName())); zos.write(IOUtils.toByteArray(new FileInputStream(file))); zos.closeEntry(); } } finally { zos.close(); } StringBuilder buf = new StringBuilder(); if(files != null && files.length > 0) { buf.append("<ul>"); for (File file : files) { buf.append(String.format("<li><a href='collect/%s'>%s</a>", file.getName(), file.getName())); } buf.append("<li><a href='collect/all.zip'>Download All files</a>"); buf.append("</ul>"); } else { buf.append("<b>No collected screen shots.</b>"); } LOG.html(buf.toString()); }
IOUtils.copy(new FileInputStream(baselineFile), new FileOutputStream(new File(baseOutputImageDir, filename))); LOG.html("Comparison: <br/> <table><tr><td><b>Baseline:</b></td><td><b>Screenshot:</b></td></tr><tr><td><img src='base/%s'/></td><td><img src='screenshot/%s'/></td></tr><tr><td colspan='2'><b>Difference:</b></td></tr><tr><td colspan='2'><img src='result/%s'/></td></tr></table>", filename, filename, filename); throw new IllegalStateException("Image Compare did not pass.");
public File elementCaptureScreenShot(String locator, String options) throws IOException { LOG.keywordAppender().appendLocator(locator); WebElement el = finder.find(locator); byte[] bytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES); File file = newScreenCaptureFile(); LOG.keywordAppender().appendArgument("File", file.getAbsolutePath()); LOG.html("Screen captured (%d): <br /> <img src='%s'/>", screenCaptureCtr, file.getName()); BufferedImage fullImg = ImageIO.read(new ByteArrayInputStream(bytes)); //Get the location of element on the page Point point = el.getLocation(); //Get width and height of the element int eleWidth = el.getSize().getWidth(); int eleHeight = el.getSize().getHeight(); LOG.keywordAppender() .appendArgument("Width", eleWidth) .appendArgument("Height", eleHeight) .appendArgument("X", point.getX()) .appendArgument("Y", point.getY()); //Crop the entire page screenshot to get only element screenshot BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight); if (options != null) { eleScreenshot = processOption(eleScreenshot, options); } ImageIO.write(eleScreenshot, "png", file); return file; }