Example usage for org.openqa.selenium.remote RemoteWebDriver RemoteWebDriver

List of usage examples for org.openqa.selenium.remote RemoteWebDriver RemoteWebDriver

Introduction

In this page you can find the example usage for org.openqa.selenium.remote RemoteWebDriver RemoteWebDriver.

Prototype

public RemoteWebDriver(URL remoteAddress, Capabilities capabilities) 

Source Link

Usage

From source file:org.aludratest.service.gui.web.selenium.selenium2.Selenium2Driver.java

License:Apache License

/** @param url the URL for which to create a WebDriver instance.
 * @param arguments Additional arguments to include for the WebDriver instance, or an empty array.
 * @return a freshly created instance of the related WebDriver class */
public WebDriver newRemoteDriver(URL url, String[] arguments) {
    AludraSeleniumHttpCommandExecutor executor = new AludraSeleniumHttpCommandExecutor(url);

    DesiredCapabilities caps = capabilities;
    if (arguments != null && arguments.length > 0) {
        caps = new DesiredCapabilities(capabilities);
        // this looks strange, but is the only way to avoid having all Threads sharing the same ChromeOptions object
        ChromeOptions opts = (ChromeOptions) createChromeCaps().getCapability(ChromeOptions.CAPABILITY);
        if (opts != null) {
            opts.addArguments(arguments);
            caps.setCapability(ChromeOptions.CAPABILITY, opts);
        }// w  w w . jav a2s .  c  o m
    }

    try {
        RemoteWebDriver driver = new RemoteWebDriver(executor, caps);
        driver.setFileDetector(new LocalFileDetector());
        return driver;
    } catch (WebDriverException e) {
        LoggerFactory.getLogger(Selenium2Driver.class).error(
                "Could not create remote web driver. Last remote HTTP response: " + executor.getLastResponse());
        throw e;
    }
}

From source file:org.apache.archiva.web.test.tools.WebdriverUtility.java

License:Apache License

public static WebDriver newWebDriver(String seleniumBrowser, String seleniumHost, int seleniumPort,
        boolean seleniumRemote) {
    log.info("WebDriver {}, {}, {}, {}", seleniumBrowser, seleniumHost, seleniumPort, seleniumRemote);
    if (seleniumRemote && StringUtils.isEmpty(seleniumHost)) {
        throw new IllegalArgumentException("seleniumHost must be set, when seleniumRemote=true");
    }/*ww  w  .  j a va  2s.c  om*/
    try {

        if (StringUtils.contains(seleniumBrowser, "chrome")) {
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            if (seleniumRemote) {
                DesiredCapabilities capabilities = DesiredCapabilities.chrome();
                capabilities.setCapability(ChromeOptions.CAPABILITY, options);
                return new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"),
                        capabilities);
            } else {
                return new ChromeDriver(options);
            }
        }

        if (StringUtils.contains(seleniumBrowser, "safari")) {
            if (seleniumRemote) {
                return new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"),
                        DesiredCapabilities.safari());
            } else {
                return new SafariDriver();
            }
        }

        if (StringUtils.contains(seleniumBrowser, "iexplore")) {
            if (seleniumRemote) {
                return new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"),
                        DesiredCapabilities.internetExplorer());
            } else {
                new InternetExplorerDriver();
            }
        }

        if (StringUtils.contains(seleniumBrowser, "firefox")) {
            if (seleniumRemote) {
                return new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"),
                        DesiredCapabilities.firefox());
            } else {
                return new FirefoxDriver();
            }
        }

        DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
        capabilities.setJavascriptEnabled(true);
        capabilities.setVersion("firefox-52");
        WebDriver driver;
        if (seleniumRemote) {
            driver = new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"),
                    capabilities);
        } else {
            driver = new HtmlUnitDriver(capabilities) {
                @Override
                protected WebClient modifyWebClient(WebClient client) {
                    client.getOptions().setThrowExceptionOnFailingStatusCode(false);
                    client.getOptions().setThrowExceptionOnScriptError(false);
                    client.getOptions().setCssEnabled(true);
                    return client;
                }
            };

        }
        return driver;

    } catch (MalformedURLException e) {
        throw new RuntimeException("Initializion of remote driver failed");
    }

}

From source file:org.apache.nutch.protocol.selenium.HttpWebClient.java

License:Apache License

public static RemoteWebDriver createFirefoxRemoteWebDriver(URL seleniumHubUrl, boolean enableHeadlessMode) {
    FirefoxOptions firefoxOptions = new FirefoxOptions();
    if (enableHeadlessMode) {
        firefoxOptions.setHeadless(true);
    }/* w  ww  .  j a  va 2 s  . c o  m*/
    RemoteWebDriver driver = new RemoteWebDriver(seleniumHubUrl, firefoxOptions);
    return driver;
}

From source file:org.apache.nutch.protocol.selenium.HttpWebClient.java

License:Apache License

public static RemoteWebDriver createChromeRemoteWebDriver(URL seleniumHubUrl, boolean enableHeadlessMode) {
    ChromeOptions chromeOptions = new ChromeOptions();
    if (enableHeadlessMode) {
        chromeOptions.setHeadless(true);
    }/*from  w w  w  . ja  v a2s .  c  o m*/
    RemoteWebDriver driver = new RemoteWebDriver(seleniumHubUrl, chromeOptions);
    return driver;
}

From source file:org.atteo.moonshine.webdriver.browsers.ChromeBrowser.java

License:Apache License

@Override
public RemoteWebDriver createDriver() {
    return new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome());
}

From source file:org.cerberus.engine.execution.impl.SeleniumServerService.java

License:Open Source License

@Override
public void startServer(TestCaseExecution tCExecution) throws CerberusException {
    //message used for log purposes 
    String logPrefix = "[" + tCExecution.getTest() + " - " + tCExecution.getTestCase() + "] ";

    try {/*ww w  . j  av  a2  s .c  o m*/

        LOG.info(logPrefix + "Start Selenium Server");

        /**
         * Set Session
         */
        LOG.debug(logPrefix + "Setting the session.");
        String system = tCExecution.getApplicationObj().getSystem();

        /**
         * Get the parameters that will be used to set the servers
         * (selenium/appium) If timeout has been defined at the execution
         * level, set the selenium & appium wait element with this value,
         * else, take the one from parameter
         */
        Integer cerberus_selenium_pageLoadTimeout, cerberus_selenium_implicitlyWait,
                cerberus_selenium_setScriptTimeout, cerberus_selenium_wait_element,
                cerberus_appium_wait_element;

        if (!tCExecution.getTimeout().isEmpty()) {
            cerberus_selenium_wait_element = Integer.valueOf(tCExecution.getTimeout());
            cerberus_appium_wait_element = Integer.valueOf(tCExecution.getTimeout());
        } else {
            cerberus_selenium_wait_element = this.getTimeoutSetInParameterTable(system,
                    "cerberus_selenium_wait_element", 90000, logPrefix);
            cerberus_appium_wait_element = this.getTimeoutSetInParameterTable(system,
                    "cerberus_appium_wait_element", 90000, logPrefix);
            ;
        }
        cerberus_selenium_pageLoadTimeout = this.getTimeoutSetInParameterTable(system,
                "cerberus_selenium_pageLoadTimeout", 90000, logPrefix);
        cerberus_selenium_implicitlyWait = this.getTimeoutSetInParameterTable(system,
                "cerberus_selenium_implicitlyWait", 0, logPrefix);
        cerberus_selenium_setScriptTimeout = this.getTimeoutSetInParameterTable(system,
                "cerberus_selenium_setScriptTimeout", 90000, logPrefix);

        LOG.debug(logPrefix + "TimeOut defined on session : " + cerberus_selenium_wait_element);

        Session session = new Session();
        session.setCerberus_selenium_implicitlyWait(cerberus_selenium_implicitlyWait);
        session.setCerberus_selenium_pageLoadTimeout(cerberus_selenium_pageLoadTimeout);
        session.setCerberus_selenium_setScriptTimeout(cerberus_selenium_setScriptTimeout);
        session.setCerberus_selenium_wait_element(cerberus_selenium_wait_element);
        session.setCerberus_appium_wait_element(cerberus_appium_wait_element);
        session.setHost(tCExecution.getSeleniumIP());
        session.setPort(tCExecution.getPort());
        tCExecution.setSession(session);
        LOG.debug(logPrefix + "Session is set.");

        /**
         * SetUp Capabilities
         */
        LOG.debug(logPrefix + "Set Capabilities");
        DesiredCapabilities caps = this.setCapabilities(tCExecution);
        session.setDesiredCapabilities(caps);
        LOG.debug(logPrefix + "Set Capabilities - retreived");

        /**
         * SetUp Driver
         */
        LOG.debug(logPrefix + "Set Driver");
        WebDriver driver = null;
        AppiumDriver appiumDriver = null;
        if (tCExecution.getApplicationObj().getType().equalsIgnoreCase("GUI")) {
            if (caps.getPlatform().is(Platform.ANDROID)) {
                appiumDriver = new AndroidDriver(new URL("http://" + tCExecution.getSession().getHost() + ":"
                        + tCExecution.getSession().getPort() + "/wd/hub"), caps);
                driver = (WebDriver) appiumDriver;
            } else if (caps.getPlatform().is(Platform.MAC)) {
                appiumDriver = new IOSDriver(new URL("http://" + tCExecution.getSession().getHost() + ":"
                        + tCExecution.getSession().getPort() + "/wd/hub"), caps);
                driver = (WebDriver) appiumDriver;
            } else {
                driver = new RemoteWebDriver(new URL("http://" + tCExecution.getSession().getHost() + ":"
                        + tCExecution.getSession().getPort() + "/wd/hub"), caps);
            }
        } else if (tCExecution.getApplicationObj().getType().equalsIgnoreCase("APK")) {
            appiumDriver = new AndroidDriver(new URL("http://" + tCExecution.getSession().getHost() + ":"
                    + tCExecution.getSession().getPort() + "/wd/hub"), caps);
            driver = (WebDriver) appiumDriver;
        } else if (tCExecution.getApplicationObj().getType().equalsIgnoreCase("IPA")) {
            appiumDriver = new IOSDriver(new URL("http://" + tCExecution.getSession().getHost() + ":"
                    + tCExecution.getSession().getPort() + "/wd/hub"), caps);
            driver = (WebDriver) appiumDriver;
        } else if (tCExecution.getApplicationObj().getType().equalsIgnoreCase("FAT")) {
            sikuliService.doSikuliAction(session, "openApp", null,
                    tCExecution.getCountryEnvironmentParameters().getIp());
        }

        /**
         * Defining the timeout at the driver level. Only in case of not
         * Appium Driver (see
         * https://github.com/vertigo17/Cerberus/issues/754)
         */
        if (driver != null && appiumDriver == null) {
            driver.manage().timeouts().pageLoadTimeout(cerberus_selenium_pageLoadTimeout,
                    TimeUnit.MILLISECONDS);
            driver.manage().timeouts().implicitlyWait(cerberus_selenium_implicitlyWait, TimeUnit.MILLISECONDS);
            driver.manage().timeouts().setScriptTimeout(cerberus_selenium_setScriptTimeout,
                    TimeUnit.MILLISECONDS);
        }
        tCExecution.getSession().setDriver(driver);
        tCExecution.getSession().setAppiumDriver(appiumDriver);

        /**
         * If Gui application, maximize window Get IP of Node in case of
         * remote Server
         */
        if (tCExecution.getApplicationObj().getType().equalsIgnoreCase("GUI")
                && !caps.getPlatform().equals(Platform.ANDROID)) {
            driver.manage().window().maximize();
            getIPOfNode(tCExecution);

            /**
             * If screenSize is defined, set the size of the screen.
             */
            if (!tCExecution.getScreenSize().equals("")) {
                Integer screenWidth = Integer.valueOf(tCExecution.getScreenSize().split("\\*")[0]);
                Integer screenLength = Integer.valueOf(tCExecution.getScreenSize().split("\\*")[1]);
                setScreenSize(driver, screenWidth, screenLength);
            }
            tCExecution.setScreenSize(getScreenSize(driver));
        }
        tCExecution.getSession().setStarted(true);

    } catch (CerberusException exception) {
        LOG.error(logPrefix + exception.toString());
        throw new CerberusException(exception.getMessageError());
    } catch (MalformedURLException exception) {
        LOG.error(logPrefix + exception.toString());
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_URL_MALFORMED);
        mes.setDescription(mes.getDescription().replace("%URL%",
                tCExecution.getSession().getHost() + ":" + tCExecution.getSession().getPort()));
        throw new CerberusException(mes);
    } catch (UnreachableBrowserException exception) {
        LOG.error(logPrefix + exception.toString());
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_SELENIUM_COULDNOTCONNECT);
        mes.setDescription(mes.getDescription().replace("%SSIP%", tCExecution.getSeleniumIP()));
        mes.setDescription(mes.getDescription().replace("%SSPORT%", tCExecution.getSeleniumPort()));
        throw new CerberusException(mes);
    } catch (Exception exception) {
        LOG.error(logPrefix + exception.toString());
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.EXECUTION_FA_SELENIUM);
        mes.setDescription(mes.getDescription().replace("%MES%", exception.toString()));
        throw new CerberusException(mes);
    }
}

From source file:org.cerberus.service.engine.impl.SeleniumServerService.java

License:Open Source License

@Override
public void startServer(TestCaseExecution tCExecution) throws CerberusException {
    //message used for log purposes 
    String testCaseDescription = "[" + tCExecution.getTest() + " - " + tCExecution.getTestCase() + "]";
    try {/*from w ww.  j a  v  a 2 s. co  m*/

        /**
         * SetUp Capabilities
         */
        MyLogger.log(SeleniumServerService.class.getName(), Level.DEBUG,
                testCaseDescription + "Set Capabilities");
        DesiredCapabilities caps = this.setCapabilities(tCExecution);

        /**
         * SetUp Driver
         */
        MyLogger.log(SeleniumServerService.class.getName(), Level.DEBUG, testCaseDescription + "Set Driver");
        WebDriver driver = null;
        AppiumDriver appiumDriver = null;
        if (tCExecution.getApplication().getType().equalsIgnoreCase("GUI")) {
            if (caps.getPlatform().is(Platform.ANDROID)) {

                appiumDriver = new AppiumDriver(new URL("http://" + tCExecution.getSession().getHost() + ":"
                        + tCExecution.getSession().getPort() + "/wd/hub"), caps);
                driver = (WebDriver) appiumDriver;
            } else {
                driver = new RemoteWebDriver(new URL("http://" + tCExecution.getSession().getHost() + ":"
                        + tCExecution.getSession().getPort() + "/wd/hub"), caps);
            }
        } else if (tCExecution.getApplication().getType().equalsIgnoreCase("APK")) {
            appiumDriver = new AppiumDriver(new URL("http://" + tCExecution.getSession().getHost() + ":"
                    + tCExecution.getSession().getPort() + "/wd/hub"), caps);
            driver = (WebDriver) appiumDriver;
        } else if (tCExecution.getApplication().getType().equalsIgnoreCase("IPA")) {
            appiumDriver = new AppiumDriver(new URL("http://" + tCExecution.getSession().getHost() + ":"
                    + tCExecution.getSession().getPort() + "/wd/hub"), caps);
            driver = (WebDriver) appiumDriver;
        }

        tCExecution.getSession().setDriver(driver);
        tCExecution.getSession().setAppiumDriver(appiumDriver);

        /**
         * If Gui application, maximize window Get IP of Node in case of
         * remote Server
         */
        if (tCExecution.getApplication().getType().equalsIgnoreCase("GUI")
                && !caps.getPlatform().equals(Platform.ANDROID)) {
            driver.manage().window().maximize();
            getIPOfNode(tCExecution);

            /**
             * If screenSize is defined, set the size of the screen.
             */
            if (!tCExecution.getScreenSize().equals("")) {
                Integer screenWidth = Integer.valueOf(tCExecution.getScreenSize().split("\\*")[0]);
                Integer screenLength = Integer.valueOf(tCExecution.getScreenSize().split("\\*")[1]);
                setScreenSize(driver, screenWidth, screenLength);
            }
            tCExecution.setScreenSize(getScreenSize(driver));
        }
        tCExecution.getSession().setStarted(true);

    } catch (CerberusException exception) {
        MyLogger.log(Selenium.class.getName(), Level.ERROR, exception.toString());
        throw new CerberusException(exception.getMessageError());
    } catch (MalformedURLException exception) {
        MyLogger.log(Selenium.class.getName(), Level.ERROR, exception.toString());
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_URL_MALFORMED);
        mes.setDescription(mes.getDescription().replace("%URL%",
                tCExecution.getSession().getHost() + ":" + tCExecution.getSession().getPort()));
        throw new CerberusException(mes);
    } catch (UnreachableBrowserException exception) {
        MyLogger.log(Selenium.class.getName(), Level.ERROR, exception.toString());
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_SELENIUM_COULDNOTCONNECT);
        mes.setDescription(mes.getDescription().replace("%SSIP%", tCExecution.getSeleniumIP()));
        mes.setDescription(mes.getDescription().replace("%SSPORT%", tCExecution.getSeleniumPort()));
        throw new CerberusException(mes);
    } catch (Exception exception) {
        MyLogger.log(Selenium.class.getName(), Level.ERROR, exception.toString());
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.EXECUTION_FA_SELENIUM);
        mes.setDescription(mes.getDescription().replace("%MES%", exception.toString()));
        throw new CerberusException(mes);
    }
}

From source file:org.cerberus.serviceEngine.impl.SeleniumServerService.java

License:Open Source License

@Override
public void startServer(TestCaseExecution tCExecution) throws CerberusException {

    try {//from   w  w w  .j  a v  a 2 s .  c om

        /**
         * SetUp Capabilities
         */
        MyLogger.log(SeleniumServerService.class.getName(), Level.DEBUG, "Set Capabilities");
        DesiredCapabilities caps = this.setCapabilities(tCExecution);

        /**
         * SetUp Driver
         */
        MyLogger.log(SeleniumServerService.class.getName(), Level.DEBUG, "Set Driver");
        WebDriver driver = new RemoteWebDriver(new URL("http://" + tCExecution.getSession().getHost() + ":"
                + tCExecution.getSession().getPort() + "/wd/hub"), caps);
        tCExecution.getSession().setDriver(driver);

        /**
         * If Gui application, maximize window Get IP of Node in case of
         * remote Server
         */
        if (tCExecution.getApplication().getType().equalsIgnoreCase("GUI")) {
            driver.manage().window().maximize();
            getIPOfNode(tCExecution);
        }
        tCExecution.getSession().setStarted(true);

    } catch (CerberusException exception) {
        MyLogger.log(Selenium.class.getName(), Level.ERROR, exception.toString());
        throw new CerberusException(exception.getMessageError());
    } catch (MalformedURLException exception) {
        MyLogger.log(Selenium.class.getName(), Level.ERROR, exception.toString());
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_URL_MALFORMED);
        mes.setDescription(mes.getDescription().replace("%URL%",
                tCExecution.getSession().getHost() + ":" + tCExecution.getSession().getPort()));
        throw new CerberusException(mes);
    } catch (UnreachableBrowserException exception) {
        MyLogger.log(Selenium.class.getName(), Level.ERROR, exception.toString());
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_SELENIUM_COULDNOTCONNECT);
        mes.setDescription(mes.getDescription().replace("%SSIP%", tCExecution.getSeleniumIP()));
        mes.setDescription(mes.getDescription().replace("%SSPORT%", tCExecution.getSeleniumPort()));
        throw new CerberusException(mes);
    } catch (Exception exception) {
        MyLogger.log(Selenium.class.getName(), Level.ERROR, exception.toString());
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.EXECUTION_FA_SELENIUM);
        mes.setDescription(mes.getDescription().replace("%MES%", exception.toString()));
        throw new CerberusException(mes);
    }
}

From source file:org.cerberus.serviceEngine.impl.SeleniumService.java

License:Open Source License

@Override
public boolean startSeleniumBrowser(TestCaseExecution tCExecution, boolean record, String country,
        String browser, String version, String platform) throws CerberusException {

    MyLogger.log(SeleniumService.class.getName(), Level.DEBUG, "Starting " + browser);

    DesiredCapabilities capabilities = null;

    //TODO : take platform and version from servlet
    try {//w  w  w .j a  v  a 2 s  . co m
        Selenium selenium = tCExecution.getSelenium();
        capabilities = setCapabilityBrowser(capabilities, browser, tCExecution.getExecutionUUID(), record,
                country);
        capabilities = setCapabilityPlatform(capabilities, platform);
        //capabilities = setCapabilityVersion(capabilities, version);

        MyLogger.log(SeleniumService.class.getName(), Level.DEBUG, "Set Driver");
        WebDriver driver = new RemoteWebDriver(
                new URL("http://" + selenium.getHost() + ":" + selenium.getPort() + "/wd/hub"), capabilities);
        selenium.setDriver(driver);
        tCExecution.setSelenium(selenium);
        //MyLogger.log(SeleniumService.class.getName(), Level.ERROR, driver.manage().logs().get(LogType.SERVER).toString());
    } catch (CerberusException exception) {
        MyLogger.log(Selenium.class.getName(), Level.ERROR, exception.toString());
        throw new CerberusException(exception.getMessageError());
    } catch (MalformedURLException exception) {
        MyLogger.log(Selenium.class.getName(), Level.ERROR, exception.toString());
        return false;
    } catch (UnreachableBrowserException exception) {
        MyLogger.log(Selenium.class.getName(), Level.ERROR, exception.toString());
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.EXECUTION_FA_SELENIUM);
        mes.setDescription(mes.getDescription().replace("%MES%", exception.getMessage()));
        throw new CerberusException(mes);
    } catch (Exception exception) {
        MyLogger.log(Selenium.class.getName(), Level.ERROR, exception.toString());
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.EXECUTION_FA_SELENIUM);
        mes.setDescription(mes.getDescription().replace("%MES%", exception.toString()));
        throw new CerberusException(mes);
    }

    return true;
}

From source file:org.codelibs.fess.crawler.client.http.webdriver.CrawlerWebDriver.java

License:Apache License

public void chrome() {
    if (capabilities == null) {
        capabilities = DesiredCapabilities.chrome();
    }/*  ww w .j a v  a  2s. co m*/
    if (capabilities instanceof DesiredCapabilities) {
        if (webdriverChromeDriver != null) {
            ((DesiredCapabilities) capabilities).setCapability("webdriver.chrome.driver",
                    webdriverChromeDriver);
        }
    }
    webDriver = new RemoteWebDriver(remoteAddress, DesiredCapabilities.chrome());
}