Search in sources :

Example 61 with ChromeDriver

use of org.openqa.selenium.chrome.ChromeDriver in project xwiki-platform by xwiki.

the class WebDriverFactory method createWebDriver.

public XWikiWebDriver createWebDriver(String browserName) {
    WebDriver driver;
    if (browserName.startsWith("*firefox")) {
        // Native events are disabled by default for Firefox on Linux as it may cause tests which open many windows
        // in parallel to be unreliable. However, native events work quite well otherwise and are essential for some
        // of the new actions of the Advanced User Interaction. We need native events to be enable especially for
        // testing the WYSIWYG editor. See http://code.google.com/p/selenium/issues/detail?id=2331 .
        FirefoxProfile profile = new FirefoxProfile();
        profile.setEnableNativeEvents(true);
        // Make sure Firefox doesn't upgrade automatically on CI agents.
        profile.setPreference("app.update.auto", false);
        profile.setPreference("app.update.enabled", false);
        profile.setPreference("app.update.silent", false);
        driver = new FirefoxDriver(profile);
        // Hide the Add-on bar (from the bottom of the window, with "WebDriver" written on the right) because it can
        // prevent buttons or links from being clicked when they are beneath it and native events are used.
        // See https://groups.google.com/forum/#!msg/selenium-users/gBozOynEjs8/XDxxQNmUSCsJ
        // We need to load a page before sending the keys otherwise WebDriver throws ElementNotVisible exception.
        driver.get("data:text/plain;charset=utf-8,XWiki");
        driver.switchTo().activeElement().sendKeys(Keys.chord(Keys.CONTROL, "/"));
    } else if (browserName.startsWith("*iexplore")) {
        driver = new InternetExplorerDriver();
    } else if (browserName.startsWith("*chrome")) {
        driver = new ChromeDriver();
    } else if (browserName.startsWith("*phantomjs")) {
        DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
        capabilities.setCapability("handlesAlerts", true);
        try {
            driver = new PhantomJSDriver(ResolvingPhantomJSDriverService.createDefaultService(), capabilities);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } else {
        throw new RuntimeException("Unsupported browser name [" + browserName + "]");
    }
    // Maximize the browser window by default so that the page has a standard layout. Individual tests can resize
    // the browser window if they want to test how the page layout adapts to limited space. This reduces the
    // probability of a test failure caused by an unexpected layout (nested scroll bars, floating menu over links
    // and buttons and so on).
    driver.manage().window().maximize();
    return new XWikiWebDriver((RemoteWebDriver) driver);
}
Also used : WebDriver(org.openqa.selenium.WebDriver) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) PhantomJSDriver(org.openqa.selenium.phantomjs.PhantomJSDriver) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) InternetExplorerDriver(org.openqa.selenium.ie.InternetExplorerDriver) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) IOException(java.io.IOException) FirefoxProfile(org.openqa.selenium.firefox.FirefoxProfile) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver)

Example 62 with ChromeDriver

use of org.openqa.selenium.chrome.ChromeDriver in project ecs-dashboard by carone1.

the class KibanaEmailer method generatePdfReports.

/*
	 * Utility method that uses ChromeDriver to programmatically control
	 * the Chrome Browser to take dashboard screen captures 
	 */
private static void generatePdfReports() {
    try {
        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);
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) HashMap(java.util.HashMap) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) IOException(java.io.IOException) Date(java.util.Date) TakesScreenshot(org.openqa.selenium.TakesScreenshot) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) File(java.io.File)

Example 63 with ChromeDriver

use of org.openqa.selenium.chrome.ChromeDriver in project selenified by Coveros.

the class TestSetup method setupDriver.

/**
 * this creates the webdriver object, which will be used to interact with
 * for all browser web tests
 *
 * @param browser      - what browser is being tested on
 * @param capabilities - what capabilities are being tested with
 * @return WebDriver: the driver to interact with for the test
 * @throws InvalidBrowserException If a browser that is not one specified in the
 *                                 Selenium.Browser class is used, this exception will be thrown
 */
public static WebDriver setupDriver(Browser browser, DesiredCapabilities capabilities) throws InvalidBrowserException {
    WebDriver driver;
    // check the browser
    switch(browser) {
        case HTMLUNIT:
            capabilities.setBrowserName("htmlunit");
            capabilities.setJavascriptEnabled(true);
            System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog", "fatal");
            java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);
            java.util.logging.Logger.getLogger("org.apache.http").setLevel(Level.OFF);
            driver = new HtmlUnitDriver(capabilities);
            break;
        case FIREFOX:
            FirefoxDriverManager.getInstance().forceCache().setup();
            FirefoxOptions firefoxOptions = new FirefoxOptions(capabilities);
            if (System.getProperty(HEADLESS_INPUT) != null && "true".equals(System.getProperty(HEADLESS_INPUT))) {
                firefoxOptions.setHeadless(true);
            }
            driver = new FirefoxDriver(firefoxOptions);
            break;
        case CHROME:
            ChromeDriverManager.getInstance().forceCache().setup();
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions = chromeOptions.merge(capabilities);
            if (System.getProperty(HEADLESS_INPUT) != null && "true".equals(System.getProperty(HEADLESS_INPUT))) {
                chromeOptions.setHeadless(true);
            }
            driver = new ChromeDriver(chromeOptions);
            break;
        case INTERNETEXPLORER:
            InternetExplorerDriverManager.getInstance().forceCache().setup();
            InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions(capabilities);
            driver = new InternetExplorerDriver(internetExplorerOptions);
            break;
        case EDGE:
            EdgeDriverManager.getInstance().forceCache().setup();
            EdgeOptions edgeOptions = new EdgeOptions();
            edgeOptions = edgeOptions.merge(capabilities);
            driver = new EdgeDriver(edgeOptions);
            break;
        case SAFARI:
            SafariOptions safariOptions = new SafariOptions(capabilities);
            driver = new SafariDriver(safariOptions);
            break;
        case OPERA:
            OperaDriverManager.getInstance().forceCache().setup();
            driver = new OperaDriver(capabilities);
            break;
        case PHANTOMJS:
            PhantomJsDriverManager.getInstance().forceCache().setup();
            driver = new PhantomJSDriver(capabilities);
            break;
        // if the browser is not listed, throw an error
        default:
            throw new InvalidBrowserException("The selected browser " + browser + " is not an applicable choice");
    }
    return driver;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) PhantomJSDriver(org.openqa.selenium.phantomjs.PhantomJSDriver) OperaDriver(org.openqa.selenium.opera.OperaDriver) InternetExplorerDriver(org.openqa.selenium.ie.InternetExplorerDriver) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver) InternetExplorerOptions(org.openqa.selenium.ie.InternetExplorerOptions) SafariOptions(org.openqa.selenium.safari.SafariOptions) SafariDriver(org.openqa.selenium.safari.SafariDriver) FirefoxOptions(org.openqa.selenium.firefox.FirefoxOptions) EdgeOptions(org.openqa.selenium.edge.EdgeOptions) EdgeDriver(org.openqa.selenium.edge.EdgeDriver) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) InvalidBrowserException(com.coveros.selenified.exceptions.InvalidBrowserException)

Example 64 with ChromeDriver

use of org.openqa.selenium.chrome.ChromeDriver in project opentest by mcdcorp.

the class SeleniumHelper method createDriver.

private static WebDriver createDriver() {
    WebDriver webDriver = null;
    DesiredCapabilities caps;
    setSystemProperties();
    if (config.hasProperty("selenium.seleniumServerUrl")) {
        String seleniumServerUrl = config.getString("selenium.seleniumServerUrl");
        Logger.info(String.format("Using remote Selenium server %s", seleniumServerUrl));
        try {
            caps = new DesiredCapabilities();
            injectCapsFromConfig(caps);
            webDriver = new RemoteWebDriver(new URL(seleniumServerUrl), caps);
        } catch (Exception ex) {
            throw new RuntimeException(String.format("Falied to connect to Selenium server %s", seleniumServerUrl), ex);
        }
    } else {
        Logger.info("Using local Selenium server");
        String browserName = config.getString("selenium.desiredCapabilities.browserName").toLowerCase();
        switch(browserName) {
            case "chrome":
                setDriverExecutable(config.getString("selenium.chromeDriverExePath", null), "chrome");
                caps = getCapsForBrowser("chrome");
                injectCapsFromConfig(caps);
                ChromeOptions chromeOptions = new ChromeOptions();
                chromeOptions.merge(caps);
                webDriver = new ChromeDriver(chromeOptions);
                break;
            case "edge":
                setDriverExecutable(config.getString("selenium.edgeDriverExePath", null), "edge");
                caps = getCapsForBrowser("edge");
                injectCapsFromConfig(caps);
                webDriver = new EdgeDriver(caps);
                break;
            case "firefox":
                setDriverExecutable(config.getString("selenium.firefoxDriverExePath", null), "firefox");
                caps = getCapsForBrowser("firefox");
                injectCapsFromConfig(caps);
                FirefoxOptions firefoxOptions = new FirefoxOptions();
                firefoxOptions.merge(caps);
                webDriver = new FirefoxDriver(firefoxOptions);
                break;
            case "internet explorer":
                setDriverExecutable(config.getString("selenium.ieDriverExePath", null), "internet explorer");
                caps = getCapsForBrowser("internet explorer");
                // Avoid the browser zoom level error
                caps.setCapability("ignoreZoomSetting", true);
                injectCapsFromConfig(caps);
                InternetExplorerOptions ieOptions = new InternetExplorerOptions();
                ieOptions.merge(caps);
                webDriver = new InternetExplorerDriver(ieOptions);
                break;
            case "opera":
                setDriverExecutable(config.getString("selenium.operaDriverExePath", null), "opera");
                caps = getCapsForBrowser("opera");
                injectCapsFromConfig(caps);
                webDriver = new OperaDriver(caps);
                break;
            case "safari":
                setDriverExecutable(config.getString("selenium.safariDriverExePath", null), "safari");
                caps = getCapsForBrowser("safari");
                injectCapsFromConfig(caps);
                webDriver = new SafariDriver();
                break;
            default:
                throw new RuntimeException(String.format("The \"selenium.browserName\" config property specifies a browser " + "that is invalid or not supported. The property value was \"%s\". " + "The valid values are: \"chrome\", \"edge\", \"firefox\", \"internet " + "explorer\" and \"safari\".", browserName));
        }
    }
    webDriver.manage().timeouts().setScriptTimeout(config.getInteger("selenium.scriptTimeout", 20), TimeUnit.SECONDS);
    webDriver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
    Boolean maximizeWindow = config.getBoolean("selenium.maximizeWindow", true);
    if (maximizeWindow) {
        try {
            webDriver.manage().window().maximize();
        } catch (Exception ex) {
            Logger.warning(String.format("Failed to maximize browser window"), ex);
        }
    }
    return webDriver;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) OperaDriver(org.openqa.selenium.opera.OperaDriver) InternetExplorerDriver(org.openqa.selenium.ie.InternetExplorerDriver) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) URL(java.net.URL) InternetExplorerOptions(org.openqa.selenium.ie.InternetExplorerOptions) SafariDriver(org.openqa.selenium.safari.SafariDriver) FirefoxOptions(org.openqa.selenium.firefox.FirefoxOptions) EdgeDriver(org.openqa.selenium.edge.EdgeDriver) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver)

Example 65 with ChromeDriver

use of org.openqa.selenium.chrome.ChromeDriver in project ifml2php by Dipiert.

the class Page_test method loadChromeDriver.

private static void loadChromeDriver() {
    System.setProperty("webdriver.chrome.driver", "drivers/chromedriver");
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("--no-sandbox");
    drivers.add(new ChromeDriver(chromeOptions));
}
Also used : ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver)

Aggregations

ChromeDriver (org.openqa.selenium.chrome.ChromeDriver)91 ChromeOptions (org.openqa.selenium.chrome.ChromeOptions)41 DesiredCapabilities (org.openqa.selenium.remote.DesiredCapabilities)39 File (java.io.File)33 FirefoxDriver (org.openqa.selenium.firefox.FirefoxDriver)27 WebDriver (org.openqa.selenium.WebDriver)24 HashMap (java.util.HashMap)21 Before (org.junit.Before)13 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)13 URL (java.net.URL)11 FirefoxProfile (org.openqa.selenium.firefox.FirefoxProfile)11 InternetExplorerDriver (org.openqa.selenium.ie.InternetExplorerDriver)11 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)11 Test (org.junit.Test)10 ChromeDriverService (org.openqa.selenium.chrome.ChromeDriverService)9 Actions (org.openqa.selenium.interactions.Actions)9 PhantomJSDriver (org.openqa.selenium.phantomjs.PhantomJSDriver)9 HtmlUnitDriver (org.openqa.selenium.htmlunit.HtmlUnitDriver)8 JavascriptExecutor (org.openqa.selenium.JavascriptExecutor)7 SafariDriver (org.openqa.selenium.safari.SafariDriver)7