Search in sources :

Example 6 with RemoteWebDriver

use of org.openqa.selenium.remote.RemoteWebDriver 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 7 with RemoteWebDriver

use of org.openqa.selenium.remote.RemoteWebDriver in project webmagic by code4craft.

the class WebDriverPool method configure.

/**
	 * Configure the GhostDriver, and initialize a WebDriver instance. This part
	 * of code comes from GhostDriver.
	 * https://github.com/detro/ghostdriver/tree/master/test/java/src/test/java/ghostdriver
	 * 
	 * @author bob.li.0718@gmail.com
	 * @throws IOException
	 */
public void configure() throws IOException {
    // Read config file
    sConfig = new Properties();
    String configFile = DEFAULT_CONFIG_FILE;
    if (System.getProperty("selenuim_config") != null) {
        configFile = System.getProperty("selenuim_config");
    }
    sConfig.load(new FileReader(configFile));
    // Prepare capabilities
    sCaps = new DesiredCapabilities();
    sCaps.setJavascriptEnabled(true);
    sCaps.setCapability("takesScreenshot", false);
    String driver = sConfig.getProperty("driver", DRIVER_PHANTOMJS);
    // Fetch PhantomJS-specific configuration parameters
    if (driver.equals(DRIVER_PHANTOMJS)) {
        // "phantomjs_exec_path"
        if (sConfig.getProperty("phantomjs_exec_path") != null) {
            sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, sConfig.getProperty("phantomjs_exec_path"));
        } else {
            throw new IOException(String.format("Property '%s' not set!", PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY));
        }
        // "phantomjs_driver_path"
        if (sConfig.getProperty("phantomjs_driver_path") != null) {
            System.out.println("Test will use an external GhostDriver");
            sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY, sConfig.getProperty("phantomjs_driver_path"));
        } else {
            System.out.println("Test will use PhantomJS internal GhostDriver");
        }
    }
    // Disable "web-security", enable all possible "ssl-protocols" and
    // "ignore-ssl-errors" for PhantomJSDriver
    // sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new
    // String[] {
    // "--web-security=false",
    // "--ssl-protocol=any",
    // "--ignore-ssl-errors=true"
    // });
    ArrayList<String> cliArgsCap = new ArrayList<String>();
    cliArgsCap.add("--web-security=false");
    cliArgsCap.add("--ssl-protocol=any");
    cliArgsCap.add("--ignore-ssl-errors=true");
    sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
    // Control LogLevel for GhostDriver, via CLI arguments
    sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS, new String[] { "--logLevel=" + (sConfig.getProperty("phantomjs_driver_loglevel") != null ? sConfig.getProperty("phantomjs_driver_loglevel") : "INFO") });
    // Start appropriate Driver
    if (isUrl(driver)) {
        sCaps.setBrowserName("phantomjs");
        mDriver = new RemoteWebDriver(new URL(driver), sCaps);
    } else if (driver.equals(DRIVER_FIREFOX)) {
        mDriver = new FirefoxDriver(sCaps);
    } else if (driver.equals(DRIVER_CHROME)) {
        mDriver = new ChromeDriver(sCaps);
    } else if (driver.equals(DRIVER_PHANTOMJS)) {
        mDriver = new PhantomJSDriver(sCaps);
    }
}
Also used : PhantomJSDriver(org.openqa.selenium.phantomjs.PhantomJSDriver) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) ArrayList(java.util.ArrayList) FileReader(java.io.FileReader) IOException(java.io.IOException) Properties(java.util.Properties) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) URL(java.net.URL)

Aggregations

RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)7 URL (java.net.URL)5 FirefoxDriver (org.openqa.selenium.firefox.FirefoxDriver)4 Dimension (org.openqa.selenium.Dimension)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 ChromeDriver (org.openqa.selenium.chrome.ChromeDriver)2 PhantomJSDriver (org.openqa.selenium.phantomjs.PhantomJSDriver)2 DesiredCapabilities (org.openqa.selenium.remote.DesiredCapabilities)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 ElementNotFoundException (com.axway.ats.uiengine.exceptions.ElementNotFoundException)1 SeleniumOperationException (com.axway.ats.uiengine.exceptions.SeleniumOperationException)1 VerificationException (com.axway.ats.uiengine.exceptions.VerificationException)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1