Search in sources :

Example 36 with ChromeOptions

use of org.openqa.selenium.chrome.ChromeOptions 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 37 with ChromeOptions

use of org.openqa.selenium.chrome.ChromeOptions in project cerberus-source by cerberustesting.

the class SeleniumServerService method setCapabilityBrowser.

/**
 * Instanciate DesiredCapabilities regarding the browser
 *
 * @param capabilities
 * @param browser
 * @param tCExecution
 * @return
 * @throws CerberusException
 */
private DesiredCapabilities setCapabilityBrowser(DesiredCapabilities capabilities, String browser, TestCaseExecution tCExecution) throws CerberusException {
    try {
        if (browser.equalsIgnoreCase("firefox")) {
            capabilities = DesiredCapabilities.firefox();
            FirefoxProfile profile = new FirefoxProfile();
            profile.setPreference("app.update.enabled", false);
            try {
                Invariant invariant = invariantService.convert(invariantService.readByKey("COUNTRY", tCExecution.getCountry()));
                if (invariant.getGp2() == null) {
                    LOG.warn("Country selected (" + tCExecution.getCountry() + ") has no value of GP2 in Invariant table, default language set to English (en)");
                    profile.setPreference("intl.accept_languages", "en");
                } else {
                    profile.setPreference("intl.accept_languages", invariant.getGp2());
                }
            } catch (CerberusException ex) {
                LOG.warn("Country selected (" + tCExecution.getCountry() + ") not in Invariant table, default language set to English (en)");
                profile.setPreference("intl.accept_languages", "en");
            }
            // Set UserAgent if testCaseUserAgent or robotUserAgent is defined
            String usedUserAgent = getUserAgentToUse(tCExecution.getTestCaseObj().getUserAgent(), tCExecution.getUserAgent());
            if (!StringUtil.isNullOrEmpty(usedUserAgent)) {
                profile.setPreference("general.useragent.override", usedUserAgent);
            }
            capabilities.setCapability(FirefoxDriver.PROFILE, profile);
        } else if (browser.equalsIgnoreCase("IE")) {
            capabilities = DesiredCapabilities.internetExplorer();
        } else if (browser.equalsIgnoreCase("chrome")) {
            capabilities = DesiredCapabilities.chrome();
            /**
             * Add custom capabilities
             */
            ChromeOptions options = new ChromeOptions();
            // Maximize windows for chrome browser
            options.addArguments("--start-fullscreen");
            // Set UserAgent if necessary
            String usedUserAgent = getUserAgentToUse(tCExecution.getTestCaseObj().getUserAgent(), tCExecution.getUserAgent());
            if (!StringUtil.isNullOrEmpty(usedUserAgent)) {
                options.addArguments("--user-agent=" + usedUserAgent);
            }
            capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        } else if (browser.contains("android")) {
            capabilities = DesiredCapabilities.android();
        } else if (browser.contains("ipad")) {
            capabilities = DesiredCapabilities.ipad();
        } else if (browser.contains("iphone")) {
            capabilities = DesiredCapabilities.iphone();
        } else if (browser.contains("safari")) {
            capabilities = DesiredCapabilities.safari();
        } else {
            LOG.warn("Not supported Browser : " + browser);
            MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.EXECUTION_FA_SELENIUM);
            mes.setDescription(mes.getDescription().replace("%MES%", "Browser '" + browser + "' is not supported"));
            mes.setDescription("Not supported Browser : " + browser);
            throw new CerberusException(mes);
        }
    } catch (CerberusException ex) {
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.EXECUTION_FA_SELENIUM);
        mes.setDescription(mes.getDescription().replace("%MES%", "Failed to set capability on the browser '" + browser + "' due to " + ex.getMessageError().getDescription()));
        throw new CerberusException(mes);
    }
    return capabilities;
}
Also used : Invariant(org.cerberus.crud.entity.Invariant) CerberusException(org.cerberus.exception.CerberusException) MessageGeneral(org.cerberus.engine.entity.MessageGeneral) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) FirefoxProfile(org.openqa.selenium.firefox.FirefoxProfile)

Example 38 with ChromeOptions

use of org.openqa.selenium.chrome.ChromeOptions 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 39 with ChromeOptions

use of org.openqa.selenium.chrome.ChromeOptions 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)

Example 40 with ChromeOptions

use of org.openqa.selenium.chrome.ChromeOptions in project cia by Hack23.

the class AbstractRoleSystemTest method getWebDriver.

/**
 * Gets the web driver.
 *
 * @return the web driver
 */
protected final synchronized WebDriver getWebDriver() {
    WebDriver driver = null;
    if ("firefox".equals(browser)) {
        final DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("marionette", true);
        driver = new FirefoxDriver(capabilities);
        driver.manage().window().maximize();
    } else if ("chrome".equals(browser)) {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--allow-insecure-localhost", "--start-maximized");
        driver = new ChromeDriver(chromeOptions);
    } else if ("htmlunit-firefox".equals(browser)) {
        final HtmlUnitDriver htmlUnitDriver = new HtmlUnitDriver(BrowserVersion.FIREFOX_45);
        htmlUnitDriver.setJavascriptEnabled(true);
        driver = htmlUnitDriver;
    } else if ("htmlunit-ie11".equals(browser)) {
        final HtmlUnitDriver htmlUnitDriver = new HtmlUnitDriver(BrowserVersion.INTERNET_EXPLORER);
        htmlUnitDriver.setJavascriptEnabled(true);
        driver = htmlUnitDriver;
    } else if ("htmlunit-edge".equals(browser)) {
        final HtmlUnitDriver htmlUnitDriver = new HtmlUnitDriver(BrowserVersion.EDGE);
        htmlUnitDriver.setJavascriptEnabled(true);
        driver = htmlUnitDriver;
    } else if ("htmlunit-chrome".equals(browser)) {
        final HtmlUnitDriver htmlUnitDriver = new HtmlUnitDriver(BrowserVersion.CHROME);
        htmlUnitDriver.setJavascriptEnabled(true);
        driver = htmlUnitDriver;
    } else {
        fail("No valid browser parameter:" + browser);
    }
    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
    webDriverMap.put(browser, driver);
    return driver;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver)

Aggregations

ChromeOptions (org.openqa.selenium.chrome.ChromeOptions)50 ChromeDriver (org.openqa.selenium.chrome.ChromeDriver)39 DesiredCapabilities (org.openqa.selenium.remote.DesiredCapabilities)28 File (java.io.File)20 HashMap (java.util.HashMap)19 FirefoxDriver (org.openqa.selenium.firefox.FirefoxDriver)16 FirefoxProfile (org.openqa.selenium.firefox.FirefoxProfile)10 HtmlUnitDriver (org.openqa.selenium.htmlunit.HtmlUnitDriver)8 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)8 WebDriver (org.openqa.selenium.WebDriver)7 Actions (org.openqa.selenium.interactions.Actions)7 InternetExplorerDriver (org.openqa.selenium.ie.InternetExplorerDriver)6 URL (java.net.URL)5 WebDriverException (org.openqa.selenium.WebDriverException)5 LoggingPreferences (org.openqa.selenium.logging.LoggingPreferences)5 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)5 ArrayList (java.util.ArrayList)4 BeforeClass (org.junit.BeforeClass)4 TakesScreenshot (org.openqa.selenium.TakesScreenshot)4 ChromeDriverService (org.openqa.selenium.chrome.ChromeDriverService)4