Search in sources :

Example 1 with TakesScreenshot

use of org.openqa.selenium.TakesScreenshot in project geode by apache.

the class ScreenshotOnFailureRule method takeScreenshot.

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);
        }
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) IOException(java.io.IOException) File(java.io.File) TakesScreenshot(org.openqa.selenium.TakesScreenshot)

Example 2 with TakesScreenshot

use of org.openqa.selenium.TakesScreenshot in project ats-framework by Axway.

the class UiEngineUtilities method createScreenshot.

/**
     * Create a screenshot image (supported image format/type is PNG).<br/>
     * If the screenshot image file already exists it will be automatically replaced by the new one.<br/>
     * <br/>
     * Currently the supported UI drivers are {@link AbstractRealBrowserDriver} and {@link MobileDriver}:
     * <ul>
     *     <li>{@link AbstractRealBrowserDriver} - the method makes a best effort to create a screenshot,
     *     depending on the browser to return the following in order of preference:
     *          <ul>
     *              <li>Entire page</li>
     *              <li>Current window</li>
     *              <li>Visible portion of the current frame</li>
     *              <li>The screenshot of the entire display containing the browser</li>
     *          </ul>
     *     </li>
     *     <li>{@link MobileDriver} - creates a screenshot of the mobile device screen.<br/>
     *          <b>NOTE:</b> There is a <a href="https://github.com/selendroid/selendroid/issues/325">known issue</a> on Android Virtual Device with <b>"Use Host GPU"</b> enabled option.
     *          So in order to get a screenshot it should be disabled. Keep in mind that it will affect the performance, because
     *          it is a performance acceleration option.
     *     </li>
     * </ul>
     *
     * @param filePath the screenshot image file path
     * @param uiDriver {@link UiDriver} instance
     */
@PublicAtsApi
public static void createScreenshot(String filePath, UiDriver uiDriver) {
    WebDriver webDriver = null;
    if (uiDriver instanceof AbstractRealBrowserDriver) {
        AbstractRealBrowserDriver browserDriver = (AbstractRealBrowserDriver) uiDriver;
        webDriver = (WebDriver) browserDriver.getInternalObject(InternalObjectsEnum.WebDriver.toString());
    } else if (uiDriver instanceof MobileDriver) {
        MobileDriver mobileDriver = (MobileDriver) uiDriver;
        webDriver = (WebDriver) mobileDriver.getInternalObject(InternalObjectsEnum.WebDriver.toString());
        ((AppiumDriver) webDriver).context(MobileDriver.NATIVE_CONTEXT);
    } else {
        throw new NotSupportedOperationException("Currently it is not possible to create a screenshot with driver: " + uiDriver.getClass().getSimpleName());
    }
    File scrTmpFile = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
    File scrFile = new File(filePath);
    if (scrFile.exists() && !scrFile.delete()) {
        log.warn("The Screenshot image file '" + filePath + "' already exists, but couldn't be deleted. You can find the current Screenshot image here: " + scrTmpFile.getAbsolutePath());
    } else if (!scrTmpFile.renameTo(scrFile)) {
        // if renameTo() fails we will try to copy the file
        try {
            new LocalFileSystemOperations().copyFile(scrTmpFile.getCanonicalPath(), scrFile.getCanonicalPath(), true);
            scrTmpFile.delete();
        } catch (Exception e) {
            log.warn("Unable to create Screenshot image file: " + filePath);
        }
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) AbstractRealBrowserDriver(com.axway.ats.uiengine.AbstractRealBrowserDriver) LocalFileSystemOperations(com.axway.ats.core.filesystem.LocalFileSystemOperations) MobileDriver(com.axway.ats.uiengine.MobileDriver) NotSupportedOperationException(com.axway.ats.uiengine.exceptions.NotSupportedOperationException) File(java.io.File) NotSupportedOperationException(com.axway.ats.uiengine.exceptions.NotSupportedOperationException) TakesScreenshot(org.openqa.selenium.TakesScreenshot) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 3 with TakesScreenshot

use of org.openqa.selenium.TakesScreenshot in project fess by codelibs.

the class WebDriverGenerator method generate.

@Override
public boolean generate(final String thumbnailId, final String url, final File outputFile) {
    if (logger.isDebugEnabled()) {
        logger.debug("Generate Thumbnail: " + url);
    }
    if (outputFile.exists()) {
        if (logger.isDebugEnabled()) {
            logger.debug("The thumbnail file exists: " + outputFile.getAbsolutePath());
        }
        return true;
    }
    final File parentFile = outputFile.getParentFile();
    if (!parentFile.exists()) {
        parentFile.mkdirs();
    }
    if (!parentFile.isDirectory()) {
        logger.warn("Not found: " + parentFile.getAbsolutePath());
        return false;
    }
    if (webDriver instanceof TakesScreenshot) {
        final FessConfig fessConfig = ComponentUtil.getFessConfig();
        synchronized (this) {
            try {
                webDriver.get(url);
                if (webDriver instanceof JavascriptExecutor) {
                    final Dimension dim = webDriver.findElement(By.tagName("body")).getSize();
                    if (dim.height >= fessConfig.getThumbnailHtmlPhantomjsMaxHeightAsInteger()) {
                        if (logger.isInfoEnabled()) {
                            logger.info("Skpped Thumbnail generation " + dim + " for " + url);
                        }
                        return false;
                    }
                }
                final File thumbnail = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
                convert(thumbnail, outputFile);
                updateThumbnailField(thumbnailId, url, url);
                return true;
            } catch (final UnreachableBrowserException | SessionNotFoundException e) {
                if (logger.isDebugEnabled()) {
                    logger.debug("WebDriver is not available.", e);
                }
                previousCheckTime = 0;
            } finally {
                final long now = ComponentUtil.getSystemHelper().getCurrentTimeAsLong();
                if (now - previousCheckTime > fessConfig.getThumbnailHtmlPhantomjsKeepAliveAsInteger().longValue()) {
                    destroy();
                    startWebDriver();
                }
            }
        }
    } else {
        logger.warn("WebDriver is not instance of TakesScreenshot: " + webDriver);
    }
    return false;
}
Also used : JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) UnreachableBrowserException(org.openqa.selenium.remote.UnreachableBrowserException) Dimension(org.openqa.selenium.Dimension) File(java.io.File) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) TakesScreenshot(org.openqa.selenium.TakesScreenshot) SessionNotFoundException(org.openqa.selenium.remote.SessionNotFoundException)

Aggregations

File (java.io.File)3 TakesScreenshot (org.openqa.selenium.TakesScreenshot)3 WebDriver (org.openqa.selenium.WebDriver)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 LocalFileSystemOperations (com.axway.ats.core.filesystem.LocalFileSystemOperations)1 AbstractRealBrowserDriver (com.axway.ats.uiengine.AbstractRealBrowserDriver)1 MobileDriver (com.axway.ats.uiengine.MobileDriver)1 NotSupportedOperationException (com.axway.ats.uiengine.exceptions.NotSupportedOperationException)1 IOException (java.io.IOException)1 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)1 Dimension (org.openqa.selenium.Dimension)1 JavascriptExecutor (org.openqa.selenium.JavascriptExecutor)1 SessionNotFoundException (org.openqa.selenium.remote.SessionNotFoundException)1 UnreachableBrowserException (org.openqa.selenium.remote.UnreachableBrowserException)1