Search in sources :

Example 1 with ChromeOptions

use of org.openqa.selenium.chrome.ChromeOptions in project ats-framework by Axway.

the class AbstractRealBrowserDriver method start.

@Override
@PublicAtsApi
public void start() {
    try {
        log.info("Starting selenium browser with " + this.getClass().getSimpleName());
        if (browserType == BrowserType.FireFox) {
            com.axway.ats.uiengine.FirefoxDriver firefoxDriver = new com.axway.ats.uiengine.FirefoxDriver(url, browserPath, remoteSeleniumURL);
            FirefoxProfile profile = null;
            if (firefoxDriver.getProfileName() != null) {
                profile = new ProfilesIni().getProfile(firefoxDriver.getProfileName());
                if (profile == null) {
                    throw new SeleniumOperationException("Firefox profile '" + firefoxDriver.getProfileName() + "' doesn't exist");
                }
            } else if (firefoxDriver.getProfileDirectory() != null) {
                File profileDirectory = new File(firefoxDriver.getProfileDirectory());
                profile = new FirefoxProfile(profileDirectory);
            } else {
                profile = new FirefoxProfile();
                String downloadDir = UiEngineConfigurator.getInstance().getBrowserDownloadDir();
                // for default browser. Now will FIX this behavior
                if (downloadDir.endsWith("/") || downloadDir.endsWith("\\")) {
                    downloadDir = downloadDir.substring(0, downloadDir.length() - 1);
                }
                // Following options are described in http://kb.mozillazine.org/Firefox_:_FAQs_:_About:config_Entries
                profile.setPreference("browser.download.dir", downloadDir);
                profile.setPreference("browser.download.folderList", 2);
                profile.setPreference("browser.helperApps.neverAsk.saveToDisk", UiEngineConfigurator.getInstance().getBrowserDownloadMimeTypes());
                // set to  "Always Activate"
                profile.setPreference("plugin.state.java", 2);
                // set to  "Always Activate"
                profile.setPreference("plugin.state.flash", 2);
                profile.setAcceptUntrustedCertificates(true);
                profile.setEnableNativeEvents(true);
            }
            DesiredCapabilities capabilities = DesiredCapabilities.firefox();
            capabilities.setCapability(FirefoxDriver.PROFILE, profile);
            setFirefoxProxyIfAvailable(capabilities);
            if (this.browserPath != null) {
                capabilities.setCapability(FirefoxDriver.BINARY, this.browserPath);
            }
            if (this.remoteSeleniumURL != null) {
                webDriver = new RemoteWebDriver(new URL(this.remoteSeleniumURL), capabilities);
            } else {
                webDriver = new FirefoxDriver(capabilities);
            }
        } else if (browserType == BrowserType.InternetExplorer) {
            if (this.remoteSeleniumURL != null) {
                webDriver = new RemoteWebDriver(new URL(this.remoteSeleniumURL), DesiredCapabilities.internetExplorer());
            } else {
                webDriver = new org.openqa.selenium.ie.InternetExplorerDriver();
            }
        } else if (browserType == BrowserType.Edge) {
            if (this.remoteSeleniumURL != null) {
                webDriver = new RemoteWebDriver(new URL(this.remoteSeleniumURL), DesiredCapabilities.edge());
            } else {
                webDriver = new org.openqa.selenium.edge.EdgeDriver();
            }
        } else if (browserType == BrowserType.Chrome) {
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            // apply Chrome options
            ChromeOptions options = UiEngineConfigurator.getInstance().getChromeDriverOptions();
            if (options == null) {
                options = new ChromeOptions();
            }
            /* set browser download dir for Chrome Browser */
            String downloadDir = UiEngineConfigurator.getInstance().getBrowserDownloadDir();
            HashMap<String, Object> prefs = new HashMap<String, Object>();
            prefs.put("profile.default_content_settings.popups", 0);
            prefs.put("download.default_directory", downloadDir);
            options.setExperimentalOption("prefs", prefs);
            capabilities.setCapability(ChromeOptions.CAPABILITY, options);
            if (this.remoteSeleniumURL != null) {
                webDriver = new RemoteWebDriver(new URL(this.remoteSeleniumURL), capabilities);
            } else {
                webDriver = new org.openqa.selenium.chrome.ChromeDriver(capabilities);
            }
        } else if (browserType == BrowserType.Safari) {
            if (this.remoteSeleniumURL != null) {
                webDriver = new RemoteWebDriver(new URL(this.remoteSeleniumURL), DesiredCapabilities.safari());
            } else {
                webDriver = new org.openqa.selenium.safari.SafariDriver();
            }
        } else if (browserType == BrowserType.PhantomJS) {
            DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
            capabilities.setJavascriptEnabled(true);
            capabilities.setCapability("acceptSslCerts", true);
            capabilities.setCapability("browserConnectionEnabled", true);
            capabilities.setCapability("takesScreenshot", true);
            // See: https://github.com/ariya/phantomjs/wiki/API-Reference-WebPage#settings-object
            if (System.getProperty(PhantomJsDriver.SETTINGS_PROPERTY) != null) {
                Map<String, String> settings = extractPhantomJSCapabilityValues(System.getProperty(PhantomJsDriver.SETTINGS_PROPERTY));
                for (Entry<String, String> capability : settings.entrySet()) {
                    capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + capability.getKey(), capability.getValue());
                }
            }
            // See:  https://github.com/ariya/phantomjs/wiki/API-Reference-WebPage#wiki-webpage-customHeaders
            if (System.getProperty(PhantomJsDriver.CUSTOM_HEADERS_PROPERTY) != null) {
                Map<String, String> customHeaders = extractPhantomJSCapabilityValues(System.getProperty(PhantomJsDriver.CUSTOM_HEADERS_PROPERTY));
                for (Entry<String, String> header : customHeaders.entrySet()) {
                    capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + header.getKey(), header.getValue());
                }
            }
            if (this.browserPath != null) {
                capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, this.browserPath);
                System.setProperty(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, // required from the create screenshot method
                this.browserPath);
            }
            // See:  https://github.com/ariya/phantomjs/wiki/API-Reference#command-line-options
            List<String> cliArgsCapabilities = new ArrayList<String>();
            cliArgsCapabilities.add("--web-security=false");
            cliArgsCapabilities.add("--ignore-ssl-errors=true");
            if (System.getProperty(PhantomJsDriver.SSL_PROTOCOL_PROPERTY) != null) {
                cliArgsCapabilities.add("--ssl-protocol=" + System.getProperty(PhantomJsDriver.SSL_PROTOCOL_PROPERTY));
            } else {
                cliArgsCapabilities.add("--ssl-protocol=any");
            }
            if (System.getProperty(PhantomJsDriver.HTTP_ONLY_COOKIES_PROPERTY) != null) {
                cliArgsCapabilities.add("--cookies-file=" + PhantomJsDriver.cookiesFile);
            }
            // cliArgsCapabilities.add( "--local-to-remote-url-access=true" );
            setPhantomJSProxyIfAvailable(cliArgsCapabilities);
            capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCapabilities);
            if (this.remoteSeleniumURL != null) {
                webDriver = new RemoteWebDriver(new URL(this.remoteSeleniumURL), capabilities);
            } else {
                webDriver = new org.openqa.selenium.phantomjs.PhantomJSDriver(capabilities);
            }
        }
        log.info("Openning URL: " + url);
        webDriver.get(url);
        if (this instanceof com.axway.ats.uiengine.PhantomJsDriver) {
            webDriver.manage().window().setSize(new Dimension(1280, 1024));
        } else if (!(this instanceof com.axway.ats.uiengine.EdgeDriver)) {
            webDriver.manage().window().maximize();
        }
        int browserActionTimeout = UiEngineConfigurator.getInstance().getBrowserActionTimeout();
        if (browserActionTimeout > 0) {
            webDriver.manage().timeouts().setScriptTimeout(browserActionTimeout, TimeUnit.SECONDS);
        }
        if (!(this instanceof com.axway.ats.uiengine.EdgeDriver)) {
            webDriver.manage().timeouts().pageLoadTimeout(browserActionTimeout, TimeUnit.SECONDS);
        }
        // waiting for the "body" element to be loaded
        waitForPageLoaded(webDriver, UiEngineConfigurator.getInstance().getWaitPageToLoadTimeout());
    } catch (Exception e) {
        throw new SeleniumOperationException("Error starting Selenium", e);
    }
}
Also used : HashMap(java.util.HashMap) FirefoxProfile(org.openqa.selenium.firefox.FirefoxProfile) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) URL(java.net.URL) Entry(java.util.Map.Entry) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) Dimension(org.openqa.selenium.Dimension) VerificationException(com.axway.ats.uiengine.exceptions.VerificationException) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) ElementNotFoundException(com.axway.ats.uiengine.exceptions.ElementNotFoundException) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) ProfilesIni(org.openqa.selenium.firefox.internal.ProfilesIni) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 2 with ChromeOptions

use of org.openqa.selenium.chrome.ChromeOptions in project jlineup by otto-de.

the class BrowserUtils method getWebDriverByConfig.

synchronized WebDriver getWebDriverByConfig(Config config, int width) {
    WebDriver driver;
    switch(config.browser) {
        case FIREFOX:
            FirefoxDriverManager.getInstance().setup();
            FirefoxOptions firefoxOptions = new FirefoxOptions();
            firefoxOptions.setProfile(getFirefoxProfileWithDisabledAnimatedGifs());
            driver = new FirefoxDriver(firefoxOptions);
            break;
        case FIREFOX_HEADLESS:
            FirefoxDriverManager.getInstance().setup();
            FirefoxOptions firefoxOptionsForHeadless = new FirefoxOptions();
            // Headless parameter is supported with Firefox >= 55
            firefoxOptionsForHeadless.addArguments("--headless");
            firefoxOptionsForHeadless.addArguments("-width", width + "", "-height", config.windowHeight + "");
            firefoxOptionsForHeadless.setProfile(getFirefoxProfileWithDisabledAnimatedGifs());
            driver = new FirefoxDriver(firefoxOptionsForHeadless);
            break;
        case CHROME:
            ChromeDriverManager.getInstance().setup();
            ChromeOptions options = new ChromeOptions();
            // To work in a headless env, this is needed
            options.addArguments("--no-sandbox");
            driver = new ChromeDriver(options);
            break;
        case CHROME_HEADLESS:
            ChromeDriverManager.getInstance().setup();
            ChromeOptions options_headless = new ChromeOptions();
            // To work in a headless env, this is needed
            options_headless.addArguments("--no-sandbox", "--headless", "--disable-gpu");
            options_headless.addArguments("--window-size=" + width + "," + config.windowHeight);
            driver = new ChromeDriver(options_headless);
            break;
        case PHANTOMJS:
        default:
            PhantomJsDriverManager.getInstance().setup();
            driver = new PhantomJSDriver();
            break;
    }
    driver.manage().timeouts().pageLoadTimeout(config.pageLoadTimeout, TimeUnit.SECONDS);
    return driver;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) FirefoxOptions(org.openqa.selenium.firefox.FirefoxOptions) PhantomJSDriver(org.openqa.selenium.phantomjs.PhantomJSDriver) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver)

Example 3 with ChromeOptions

use of org.openqa.selenium.chrome.ChromeOptions in project activityinfo by bedatadriven.

the class ChromeWebDriverProvider method initPool.

private static WebDriverPool initPool() {
    WebDriverPool pool = new WebDriverPool();
    // pool.setMaxTotalSize(3);
    pool.setCreator(new Function<BrowserProfile, WebDriver>() {

        @Override
        public WebDriver apply(BrowserProfile input) {
            // Start a local http proxy that we can use to control the
            // the connection's properties
            // ProxyController proxyController = new ProxyController();
            // proxyController.start();
            Map<String, String> environment = Maps.newHashMap();
            if (!Strings.isNullOrEmpty(System.getProperty("browserTimezone"))) {
                environment.put("TZ", System.getProperty("browserTimezone"));
            }
            ChromeDriverService service = new ChromeDriverService.Builder().usingAnyFreePort().usingDriverExecutable(findDriverBin()).withEnvironment(environment).build();
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--verbose");
            // 
            DesiredCapabilities capabilities = new DesiredCapabilities();
            // capabilities.setCapability(CapabilityType.PROXY, proxyController.getWebDriverProxy());
            capabilities.setCapability(ChromeOptions.CAPABILITY, options);
            // return new ProxiedWebDriver(new ChromeDriver(capabilities), proxyController);
            return new ChromeDriver(service, capabilities);
        }
    });
    return pool;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) ChromeDriverService(org.openqa.selenium.chrome.ChromeDriverService) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) Map(java.util.Map)

Example 4 with ChromeOptions

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

the class Form_test method loadChromeDriver.

private 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 5 with ChromeOptions

use of org.openqa.selenium.chrome.ChromeOptions in project NoraUi by NoraUi.

the class DriverFactory method generateGoogleChromeDriver.

/**
 * Generates a chrome webdriver.
 *
 * @return
 *         A chrome webdriver
 * @throws TechnicalException
 *             if an error occured when Webdriver setExecutable to true.
 */
private WebDriver generateGoogleChromeDriver() throws TechnicalException {
    final String pathWebdriver = DriverFactory.getPath(Driver.CHROME);
    if (!new File(pathWebdriver).setExecutable(true)) {
        throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE_WEBDRIVER_SET_EXECUTABLE));
    }
    logger.info("Generating Chrome driver ({}) ...", pathWebdriver);
    System.setProperty(Driver.CHROME.getDriverName(), pathWebdriver);
    final ChromeOptions chromeOptions = new ChromeOptions();
    final DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
    capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
    setLoggingLevel(capabilities);
    if (Context.isHeadless()) {
        chromeOptions.addArguments("--headless");
    }
    // Proxy configuration
    if (Context.getProxy().getProxyType() != ProxyType.UNSPECIFIED && Context.getProxy().getProxyType() != ProxyType.AUTODETECT) {
        capabilities.setCapability(CapabilityType.PROXY, Context.getProxy());
    }
    setChromeOptions(capabilities, chromeOptions);
    final String withWhitelistedIps = Context.getWebdriversProperties("withWhitelistedIps");
    if (withWhitelistedIps != null && !"".equals(withWhitelistedIps)) {
        final ChromeDriverService service = new ChromeDriverService.Builder().withWhitelistedIps(withWhitelistedIps).withVerbose(false).build();
        return new ChromeDriver(service, capabilities);
    } else {
        return new ChromeDriver(capabilities);
    }
}
Also used : TechnicalException(com.github.noraui.exception.TechnicalException) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) ChromeDriverService(org.openqa.selenium.chrome.ChromeDriverService) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) File(java.io.File)

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