protected File takeScreenshotInMemory(TakesScreenshot driver) { try { return driver.getScreenshotAs(FILE); } catch (Exception e) { log.log(SEVERE, "Failed to take screenshot in memory", e); return null; } }
private void takeScreenshot(String screenshotName) { WebDriver driver = this.webDriverSupplier.get(); if (driver instanceof TakesScreenshot) { File tempFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); try { File screenshot = new File("build/screenshots/" + screenshotName + ".png"); FileUtils.copyFile(tempFile, screenshot); System.err.println("Screenshot saved to: " + screenshot.getCanonicalPath()); } catch (IOException e) { throw new Error(e); } } }
protected File takeScreenshotImage(Config config, TakesScreenshot driver, String fileName) { try { File scrFile = driver.getScreenshotAs(FILE); File imageFile = new File(config.reportsFolder(), fileName + ".png"); try { copyFile(scrFile, imageFile); } catch (IOException e) { log.log(SEVERE, "Failed to save screenshot to " + imageFile, e); } return imageFile; } catch (WebDriverException e) { log.log(SEVERE, "Failed to take screenshot to " + fileName + " because of " + e); return null; } }
public static void takeScreenShot(String prefix, WebDriver webDriver) { File scrFile = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE); try { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd-HHmmss.SSS"); String now = format.format(new Date(System.currentTimeMillis())); FileUtils.copyFile(scrFile, new File("build/reports/", prefix + now + ".png")); } catch (IOException e) { e.printStackTrace(); } }
public static File takeScreenshot(WebDriver driver) throws IOException { File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); if (GalenConfig.getConfig().shouldAutoresizeScreenshots()) { BufferedImage image = Rainbow4J.loadImage(file.getAbsolutePath()); File newFile = File.createTempFile("screenshot", ".png"); image = GalenUtils.resizeScreenshotIfNeeded(driver, image); Rainbow4J.saveImage(image, newFile); return newFile; } else return file; }
public static File makeFullScreenshot(WebDriver driver) throws IOException, InterruptedException { byte[] bytes = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES); BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes)); int capturedWidth = image.getWidth(); scroll += scrollOffset; scrollVerticallyTo(driver, scroll); BufferedImage nextImage = ImageIO.read(new ByteArrayInputStream(((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES))); g2dTile.drawImage(nextImage, 0, (i+1) * capturedHeight, null); scroll += scrollOffset; scrollVerticallyTo(driver, scroll); BufferedImage nextImage = ImageIO.read(new ByteArrayInputStream(((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES))); BufferedImage lastPart = nextImage.getSubimage(0, nextImage.getHeight() - (int)(((double)leftover) * devicePixelRatio), nextImage.getWidth(), leftover); g2dTile.drawImage(lastPart, 0, times * capturedHeight, null);
private BufferedImage takeScreenshotAsImage(WebDriver webdriver, WebElement element) { if (!(webdriver instanceof TakesScreenshot)) { log.warning("Cannot take screenshot because browser does not support screenshots"); return null; } byte[] screen = ((TakesScreenshot) webdriver).getScreenshotAs(OutputType.BYTES); Point elementLocation = element.getLocation(); try { BufferedImage img = ImageIO.read(new ByteArrayInputStream(screen)); int elementWidth = element.getSize().getWidth(); int elementHeight = element.getSize().getHeight(); if (elementWidth > img.getWidth()) { elementWidth = img.getWidth() - elementLocation.getX(); } if (elementHeight > img.getHeight()) { elementHeight = img.getHeight() - elementLocation.getY(); } return img.getSubimage(elementLocation.getX(), elementLocation.getY(), elementWidth, elementHeight); } catch (IOException e) { log.log(SEVERE, "Failed to take screenshot of " + element, e); return null; } catch (RasterFormatException e) { log.warning("Cannot take screenshot because element is not displayed on current screen position"); return null; } }
public void debugPage(String className, String description) { TakesScreenshot takesScreenshot = (TakesScreenshot) browser; File scrFile = takesScreenshot.getScreenshotAs(OutputType.FILE); File destFile = getDestinationFile(className, description); try { FileUtils.copyFile(scrFile, destFile); } catch (IOException ioe) { throw new RuntimeException(ioe); } File pageSourceFile = getDestinationFile(className, description + ".html"); String pageSource = browser.getPageSource(); try { FileUtils.write(pageSourceFile, pageSource); } catch (IOException e) { throw new RuntimeException(e); } }
return null; byte[] screen = ((TakesScreenshot) webdriver).getScreenshotAs(OutputType.BYTES); Point iframeLocation = iframe.getLocation(); BufferedImage img;
public class ScreenshotListener extends RunListener { private TakesScreenshot screenshotTaker; @Override public void testFailure(Failure failure) throws Exception { File file = screenshotTaker.getScreenshotAs(OutputType.File); // do something with your file } }
@Override public <X> X getScreenshotAs(final OutputType<X> target) throws WebDriverException { return ((TakesScreenshot) webDriver).getScreenshotAs(target); }
@Override public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException { return ((TakesScreenshot) webDriver).getScreenshotAs(target); }
if (driver instanceof TakesScreenshot) { TakesScreenshot ts = (TakesScreenshot) driver; File screenshotFile = ts.getScreenshotAs(OutputType.FILE); //feel free to move or rename the file as you see fit. }else{ /* unsupported*/ }
/** * Makes screenshot of visible part of the page * * @param augmentedDriver * - webDriver. * @exception IOException * * @return screenshot image */ private static BufferedImage takeVisibleScreenshot(WebDriver augmentedDriver) throws Exception { return ImageIO.read(((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE)); }
public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException { if (driver instanceof TakesScreenshot) { return ((TakesScreenshot) driver).getScreenshotAs(target); } throw new UnsupportedOperationException( "Underlying driver instance does not support taking screenshots"); }
@Override public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException { if (driver instanceof TakesScreenshot) { return ((TakesScreenshot) driver).getScreenshotAs(target); } throw new UnsupportedOperationException( "Underlying driver instance does not support taking screenshots"); }
public Object takeScreenshot(OutputType outputType) { try { return ((TakesScreenshot) webDriver).getScreenshotAs(outputType); } catch (Exception e) { LOGGER.error(e.getMessage()); throw e; } }
private static void snapshotBrowser(TakesScreenshot driver, String screenSnapshotName, File browserFile) { try { File scrFile = driver.getScreenshotAs(OutputType.FILE); log.info("PNG browser snapshot file name: \"{}\"", browserFile.toURI().toString()); FileUtils.deleteQuietly(browserFile); FileUtils.moveFile(scrFile, browserFile); } catch (Exception e) { log.error("Could not create browser snapshot: " + screenSnapshotName, e); } }
void logBase64Screenshot(WebDriver driver, String fileName) { try { String screenshotBase64 = ((TakesScreenshot) driver) .getScreenshotAs(BASE64); log.info("Screenshot (in Base64) at the end of {} " + "(copy&paste this string as URL in browser to watch it):\r\n" + "data:image/png;base64,{}", fileName, screenshotBase64); } catch (Exception e) { log.trace("Exception getting screenshot in Base64", e); } }
private BufferedImage takeBufferedImage() { byte[] screenshotBytes = screenshotTaker.getScreenshotAs(OutputType.BYTES); try { return ImageIO.read(new ByteArrayInputStream(screenshotBytes)); } catch (IOException e) { throw new RuntimeException(e); } }