public File takeScreenshot(WebElement element) { if (!WebDriverRunner.hasWebDriverStarted()) { log.warning("Cannot take screenshot because browser is not started"); return null; } WebDriver webdriver = getWebDriver(); if (!(webdriver instanceof TakesScreenshot)) { log.warning("Cannot take screenshot because browser does not support screenshots"); return null; } File screen = ((TakesScreenshot) webdriver).getScreenshotAs(OutputType.FILE); Point p = element.getLocation(); Dimension elementSize = element.getSize(); try { BufferedImage img = ImageIO.read(screen); BufferedImage dest = img.getSubimage(p.getX(), p.getY(), elementSize.getWidth(), elementSize.getHeight()); ImageIO.write(dest, "png", screen); File screenshotOfElement = new File(generateScreenshotFileName()); FileUtils.copyFile(screen, screenshotOfElement); return screenshotOfElement; } catch (IOException e) { printOnce("takeScreenshotImage", e); return null; } }
/** * Takes screenshot of current browser window. Stores 2 files: html of page, and (if possible) * image in PNG format. * * @param fileName name of file (without extension) to store screenshot to. * @return the name of last saved file, it's either my_screenshot.png or my_screenshot.html (if * failed to create png) */ public String takeScreenShot(String fileName) { if (!WebDriverRunner.hasWebDriverStarted()) { log.warning("Cannot take screenshot because browser is not started"); return null; } WebDriver webdriver = getWebDriver(); File pageSource = savePageSourceToFile(fileName, webdriver); File imageFile = savePageImageToFile(fileName, webdriver); String screenshot = firstNonNull(imageFile, pageSource).getAbsolutePath(); return addToHistory(screenshot); }
public File getScreenShotAsFile() { if (!WebDriverRunner.hasWebDriverStarted()) { log.warning("Cannot take screenshot because browser is not started"); return null; } WebDriver webdriver = getWebDriver(); // File pageSource = savePageSourceToFile(fileName, webdriver); - temporary not available File scrFile = getPageImage(webdriver); String screenshot = scrFile.getAbsolutePath(); addToHistory(screenshot); return scrFile; }