Example usage for org.openqa.selenium.chrome ChromeDriver ChromeDriver

List of usage examples for org.openqa.selenium.chrome ChromeDriver ChromeDriver

Introduction

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

Prototype

public ChromeDriver(ChromeOptions options) 

Source Link

Document

Creates a new ChromeDriver instance with the specified options.

Usage

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

License:Apache License

/**
 * create native driver instance, designed for unit testing.
 *
 * @return//from  w  w  w .j a  v a  2  s  . co m
 */
protected WebDriver createNativeDriver() {
    return new ChromeDriver(new ChromeCapabilitiesFactory().createCapabilities(webDriverConfig));
}

From source file:com.emc.kibana.emailer.KibanaEmailer.java

License:Open Source License

private static void generatePdfReports() {

    try {//from w ww .  ja  v  a2 s .  com
        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);
    }
}

From source file:com.epam.jdi.uitests.web.selenium.driver.SeleniumDriverFactory.java

License:Open Source License

protected String registerLocalDriver(DriverTypes driverType) {
    switch (driverType) {
    case CHROME:/*from  w  ww .  j ava2 s.  co m*/
        return registerDriver(driverType, () -> {
            if (getLatestDriver) {
                return webDriverSettings.apply(initChrome());
            } else {
                setProperty("webdriver.chrome.driver", getChromeDriverPath(driversPath));
                return webDriverSettings.apply(new ChromeDriver(defaultChromeOptions()));
            }
        });
    case FIREFOX:
        return registerDriver(driverType, () -> {
            if (getLatestDriver) {
                return webDriverSettings.apply(initFirefox());
            } else {
                setProperty("webdriver.gecko.driver", getFirefoxDriverPath(driversPath));
                return webDriverSettings.apply(new FirefoxDriver(defaultFirefoxOptions()));
            }
        });
    case IE:
        return registerDriver(driverType, () -> {
            if (getLatestDriver) {
                return webDriverSettings.apply(initIE());
            } else {
                setProperty("webdriver.ie.driver", getIEDriverPath(driversPath));
                return webDriverSettings.apply(new InternetExplorerDriver(defaultIEOptions()));
            }
        });
    }
    throw exception("Unknown driver: " + driverType);
}

From source file:com.epam.jdi.uitests.web.selenium.driver.SeleniumDriverFactory.java

License:Open Source License

private WebDriver initChrome() {
    ChromeDriverManager.getInstance().setup();
    return new ChromeDriver(defaultChromeOptions());
}

From source file:com.expedia.lux.accountsettingstest.core.CreateWebDriverHelper.java

License:Open Source License

/**
 * Create webdriver entry/*from www .  j a  v  a2s .co m*/
 * 
 * @param testName
 * @param browserType
 *            1 is chrome, 2 is firefox, 0 is internet explorer, 3 is safari
 * @param logging
 * @return
 */
public static WebDriver createWebDriver(String testName, String testBrowser) {
    WebDriver driver = null;
    String isCloud = String.valueOf(isRemoteTest);

    if (!isRemoteTest) {
        DesiredCapabilities capabillities = new DesiredCapabilities();
        capabillities.setCapability(CapabilityType.SUPPORTS_WEB_STORAGE, false);
        switch (testBrowser.toUpperCase()) {
        case "IE":
            // Need set iedriver.exe file path
            System.setProperty("webdriver.ie.driver", ieDriverPath);
            driver = new InternetExplorerDriver(capabillities);
            break;
        case "FF":
            // TODO: Not stable
            capabillities.setCapability(FirefoxDriver.BINARY, new FirefoxBinary(new File(firefoxPath)));
            FirefoxProfile profile = new FirefoxProfile();
            profile.setAcceptUntrustedCertificates(true);
            capabillities.setCapability(FirefoxDriver.PROFILE, profile);
            driver = new FirefoxDriver(capabillities);
            break;
        case "SF":
            // TODO: No one's machine has safari installed in SZ
            driver = new SafariDriver(capabillities);
            break;
        default:
            // Need set the chromedriver.exe file path.
            System.setProperty("webdriver.chrome.driver", chromeDriverPath);
            driver = new ChromeDriver(capabillities);
            break;
        }
    } else {
        DesiredCapabilities capabillities;
        switch (testBrowser.toUpperCase()) {
        case "IE9":
            capabillities = DesiredCapabilities.internetExplorer();
            capabillities.setCapability(CapabilityType.VERSION, "9");
            break;
        case "FF":
            capabillities = DesiredCapabilities.firefox();
            break;
        case "SF":
            capabillities = DesiredCapabilities.safari();
            capabillities.setCapability("platform", "OS X 10.6");
            capabillities.setCapability("version", "5");
            capabillities.setCapability("disable-popup-handler", true);
            break;
        case "IE7":
            capabillities = DesiredCapabilities.internetExplorer();
            capabillities.setCapability(CapabilityType.VERSION, "7");
            break;
        case "IE8":
            capabillities = DesiredCapabilities.internetExplorer();
            capabillities.setCapability(CapabilityType.VERSION, "8");
            break;
        case "IE10":
            capabillities = DesiredCapabilities.internetExplorer();
            capabillities.setCapability(CapabilityType.VERSION, "10");
            break;
        case "IE11":
            capabillities = DesiredCapabilities.internetExplorer();
            capabillities.setCapability("version", "11");
            break;
        case "OP":
            capabillities = DesiredCapabilities.opera();
            capabillities.setCapability("version", "12");
            break;
        //Note: may not support HTTPS via sauceConnect with mobile platform yet
        case "IOS":
            capabillities = DesiredCapabilities.iphone();
            capabillities.setCapability("version", "6.1");
            capabillities.setCapability("platform", "OS X 10.8");
            capabillities.setCapability("device-orientation", "portrait");
            capabillities.setCapability("disable-popup-handler", true);
            break;
        case "AND":
            capabillities = DesiredCapabilities.android();
            capabillities.setCapability("version", "4.0");
            capabillities.setCapability("platform", "Linux");
            capabillities.setCapability("device-type", "tablet");
            capabillities.setCapability("device-orientation", "portrait");
            break;
        case "SAUCE":
            return createWebDriverSaucelab(testName + " (SauceLabs)");
        default:
            capabillities = DesiredCapabilities.chrome();
            capabillities.setBrowserName("chrome");
            break;
        }

        try {
            driver = new RemoteWebDriver(new URL("http://10.208.52.16:5555/wd/hub"), capabillities);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    fillBrowserName(testBrowser, isCloud);
    return driver;
}

From source file:com.formkiq.web.SeleniumTestBase.java

License:Apache License

/**
 * Create Chrome Driver.// www  . j a v  a2  s.com
 */
protected static void initializeChromeDriver() {

    String path = "src/test/resources/selenium/chromedriver";

    if (SystemUtils.IS_OS_WINDOWS) {
        path += ".exe";
    }

    System.setProperty("webdriver.chrome.driver", path);

    ChromeOptions options = new ChromeOptions();
    options.setHeadless(true);
    driver = new ChromeDriver(options);
    isInitialized = true;
}

From source file:com.fullteaching.backend.e2e.ChromeUser.java

License:Apache License

public ChromeUser(String userName, int timeOfWaitInSeconds) {
    super(userName, timeOfWaitInSeconds);

    ChromeOptions options = new ChromeOptions();
    // This flag avoids to grant the user media
    options.addArguments("--use-fake-ui-for-media-stream");
    // This flag fakes user media with synthetic video
    options.addArguments("--use-fake-device-for-media-stream");
    // This flag selects the entire screen as video source when screen sharing
    options.addArguments("--auto-select-desktop-capture-source=Entire screen");

    String eusApiURL = System.getenv("ET_EUS_API");

    if (eusApiURL == null) {
        this.driver = new ChromeDriver(options);
    } else {//w w w  . ja v a  2 s .  com

        try {
            DesiredCapabilities caps = new DesiredCapabilities();
            caps.setBrowserName("chrome");
            //caps.setVersion("61");
            caps.setCapability(ChromeOptions.CAPABILITY, options);

            this.driver = new RemoteWebDriver(new URL(eusApiURL), caps);

        } catch (MalformedURLException e) {
            throw new RuntimeException("Exception creaing eusApiURL", e);
        }
    }

    this.driver.manage().timeouts().setScriptTimeout(this.timeOfWaitInSeconds, TimeUnit.SECONDS);

    this.configureDriver();
}

From source file:com.galenframework.browser.SeleniumBrowserFactory.java

License:Apache License

public static WebDriver getDriver(String browserType) {

    if (StringUtils.isEmpty(browserType) || FIREFOX.equals(browserType)) {
        return new FirefoxDriver(SeleniumBrowserFactory.getBrowserCapabilities(browserType));
    } else if (CHROME.equals(browserType)) {
        return new ChromeDriver(SeleniumBrowserFactory.getBrowserCapabilities(browserType));
    } else if (IE.equals(browserType)) {
        return new InternetExplorerDriver(SeleniumBrowserFactory.getBrowserCapabilities(browserType));
    } else if (PHANTOMJS.equals(browserType)) {
        return new PhantomJSDriver();
    } else if (SAFARI.equals(browserType)) {
        return new SafariDriver();
    } else if (EDGE.equals(browserType)) {
        return new EdgeDriver();
    } else {//from   w  ww  .ja  v  a 2  s . c  o m
        throw new RuntimeException(String.format("Unknown browser type: \"%s\"", browserType));
    }
}

From source file:com.galois.fiveui.drivers.Drivers.java

License:Apache License

public static ChromeDriver buildChromeDriver() {

    String rootPath = getRootPath();

    // set the chrome driver path:
    String chromeDriverPth = mkPath(CD_BASE_PATH, osNameArch(), CD_BINARY_NAME);
    System.setProperty("webdriver.chrome.driver", chromeDriverPth);

    // setting the path to chrome also seems to cause issues:
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--user-data-dir=" + rootPath + chromeProfilePath); // ,
    // "--enable-logging",
    // "--v=1");//from  w  ww  .j  a v  a 2 s.co m

    String chromeBinaryPath = System.getProperty(CHROME_BIN_PATH);
    if (null == chromeBinaryPath) {
        System.err.println("WARNING: Running essentially random version of Chrome!");
        System.err.println("         set a path to Chrome with -D" + CHROME_BIN_PATH + "=<path to chrome>");
    } else {
        options.setBinary(new File(chromeBinaryPath));
    }
    // For use with ChromeDriver:
    return new ChromeDriver(options);
}

From source file:com.gargoylesoftware.htmlunit.WebDriverTestCase.java

License:Apache License

/**
 * Builds a new WebDriver instance.//from   w w  w .j a v  a 2  s  .  c  o  m
 * @return the instance
 * @throws IOException in case of exception
 */
protected WebDriver buildWebDriver() throws IOException {
    if (useRealBrowser()) {
        if (getBrowserVersion().isIE()) {
            if (IE_BIN_ != null) {
                System.setProperty("webdriver.ie.driver", IE_BIN_);
            }
            return new InternetExplorerDriver();
        }

        if (BrowserVersion.CHROME == getBrowserVersion()) {
            if (CHROME_SERVICE_ == null) {
                final ChromeDriverService.Builder builder = new ChromeDriverService.Builder();
                if (CHROME_BIN_ != null) {
                    builder.usingDriverExecutable(new File(CHROME_BIN_));
                }
                CHROME_SERVICE_ = builder.usingAnyFreePort().build();

                CHROME_SERVICE_.start();
            }
            return new ChromeDriver(CHROME_SERVICE_);
        }

        if (BrowserVersion.EDGE == getBrowserVersion()) {
            if (EDGE_BIN_ != null) {
                System.setProperty("webdriver.edge.driver", EDGE_BIN_);
            }
            return new EdgeDriver();
        }

        if (BrowserVersion.FIREFOX_45 == getBrowserVersion()) {
            // disable the new marionette interface because it requires ff47 or more
            System.setProperty("webdriver.firefox.marionette", "false");

            if (FF45_BIN_ != null) {
                final FirefoxOptions options = new FirefoxOptions();
                options.setBinary(FF45_BIN_);
                return new FirefoxDriver(options);
            }
            return new FirefoxDriver();
        }

        if (BrowserVersion.FIREFOX_52 == getBrowserVersion()) {
            if (FF52_BIN_ != null) {
                final FirefoxOptions options = new FirefoxOptions();
                options.setBinary(FF52_BIN_);
                return new FirefoxDriver(options);
            }
            return new FirefoxDriver();
        }

        throw new RuntimeException("Unexpected BrowserVersion: " + getBrowserVersion());
    }
    if (webDriver_ == null) {
        final DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setBrowserName(BrowserType.HTMLUNIT);
        capabilities.setVersion(getBrowserName(getBrowserVersion()));
        webDriver_ = new HtmlUnitDriver(capabilities);
    }
    return webDriver_;
}