Example usage for org.openqa.selenium.remote CapabilityType TAKES_SCREENSHOT

List of usage examples for org.openqa.selenium.remote CapabilityType TAKES_SCREENSHOT

Introduction

In this page you can find the example usage for org.openqa.selenium.remote CapabilityType TAKES_SCREENSHOT.

Prototype

String TAKES_SCREENSHOT

To view the source code for org.openqa.selenium.remote CapabilityType TAKES_SCREENSHOT.

Click Source Link

Usage

From source file:Util.java

License:Apache License

/**
 * // Takes a screen shot if the driver's capabilities include Screen Shots
 * @param driver        The Web Driver instance
 * @param outputType    Either FILE or Base64
 * @param folder        The folder for images
 * @param filename      The base file name (sans time stamp)
 * @return              Error string starting with ERROR: if failed to create file or the file name that was created
 *//*from   w w w.  j  a  v  a2s .c om*/
public static String takeScreenImage(WebDriver driver, String folder, String fileName) {
    String result = "";
    WebDriver theDriver = Driver.getDriver();

    if (!(theDriver instanceof WebDriver)) {
        return "ERROR: No Web Driver found at this step!";
    }

    if (!((HasCapabilities) theDriver).getCapabilities().is(CapabilityType.TAKES_SCREENSHOT)) {
        return "ERROR: Driver " + Util.sq(theDriver.toString())
                + " has no TAKES_SCREENSHOT capabilities.  Screen shot not taken";
    }

    String imagesFolder = (isBlank(folder)) ? DDTTestRunner.getReporter().sessionImagesFolderName() : folder;

    try {
        File testTempDir = new File(DDTSettings.asValidOSPath(imagesFolder, true));
        if (testTempDir.exists()) {
            if (!testTempDir.isDirectory()) {
                return "ERROR: Image path exists but is not a directory";
            }
        } else {
            testTempDir.mkdirs();
        }
    } catch (Exception e) {
        return "ERROR: File operation (mkdir) failed. " + e.getCause();
    }

    String actualFileName = fileName + " - " + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date())
            + ".png";

    // create screenshot using a casted driver
    TakesScreenshot snapper = (TakesScreenshot) theDriver;

    if (DDTSettings.Settings().isLocal()) {
        File tempImageFile = snapper.getScreenshotAs(OutputType.FILE);
        if (tempImageFile.length() < (1L)) {
            return "ERROR: Failed to take screen shot on remote driver";
        }

        File tmpFile = new File(imagesFolder, DDTSettings.asValidOSPath(actualFileName, true)); // testTempImage
        // move screenshot to our local store
        try {
            FileUtils.moveFile(tempImageFile, tmpFile);
        } catch (Exception e) {
            return "ERROR: Failed to move tmp file to image file. " + Util.sq(tmpFile.getAbsolutePath()) + " "
                    + e.getCause().toString();
        }

        if (tmpFile.length() < 1L) {
            return "ERROR: Failed to move tmp file to image file. " + Util.sq(tmpFile.getAbsolutePath());
        }

        result = tmpFile.getAbsolutePath();
    } else {
        // Create Base64 screen shot file on the remote driver and store it locally
        String tempImageFile = snapper.getScreenshotAs(OutputType.BASE64);
        if (tempImageFile.length() < (1L)) {
            return "ERROR: Failed to take screen shot on remote driver";
        }

        Base64 decoder = new Base64();
        byte[] imgBytes = (byte[]) decoder.decode(tempImageFile);

        File tmpFile = new File(imagesFolder, DDTSettings.asValidOSPath(actualFileName, true));

        FileOutputStream osf = null;
        try {
            osf = new FileOutputStream(tmpFile);
            osf.write(imgBytes);
            osf.flush();
        } catch (Exception e) {
            return "ERROR: Failed to create File Output Stream " + e.getCause();
        }

        if (tmpFile.length() < 1L) {
            return "ERROR: File created from  File Output Stream is empty!";
        }

        result = tmpFile.getAbsolutePath();
    }

    return result;
}

From source file:ca.nrc.cadc.web.selenium.AbstractWebApplicationIntegrationTest.java

License:Open Source License

/**
 * Override to set up your specific external resource.
 *//*from w  w w .  j a  va 2 s  .c  o  m*/
@BeforeClass
public static void setUp() {
    try {
        final String seleniumURL = seleniumServerURL
                + (seleniumServerURL.contains(SELENIUM_SERVER_URL_ENDPOINT) ? ""
                        : SELENIUM_SERVER_URL_ENDPOINT);
        System.out.println("Connecting to " + seleniumURL);

        final String browserDriverName = System.getProperty("driver");
        driverCapabilities = CAPABILITIES_LOOKUP.get(browserDriverName.toLowerCase());
        driverCapabilities.setCapability(CapabilityType.HAS_NATIVE_EVENTS, true);
        driverCapabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);
        driverCapabilities.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
        driverCapabilities.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
        driverCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

        driver = new RemoteWebDriver(new URL(seleniumURL), driverCapabilities);

        username = System.getProperty("user.name");
        password = System.getProperty("user.password");
        webURL = System.getProperty("web.app.url");
    } catch (MalformedURLException e) {
        System.err.println("Can't create URL.");
        e.printStackTrace(System.err);
        throw new RuntimeException(e);
    }

    driver.manage().window().maximize();

    final WebDriver.Timeouts timeouts = driver.manage().timeouts();

    // Safari does not support setTimeout.
    if (!driverCapabilities.getBrowserName().contains("afari")) {
        // Set the timeout to four minutes.
        timeouts.pageLoadTimeout(TIMEOUT_IN_MILLISECONDS, TimeUnit.MILLISECONDS);
    }

    timeouts.setScriptTimeout(TIMEOUT_IN_MILLISECONDS, TimeUnit.MILLISECONDS);
}

From source file:com.ecofactor.qa.automation.platform.ops.impl.IOSOperations.java

License:Open Source License

/**
 * Creates the device driver./*from   w  ww.  j a v  a2  s  .  c  o  m*/
 * @return the web driver
 * @throws DeviceException the device exception
 * @see com.ecofactor.qa.automation.mobile.ops.impl.AbstractMobileOperations#createDeviceDriver()
 */
@Override
protected WebDriver createDeviceDriver() throws DeviceException {

    WebDriver driver = null;
    if (!hasErrors) {
        try {
            final File classpathRoot = new File(System.getProperty("user.dir"));
            final File app = new File(classpathRoot, "/EcoFactor_iOS/EcoFactorNew.zip");
            setLogString("Application Path: " + app.getPath(), true);
            final DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("device", "iphone");
            capabilities.setCapability(CapabilityType.BROWSER_NAME, JenkinsParamUtil.getDeviceName());
            capabilities.setCapability(CapabilityType.VERSION, "7.0");
            capabilities.setCapability(CapabilityType.PLATFORM, SystemUtil.getOSType());
            capabilities.setCapability("cleanSession", true);
            capabilities.setCapability("app", app.getAbsolutePath());
            capabilities.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
            driver = new SwipeableWebDriver(new URL(getAppiumHostUrl()), capabilities);
        } catch (Exception ex) {
            if (ex.getCause() instanceof HttpHostConnectException) {
                setErrorMsg("Appium not started");
                setLogString(errorMsg, true);
                hasErrors = true;
                throw new DeviceException("\033[41;1m" + getErrorMsg());
            }
            setLogString("\033[41;1mERROR in initialze (" + ex.getMessage() + ")", true);
            throw new DeviceException("ERROR in initialze (" + ex.getMessage() + ")");
        }
    }
    return driver;
}

From source file:com.ecofactor.qa.automation.platform.util.SeleniumDriverUtil.java

License:Open Source License

/**
 * Creates the ios driver.// ww w.  ja  v  a 2s.  c om
 * @return the web driver
 * @throws DeviceException the device exception
 */
public static WebDriver createIOSDriver() throws DeviceException {

    WebDriver driver = null;
    if (!hasErrors) {
        try {
            final File classpathRoot = new File(System.getProperty("user.dir"));
            final File app = new File(classpathRoot, ECOFACTOR_IPA);
            setLogString("Application Path: " + app.getPath(), true);
            final DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("device", "iphone");
            setHubCapabilities(capabilities);
            capabilities.setCapability(CapabilityType.BROWSER_NAME, getDeviceName());
            capabilities.setCapability("cleanSession", true);
            capabilities.setCapability("app", app.getAbsolutePath());
            capabilities.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
            driver = new SwipeableWebDriver(new URL(createHostUrl()), capabilities);
        } catch (final Exception ex) {
            checkIfAppiumError(ex);
        }
    }
    return driver;
}

From source file:com.elastica.browserfactory.AndroidCapabilitiesFactory.java

License:Apache License

public DesiredCapabilities createCapabilities(final DriverConfig cfg) {
    DesiredCapabilities capability = null;
    capability = DesiredCapabilities.android();

    if (cfg.isEnableJavascript()) {
        capability.setJavascriptEnabled(true);
    } else {//from   www.  j av a  2 s .  co m
        capability.setJavascriptEnabled(false);
    }

    capability.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
    capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

    if (cfg.getBrowserVersion() != null) {
        capability.setVersion(cfg.getBrowserVersion());
    }

    if (cfg.getPlatform() != null) {
        capability.setPlatform(cfg.getPlatform());
    }

    if (cfg.getProxyHost() != null) {
        Proxy proxy = cfg.getProxy();
        capability.setCapability(CapabilityType.PROXY, proxy);
    }

    return capability;
}

From source file:com.elastica.browserfactory.ChromeCapabilitiesFactory.java

License:Apache License

public DesiredCapabilities createCapabilities(final DriverConfig webDriverConfig) {

    DesiredCapabilities capability = null;
    capability = DesiredCapabilities.chrome();
    capability.setBrowserName(DesiredCapabilities.chrome().getBrowserName());

    ChromeOptions options = new ChromeOptions();
    if (webDriverConfig.getUserAgentOverride() != null) {
        options.addArguments("--user-agent=" + webDriverConfig.getUserAgentOverride());
    }//from w  w  w .j a  v a2 s  .  c  om

    capability.setCapability(ChromeOptions.CAPABILITY, options);

    if (webDriverConfig.isEnableJavascript()) {
        capability.setJavascriptEnabled(true);
    } else {
        capability.setJavascriptEnabled(false);
    }

    capability.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
    capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

    if (webDriverConfig.getBrowserVersion() != null) {
        capability.setVersion(webDriverConfig.getBrowserVersion());
    }

    if (webDriverConfig.getPlatform() != null) {
        capability.setPlatform(webDriverConfig.getPlatform());
    }

    if (webDriverConfig.getProxyHost() != null) {
        Proxy proxy = webDriverConfig.getProxy();
        capability.setCapability(CapabilityType.PROXY, proxy);
    }

    if (webDriverConfig.getChromeBinPath() != null) {
        capability.setCapability("chrome.binary", webDriverConfig.getChromeBinPath());
    }

    // Set ChromeDriver for local mode
    if (webDriverConfig.getMode() == DriverMode.LOCAL) {
        String chromeDriverPath = webDriverConfig.getChromeDriverPath();
        if (chromeDriverPath == null) {
            try {
                if (System.getenv("webdriver.chrome.driver") != null) {
                    System.out.println(
                            "get Chrome driver from property:" + System.getenv("webdriver.chrome.driver"));
                    System.setProperty("webdriver.chrome.driver", System.getenv("webdriver.chrome.driver"));
                } else {
                    handleExtractResources();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } else {
            System.setProperty("webdriver.chrome.driver", chromeDriverPath);
        }
    }

    return capability;
}

From source file:com.elastica.browserfactory.FirefoxCapabilitiesFactory.java

License:Apache License

/**
 * Create firefox capabilities.// ww w. ja  va  2s  .  com
 */
public DesiredCapabilities createCapabilities(final DriverConfig webDriverConfig) {
    DesiredCapabilities capability;
    capability = new DesiredCapabilities();
    capability.setBrowserName(DesiredCapabilities.firefox().getBrowserName());

    FirefoxProfile profile = getFirefoxProfile(webDriverConfig);
    configProfile(profile, webDriverConfig);
    capability.setCapability(FirefoxDriver.PROFILE, profile);

    if (webDriverConfig.isEnableJavascript()) {
        capability.setJavascriptEnabled(true);
    } else {
        capability.setJavascriptEnabled(false);
    }

    capability.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
    capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

    if (webDriverConfig.getBrowserVersion() != null) {
        capability.setVersion(webDriverConfig.getBrowserVersion());
    }

    if (webDriverConfig.getPlatform() != null) {
        capability.setPlatform(webDriverConfig.getPlatform());
    }

    if (webDriverConfig.getProxyHost() != null) {
        capability.setCapability(CapabilityType.PROXY, webDriverConfig.getProxy());
    }

    return capability;
}

From source file:com.elastica.browserfactory.HtmlUnitCapabilitiesFactory.java

License:Apache License

public DesiredCapabilities createCapabilities(final DriverConfig webDriverConfig) {
    DesiredCapabilities capability = null;
    capability = DesiredCapabilities.htmlUnit();

    if (webDriverConfig.isEnableJavascript()) {
        capability.setJavascriptEnabled(true);
    } else {//from ww w  .ja  v  a 2s  . com
        capability.setJavascriptEnabled(false);
    }

    capability.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
    capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

    if (webDriverConfig.getBrowserVersion() != null) {
        capability.setVersion(webDriverConfig.getBrowserVersion());
    }

    if (webDriverConfig.getPlatform() != null) {
        capability.setPlatform(webDriverConfig.getPlatform());
    }

    if (webDriverConfig.getProxyHost() != null) {
        Proxy proxy = webDriverConfig.getProxy();
        capability.setCapability(CapabilityType.PROXY, proxy);
    }

    return capability;
}

From source file:com.elastica.browserfactory.IECapabilitiesFactory.java

License:Apache License

public DesiredCapabilities createCapabilities(final DriverConfig cfg) {

    // Set IEDriver for Local Mode
    if (cfg.getMode() == DriverMode.LOCAL) {
        if (cfg.getIeDriverPath() != null) {
            System.setProperty("webdriver.ie.driver", cfg.getIeDriverPath());
        } else {/*from   w  w  w. j av  a  2  s  . co m*/
            if (System.getenv("webdriver.ie.driver") != null) {
                System.out.println("Get IE Driver from property:" + System.getenv("webdriver.ie.driver"));
                System.setProperty("webdriver.ie.driver", System.getenv("webdriver.ie.driver"));
            } else {
                try {
                    handleExtractResources();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    DesiredCapabilities capability = DesiredCapabilities.internetExplorer();

    capability.setBrowserName(DesiredCapabilities.internetExplorer().getBrowserName());

    if (cfg.isEnableJavascript()) {
        capability.setJavascriptEnabled(true);
    } else {
        capability.setJavascriptEnabled(false);
    }

    capability.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
    capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    capability.setCapability("ignoreZoomSetting", true);

    if (cfg.getBrowserVersion() != null) {
        capability.setVersion(cfg.getBrowserVersion());
    }

    if (cfg.getPlatform() != null) {
        capability.setPlatform(cfg.getPlatform());
    }

    if (cfg.getProxyHost() != null) {
        Proxy proxy = cfg.getProxy();
        capability.setCapability(CapabilityType.PROXY, proxy);
    }

    capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
    return capability;
}

From source file:com.elastica.browserfactory.OperaCapabilitiesFactory.java

License:Apache License

public DesiredCapabilities createCapabilities(final DriverConfig cfg) {
    DesiredCapabilities capability = DesiredCapabilities.opera();

    capability.setBrowserName(DesiredCapabilities.opera().getBrowserName());

    if (cfg.isEnableJavascript()) {
        capability.setJavascriptEnabled(true);
    } else {//from   w  w w  .  j av a2  s.c o  m
        capability.setJavascriptEnabled(false);
    }

    capability.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
    capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

    if (cfg.getBrowserVersion() != null) {
        capability.setVersion(cfg.getBrowserVersion());
    }

    if (cfg.getPlatform() != null) {
        capability.setPlatform(cfg.getPlatform());
    }

    OperaProfile profile = getOperaProfile(cfg);
    capability.setCapability("opera.profile", profile);

    if (cfg.getProxyHost() != null) {
        Proxy proxy = cfg.getProxy();
        capability.setCapability(CapabilityType.PROXY, proxy);
    }

    return capability;
}