Example usage for org.openqa.selenium.chrome ChromeOptions CAPABILITY

List of usage examples for org.openqa.selenium.chrome ChromeOptions CAPABILITY

Introduction

In this page you can find the example usage for org.openqa.selenium.chrome ChromeOptions CAPABILITY.

Prototype

String CAPABILITY

To view the source code for org.openqa.selenium.chrome ChromeOptions CAPABILITY.

Click Source Link

Document

Key used to store a set of ChromeOptions in a Capabilities object.

Usage

From source file:chromedriveryahoologin.ChromeDriverDeleteTrashEmails.java

public static void main(String[] args) {
    try {//from   w  ww  .j  av  a2s.c o  m

        Integer pozSpam = 0;
        Integer pozTrash = 0;
        Integer pozHelp = 0;
        Integer pozCompose = 0;
        Integer pozNewFolder = 0;

        System.setProperty("webdriver.chrome.driver",
                "D:\\Proiecte\\selenium-java-2.47.1\\selenium-2.47.1\\chromedriver_win32\\chromedriver.exe");
        System.setProperty("webdriver.chrome.logfile",
                "D:\\prjAutJava\\YahooLogin\\ChromeDriverDeleteSpam.log");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("test-type");
        options.addArguments("--start-maximized");
        options.addArguments("--disable-web-security");
        options.addArguments("--no-proxy-server");
        options.addArguments("--disable-extensions");
        options.addArguments("--disable-notifications");
        options.addArguments("--disable-popup-blocking");
        options.addArguments("--disable-plug-in");
        Map<String, Object> prefs = new HashMap<String, Object>();
        prefs.put("credentials_enable_service", false);
        prefs.put("profile.password_manager_enabled", false);

        options.setExperimentalOption("prefs", prefs);

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        capabilities.setCapability(CapabilityType.SUPPORTS_APPLICATION_CACHE, true);
        capabilities.setCapability(CapabilityType.SUPPORTS_FINDING_BY_CSS, true);
        capabilities.setCapability(CapabilityType.SUPPORTS_LOCATION_CONTEXT, true);
        capabilities.setCapability(CapabilityType.SUPPORTS_ALERTS, true);
        capabilities.setCapability(CapabilityType.PLATFORM, "WIN10");

        driver = new ChromeDriver(capabilities);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("https://mail.yahoo.com");
        driver.manage().window().maximize();
        WebElement usernameElem = driver.findElement(By.id("login-username"));
        usernameElem.sendKeys("andadeacu@yahoo.com");

        WebElement Next = driver.findElement(By.name("signin"));
        Next.click();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        WebElement passwordElem = driver.findElement(By.xpath("//input[@id='login-passwd']"));
        boolean selected = passwordElem.isSelected();

        CharSequence password = "";
        passwordElem.sendKeys(password);

        WebElement login = driver.findElement(By.id("login-signin"));

        login.click();
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

        driver.switchTo().activeElement();

        WebElement tooltipElem = driver.findElement(By.xpath("//div[@class='Pos(r) M(0) TooltipContent']"));

        List<WebElement> childs = tooltipElem.findElements(By.xpath("//ul/li"));

        for (int j = 0; j < childs.size(); j++) {

            WebElement spamElem = childs.get(j);
            /*
            * System.out.println("ancorele " + j + " " + " tag " +
            * childs.get(j).getTagName() + " " + childs.get(j).getText() +
            * " " + childs.get(j).getAttribute("innerHTML"));
             */
            if (spamElem.getText().startsWith(("Spam"))) {

                pozSpam = j;

            }
            if (spamElem.getText().startsWith(("Help"))) {

                pozHelp = j;

            }
            if (spamElem.getText().startsWith(("Compose"))) {

                pozCompose = j;

            }
            if (spamElem.getText().startsWith(("New"))) {

                pozNewFolder = j;

            }

            if (spamElem.getText().startsWith(("Trash"))) {
                System.out.println(j + " Trash is " + spamElem.getTagName() + " " + spamElem.getText());
                pozTrash = j;
                break;
            }

        }
        System.out.println(pozTrash);

        WebElement trashElem = childs.get(pozTrash);

        /*
        * System.out.println("trashElem " + trashElem.getTagName() +
        * " text " + trashElem.getText() + " class_atribute " +
        * trashElem.getAttribute("class") + " data_action " +
        * trashElem.getAttribute("data_action"));
         */
        WebElement trashFolderEmpty = trashElem.findElement(
                By.xpath("//a[@class='x-gap btn btn-trash']/span/span[@class='btn icon  icon-delete']"));

        /*
        * System.out.println("spamEmptyFolder " +
        * spamFolderEmpty.getTagName() + " text " +
        * spamFolderEmpty.getText() + " location " +
        * spamFolderEmpty.isEnabled());
         */
        trashFolderEmpty.click();

        int timeOut = 5;

        WebDriverWait wait = new WebDriverWait(driver, timeOut);

        driver.switchTo().alert();

        /* for Chrome is not working 
        WebElement modalPopUp = driver.findElement(By
            .className("modal-hd yui3-widget-hd"));
                
        */

        WebElement btnDeleteSpamEmails = driver.findElement(By.className("btn left right default"));

        /*
        * <button id="okayModalOverlay" class="btn left right default"
        * title="OK" role="button" data-action="ok">OK</button>
         */
        btnDeleteSpamEmails.click();

        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

        driver.switchTo().activeElement();

        // verify msg after you delete spam emails
        System.out.println(driver.getCurrentUrl());

        driver.quit();
    } catch (Exception ex) {

        ex.printStackTrace();
        driver.quit();
    }

}

From source file:com.atanas.kanchev.testframework.selenium.driverfactory.DesiredCapsFactory.java

License:Apache License

/**
 * <p>setCustomChromeOptions.</p>
 *
 * @param chromeOptions a {@link org.openqa.selenium.chrome.ChromeOptions} object.
 * @return a {@link org.openqa.selenium.remote.DesiredCapabilities} object.
 *///from  www . j a va  2 s  .c o  m
public DesiredCapabilities setCustomChromeOptions(ChromeOptions chromeOptions) {

    if (chromeOptions != null) {
        try {
            if (caps == null)
                caps = DesiredCapabilities.chrome();
            caps.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
            logger.debug("Setting custom Chrome Options: " + chromeOptions.toJson());
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
    } else
        throw new IllegalArgumentException("Null ChromeOptions argument");

    return caps;
}

From source file:com.atanas.kanchev.testframework.selenium.driverfactory.DesiredCapsFactory.java

License:Apache License

/**
 * <p>setCustomChromeOptions.</p>
 *
 * @param chromeOptions a {@link java.util.Map} object.
 * @return a {@link org.openqa.selenium.remote.DesiredCapabilities} object.
 *///  ww  w .  j a v  a2  s . c om
public DesiredCapabilities setCustomChromeOptions(Map<String, Object> chromeOptions) {

    if (chromeOptions != null) {
        if (caps == null)
            caps = DesiredCapabilities.chrome();
        caps.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
        logger.debug("Setting custom Chrome Options: " + chromeOptions.entrySet());
    } else
        throw new IllegalArgumentException("Null ChromeOptions argument");

    return caps;
}

From source file:com.axatrikx.webdriver.ChromeLoader.java

License:Apache License

@Override
public WebDriver getDriver() {
    String resource = "chromedriver.exe";
    if (Utils.getOS().equals("Unix")) {
        resource = "chromedriver";
    }//from w w w. ja  v a2  s . c o  m
    System.setProperty("webdriver.chrome.driver", Utils.getResource(resource).getAbsolutePath());
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("disable-popup-blocking");
    Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("download.default_directory", downloadDir);
    prefs.put("profile.default_content_settings.popups", 0);

    // set chrome custom binary
    if (!chromeBinaryPath.isEmpty()) {
        prefs.put("binary", chromeBinaryPath);
    }

    chromeOptions.setExperimentalOption("prefs", prefs);

    if (useProxy) {
        String proxyURL = proxyHost + ":" + proxyPort;
        Proxy proxy = new Proxy();
        proxy.setHttpProxy(proxyURL).setFtpProxy(proxyURL).setSslProxy(proxyURL);
        capabilities.setCapability(CapabilityType.PROXY, proxy);
    }

    if (!chromeProfilePath.isEmpty()) {
        chromeOptions.addArguments("user-data-dir=" + chromeProfilePath);
    }

    capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
    return new ChromeDriver(capabilities);
}

From source file:com.ceiwc.compugain.setup.TestBase.java

public DesiredCapabilities chromeProfileSetUp() {
    System.setProperty("webdriver.chrome.driver", getFilePath(CHROMEPATH));
    String downloadFilepath = getFilePath(DOWNLOADSPATH);
    HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
    chromePrefs.put("profile.default_content_settings.popups", 0);
    chromePrefs.put("download.default_directory", downloadFilepath);
    ChromeOptions options = new ChromeOptions();
    HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
    options.setExperimentalOption("prefs", chromePrefs);
    options.addArguments("--test-type");
    DesiredCapabilities cap = DesiredCapabilities.chrome();
    cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
    cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    cap.setCapability(ChromeOptions.CAPABILITY, options);
    options.addArguments("--always-authorize-plugins");
    options.addArguments("--ignore-certificate-errors");
    //   logger.info("The Chrome Profile is setup");
    return cap;/*  w  w w .  ja v a  2s . c om*/
}

From source file:com.chtr.tmoauto.webui.CommonFunctions.java

License:Open Source License

/**
 * This method build a webDriver based on the passed in browser request
 *
 * @param browser//  w w w . j  a v  a2s  .c o  m
 * @return WebDriver
 * @throws MalformedURLException
 */
private static WebDriver buildWebDriver(String browserName) {
    DesiredCapabilities capability = null;
    BrowserType browserType = BrowserType.getBrowserTypeFromString(browserName);
    switch (browserType) {
    case MARIONETTE:
        FirefoxProfile ffProfile = null;
        ffProfile = new FirefoxProfile();
        ffProfile.setAcceptUntrustedCertificates(true);
        ffProfile.setAssumeUntrustedCertificateIssuer(false);
        DesiredCapabilities cap = DesiredCapabilities.firefox();
        cap.setCapability("marionette", true);
        cap.setCapability("firefox_profile", ffProfile);
        cap.setCapability("handlesAlerts", true);
        sysEnv = System.getenv("webdriver.firefox.marionette");
        if (sysEnv == null) {
            sysEnv = findFileName("C:/apps/selenium", "geckodriver.exe", FileSearchType.File);
            if (sysEnv == null) {
                log.info(
                        "Please set the webdriver.firefox.marionette in system environment variables and restart the PC OR copy all your webdrivers under 'C:/selenium' location");
                throw new RuntimeException("Failed to instantiate a WebDriver instance for " + browserName);
            } else {
                System.setProperty("webdriver.firefox.marionette", sysEnv);
            }
        }
        return new MarionetteDriver(capability);
    case FIREFOX_DRIVER:
        capability = DesiredCapabilities.firefox();
        FirefoxProfile firefoxProfile = new FirefoxProfile();
        firefoxProfile.setAcceptUntrustedCertificates(true);
        firefoxProfile.setEnableNativeEvents(true);
        firefoxProfile.setAssumeUntrustedCertificateIssuer(true);
        capability.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
        return new FirefoxDriver(capability);
    case CHROME_DRIVER:
        sysEnv = System.getenv("webdriver.chrome.driver");
        if (sysEnv == null) {
            sysEnv = findFileName("C:/apps/selenium", "chromedriver.exe", FileSearchType.File);
            if (sysEnv == null) {
                log.info(
                        "Please set the webdriver.chrome.driver in system environment variables and restart the PC OR copy all your webdrivers under 'C:/selenium' location");
                throw new RuntimeException("Failed to instantiate a WebDriver instance for " + browserName);
            } else {
                System.setProperty("webdriver.chrome.driver", sysEnv);
            }
        }
        capability = DesiredCapabilities.chrome();
        ChromeOptions options = new ChromeOptions();
        options.addArguments(new String[] { "--allow-running-insecure-content" });
        options.addArguments(new String[] { "--ignore-certificate-errors" });
        options.addArguments(new String[] { "--enable-npapi" });
        options.addArguments(new String[] { "--disable-extensions" });
        options.addArguments(new String[] { "--start-maximized" });
        capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        capability.setCapability(ChromeOptions.CAPABILITY, options);
        return new ChromeDriver(capability);
    case INTERNET_EXPLORER:
        sysEnv = System.getenv("webdriver.ie.driver");
        if (sysEnv == null) {
            sysEnv = findFileName("C:/apps/selenium", "IEDriverServer.exe", FileSearchType.File);
            if (sysEnv == null) {
                log.info(
                        "Please set the webdriver.ie.driver in system environment variables and restart the PC");
                throw new RuntimeException("Failed to instantiate a WebDriver instance for " + browserName);
            } else {
                System.setProperty("webdriver.ie.driver", sysEnv);
            }
        }
        capability = DesiredCapabilities.internetExplorer();
        capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
        capability.setCapability("ignoreProtectedModeSettings", true);
        capability.setCapability("acceptSslCerts", true);
        capability.setCapability("ignoreZoomSetting", true);
        capability.setCapability("nativeEvents", true);
        capability.setCapability("ie.ensureCleanSession", true);
        return new InternetExplorerDriver(capability);
    case SAFARI:
        capability = DesiredCapabilities.safari();
        capability.setCapability("acceptSslCerts", true);
        capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        capability.setCapability("ensureCleanSession", true);
        capability.setJavascriptEnabled(true);
        return new SafariDriver(capability);
    /*        case OPERA_DRIVER:
    capability = DesiredCapabilities.opera();
    capability.setCapability("opera.host", "127.0.0.1");
    return new OperaDriver();
     */
    case EDGE:
        capability = DesiredCapabilities.edge();
        EdgeOptions option = new EdgeOptions();
        capability.setCapability("edgeOptions", option);
        return new EdgeDriver(capability);
    default:
        log.info(
                "Currenty support is there for Chrome, Firefox, Firefox Marionette, Internet Explorer, Edge, Safari & Opera. Support is not there for "
                        + browserName);
        capability = DesiredCapabilities.firefox();
        FirefoxProfile firefoxProf = new FirefoxProfile();
        firefoxProf.setAcceptUntrustedCertificates(true);
        firefoxProf.setEnableNativeEvents(true);
        firefoxProf.setAssumeUntrustedCertificateIssuer(true);
        capability.setCapability(FirefoxDriver.PROFILE, firefoxProf);
        return new FirefoxDriver(capability);
    }
}

From source file:com.chtr.tmoauto.webui.CommonFunctions.java

License:Open Source License

/**
 * This method build a RemoteWebDriver based on the passed in browser request
 *
 * @param browser//from www . j  a va2  s.c om
 * @return RemoteWebDriver
 *
 */
private static RemoteWebDriver buildRemoteWebDriver(String browserName) {
    DesiredCapabilities capability = null;
    BrowserType browserType = BrowserType.getBrowserTypeFromString(browserName);
    switch (browserType) {
    case MARIONETTE:
        FirefoxProfile ffProfile = null;
        ffProfile = new FirefoxProfile();
        ffProfile.setAcceptUntrustedCertificates(true);
        ffProfile.setAssumeUntrustedCertificateIssuer(false);
        DesiredCapabilities cap = DesiredCapabilities.firefox();
        cap.setCapability("marionette", true);
        cap.setCapability("firefox_profile", ffProfile);
        cap.setCapability("handlesAlerts", true);
        sysEnv = System.getenv("webdriver.firefox.marionette");
        if (sysEnv == null) {
            sysEnv = findFileName("C:/apps/selenium", "geckodriver.exe", FileSearchType.File);
            if (sysEnv == null) {
                log.info(
                        "Please set the webdriver.firefox.marionette in system environment variables and restart the PC OR copy all your webdrivers under 'C:/selenium' location");
                throw new RuntimeException("Failed to instantiate a WebDriver instance for " + browserName);
            } else {
                System.setProperty("webdriver.firefox.marionette", sysEnv);
            }
        }
        return new MarionetteDriver(capability);
    case FIREFOX_DRIVER:
        capability = DesiredCapabilities.firefox();
        FirefoxProfile firefoxProfile = new FirefoxProfile();
        firefoxProfile.setAcceptUntrustedCertificates(true);
        firefoxProfile.setEnableNativeEvents(true);
        capability.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
        capability.setPlatform(capability.getPlatform());
        capability.setVersion(capability.getVersion());
        return new FirefoxDriver(capability);
    case CHROME_DRIVER:
        sysEnv = System.getenv("webdriver.chrome.driver");
        if (sysEnv == null) {
            sysEnv = findFileName("C:/apps/selenium", "chromedriver.exe", FileSearchType.File);
            if (sysEnv == null) {
                log.info(
                        "Please set the webdriver.chrome.driver in system environment variables and restart the PC OR copy all your webdrivers under 'C:/selenium' location");
                throw new RuntimeException("Failed to instantiate a WebDriver instance for " + browserName);
            } else {
                System.setProperty("webdriver.chrome.driver", sysEnv);
            }
        }
        capability = DesiredCapabilities.chrome();
        ChromeOptions options = new ChromeOptions();
        options.addArguments(new String[] { "--allow-running-insecure-content" });
        options.addArguments(new String[] { "--ignore-certificate-errors" });
        options.addArguments(new String[] { "--enable-npapi" });
        options.addArguments(new String[] { "--disable-extensions" });
        options.addArguments(new String[] { "--start-maximized" });
        capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        capability.setCapability(ChromeOptions.CAPABILITY, options);
        capability.setPlatform(capability.getPlatform());
        capability.setVersion(capability.getVersion());
        return new ChromeDriver(capability);
    case INTERNET_EXPLORER:
        sysEnv = System.getenv("webdriver.ie.driver");
        if (sysEnv == null) {
            sysEnv = findFileName("C:/apps/selenium", "IEDriverServer.exe", FileSearchType.File);
            if (sysEnv == null) {
                log.info(
                        "Please set the webdriver.ie.driver in system environment variables and restart the PC");
                throw new RuntimeException("Failed to instantiate a WebDriver instance for " + browserName);
            } else {
                System.setProperty("webdriver.ie.driver", sysEnv);
            }
        }
        capability = DesiredCapabilities.internetExplorer();
        capability.setCapability("ignoreProtectedModeSettings", true);
        String browserVersion = capability.getVersion();
        if (browserVersion != null && browserVersion.equals("10")) {
            capability.setPlatform(Platform.WINDOWS);
            capability.setVersion(browserVersion);
        } else if (browserVersion != null && browserVersion.equals("11")) {
            capability.setPlatform(Platform.WIN8_1);
            capability.setVersion(browserVersion);
        }
        capability.setBrowserName("internet explorer");
        capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        return new InternetExplorerDriver(capability);
    case SAFARI:
        capability = DesiredCapabilities.safari();
        capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        capability.setCapability("ensureCleanSession", true);
        capability.setPlatform(capability.getPlatform());
        capability.setVersion(null);
        return new SafariDriver(capability);
    /*         case OPERA_DRIVER:
    capability = DesiredCapabilities.opera();
    capability.setCapability("opera.profile", "/test");
    return new OperaDriver();
    */
    case EDGE:
        capability = DesiredCapabilities.edge();
        EdgeOptions option = new EdgeOptions();
        capability.setCapability("edgeOptions", option);
        return new EdgeDriver(capability);
    default:
        log.info(
                "Currenty support is there for Chrome, Firefox, Firefox Marionette, Internet Explorer, Edge, Safari & Opera. Support is not there for "
                        + browserName);
        capability = DesiredCapabilities.firefox();
        firefoxProfile = new FirefoxProfile();
        firefoxProfile.setAcceptUntrustedCertificates(true);
        firefoxProfile.setEnableNativeEvents(true);
        capability.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
        capability.setPlatform(capability.getPlatform());
        capability.setVersion(capability.getVersion());
        return new FirefoxDriver(capability);
    }
}

From source file:com.comcast.dawg.selenium.BrowserServiceManager.java

License:Apache License

/**
 * Provides the browser desired capabilities.
 *
 * @param browser//from  w ww.j a  va2  s .c  o  m
 *        Browser for which the desired capabilities to be returned.
 * @return Capabilities corresponding to the browser passed.
 */
private static Capabilities getDesiredBrowserCapabilities(Browser browser) {
    DesiredCapabilities capabilities = null;
    switch (browser) {
    case chrome:
        ChromeOptions options = new ChromeOptions();
        options.addArguments(CHROME_OPTION_ARGUMENTS);
        capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        break;
    default:
        capabilities = new DesiredCapabilities();
        break;
    }
    return capabilities;
}

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 ww.ja  v  a  2 s.  com*/

    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.emc.kibana.emailer.KibanaEmailer.java

License:Open Source License

private static void generatePdfReports() {

    try {/*www.j a  v  a  2 s.c o m*/
        System.setProperty("webdriver.chrome.driver", chromeDriverPath);

        Map<String, Object> chromeOptions = new HashMap<String, Object>();

        // Specify alternate browser location
        if (chromeBrowserPath != null && !chromeBrowserPath.isEmpty()) {
            chromeOptions.put("binary", chromeBrowserPath);
        }

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();

        capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

        int index = 1;
        Date timestamp = new Date(System.currentTimeMillis());

        for (Map<String, String> entry : kibanaUrls) {

            String dashboardName = entry.get(NAME_KEY);
            String dashboardUrl = entry.get(URL_KEY);

            if (dashboardUrl == null) {
                continue;
            }

            String filename;

            if ((dashboardName != null)) {
                String invalidCharRemoved = dashboardName.replaceAll("[\\/:\"*?<>|]+", "_").replaceAll(" ",
                        "_");
                filename = invalidCharRemoved + ".png";
            } else {
                filename = "dashboard-" + index++ + ".png";
            }

            WebDriver driver = new ChromeDriver(capabilities);
            driver.manage().window().maximize();
            driver.get(dashboardUrl);

            // let kibana load for x seconds before taking the snapshot
            Integer delay = (screenCaptureDelay != null) ? (screenCaptureDelay * 1000) : (20000);
            Thread.sleep(delay);
            // take screenshot
            File scrnshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

            // generate absolute path filename
            String absoluteFileName = destinationPath + File.separator + DATA_DATE_FORMAT.format(timestamp)
                    + File.separator + filename;
            Map<String, String> fileMap = new HashMap<String, String>();
            // file name portion
            fileMap.put(FILE_NAME_KEY, filename);
            fileMap.put(ABSOLUE_FILE_NAME_KEY, absoluteFileName);

            kibanaScreenCaptures.add(fileMap);
            File destFile = new File(absoluteFileName);

            logger.info("Copying " + scrnshot + " in " + destFile.getAbsolutePath());

            FileUtils.copyFile(scrnshot, destFile);
            driver.close();
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } catch (IOException e1) {
        throw new RuntimeException(e1);
    }
}