Example usage for org.openqa.selenium WebDriver manage

List of usage examples for org.openqa.selenium WebDriver manage

Introduction

In this page you can find the example usage for org.openqa.selenium WebDriver manage.

Prototype

Options manage();

Source Link

Document

Gets the Option interface

Usage

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

License:Open Source License

private String getScreenSize(WebDriver driver) {
    return driver.manage().window().getSize().toString();
}

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 {//ww  w  .j a  v a2  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 {// w  w  w . jav  a2  s  . c  o m

        /**
         * 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.codehaus.mojo.screenshot.ScreenshotMojo.java

License:Apache License

public void execute() throws MojoExecutionException {
    System.setProperty("webdriver.chrome.driver", chromeDriverPath);
    webDriverMap = new HashMap<String, WebDriver>();
    final DesiredCapabilities firefoxCapabilities = DesiredCapabilities.firefox();
    firefoxCapabilities.setJavascriptEnabled(true);
    WebDriver firefoxDriver = new FirefoxDriver(firefoxCapabilities);
    firefoxDriver.manage().window().maximize();
    webDriverMap.put("firefox", firefoxDriver);
    final DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
    ieCapabilities.setJavascriptEnabled(true);
    WebDriver ieDriver = new InternetExplorerDriver(ieCapabilities);
    ieDriver.manage().window().maximize();
    webDriverMap.put("ie", ieDriver);
    final ChromeOptions chromeOptions = new ChromeOptions();
    WebDriver chromeDriver = new ChromeDriver(chromeOptions);
    chromeDriver.manage().window().maximize();
    webDriverMap.put("chrome", chromeDriver);
    WebDriver smallChromeDriver = new ChromeDriver(chromeOptions);
    smallChromeDriver.manage().window().setSize(new Dimension(400, 800));
    webDriverMap.put("chrome-small", smallChromeDriver);
    if (this.inDirectory.exists() && this.inDirectory.isDirectory()) {
        final List<String> includes = new ArrayList<String>();
        includes.add("**/*.html");

        final List<String> excludes = new ArrayList<String>();
        excludes.add("**/archive/**");

        final DirectoryWalker dw = new DirectoryWalker();
        dw.setBaseDir(this.inDirectory);
        dw.setIncludes(includes);//from  w  w w  . j ava 2s .  c o m
        dw.setExcludes(excludes);

        dw.addDirectoryWalkListener(new ScreenshotDirectoryWalker());
        dw.scan();
    } else {
        getLog().debug("No wireframes in that folder");
    }
}

From source file:org.eclipse.skalli.selenium.utils.DriverProvider.java

License:Open Source License

/**
 * Try to login with the given user name and password
 * @param driver The {@link org.openqa.selenium.WebDriver}
 * @param username The user name//  w w w.j  ava  2  s.c  om
 * @param password The password
 * @return {@code true} if the login was possible, {@code false} otherwise
 */
public static boolean login(WebDriver driver, String username, String password) {
    try {
        LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class);
        LoginFailedPage loginFailedPage = PageFactory.initElements(driver, LoginFailedPage.class);
        MainPage mainPage = PageFactory.initElements(driver, MainPage.class);

        driver.manage().timeouts().implicitlyWait(500, TimeUnit.MILLISECONDS);
        while (loginPage.isDisplayed()) {
            loginPage.sendKeysToUserNameAndPasswordFieldAndSubmit(username, password);

            driver.manage().timeouts().implicitlyWait(500, TimeUnit.MILLISECONDS);

            //avoiding that the driver is positioned at getHTTPBaseUrl() + "/favicon.ico"
            //maybe a bug!?
            try {
                if (!loginFailedPage.isDisplayed() && !mainPage.isDisplayed()) {
                    navigateToBaseUrl(driver);
                }
            } catch (Exception e) {
            }

            //if the login page is displayed the login was successful
            try {
                if (mainPage.isDisplayed()) {
                    return true;
                }
            } catch (Exception e) {
            }
        }
    } catch (NoSuchElementException e) {
        return false;
    }

    //should never be reached
    return true;
}

From source file:org.eclipse.skalli.selenium.utils.DriverProvider.java

License:Open Source License

/**
 * Returns whether the driver is located on the login page or not
 * @param driver The driver//  ww  w  . j  ava 2s. c om
 * @return {@code true} if the driver is on the login page {@code false} otherwise
 */
public static boolean isOnLoginPage(WebDriver driver) {
    try {
        LoginPage login = PageFactory.initElements(driver, LoginPage.class);
        driver.manage().timeouts().implicitlyWait(500, TimeUnit.MILLISECONDS);
        login.isDisplayed();
    } catch (NoSuchElementException e) {
        return false;
    }

    return true;
}

From source file:org.ehoffman.testing.sample.WebDriverFactory.java

License:Apache License

/**
 * Returns a {@link WebDriver} instance built to the input specifications.
 * /* w  w w.j a v  a2s  . c  o m*/
 * @param type the type of {@link WebDriver}.
 * @return a {@link WebDriver} instance ready for use.
 */
public WebDriver buildWebdriver(final Driver type, final String url) {
    WebDriver driver = null;
    switch (type) {
    case CHROME:
        driver = new ChromeDriver();
        break;
    case FIREFOX:
        driver = new FirefoxDriver();
        break;
    case PHANTOMJS:
    default:
        driver = new PhantomJSDriver();
        break;
    }
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.NANOSECONDS);
    driver.navigate().to(url);
    return driver;
}

From source file:org.finra.jtaf.ewd.impl.DefaultSessionFactory.java

License:Apache License

@Override
public WebDriver createInnerDriver(Map<String, String> options, DesiredCapabilities capabilities)
        throws Exception {
    ClientProperties properties = new ClientProperties(options.get("client"));

    WebDriver wd = null;
    DesiredCapabilities desiredCapabilities = new DesiredCapabilities(capabilities);
    String browser = properties.getBrowser();

    if (properties.isUseGrid()) {
        RemoteWebDriver remoteWebDriver = new RemoteWebDriver(new URL(properties.getGridUrl()), capabilities);
        remoteWebDriver.setFileDetector(new LocalFileDetector());
        wd = remoteWebDriver;//from  w  w w. j  a va 2 s  . c om
    } else {
        if (browser == null || browser.equals("")) {
            throw new RuntimeException(
                    "Browser cannot be null. Please set 'browser' in client properties. Supported browser types: IE, Firefox, Chrome, Safari, HtmlUnit.");
        } else if (browser.equalsIgnoreCase("ie") || browser.equalsIgnoreCase("iexplore")
                || browser.equalsIgnoreCase("*iexplore")) {
            String webdriverIEDriver = properties.getWebDriverIEDriver();

            if (webdriverIEDriver != null) {
                System.setProperty("webdriver.ie.driver", webdriverIEDriver);
            }

            String browserVersion = properties.getBrowserVersion();

            if (browserVersion == null || browserVersion.equals("")) {
                throw new RuntimeException(
                        "When using IE as the browser, please set 'browser.version' in client properties");
            } else {
                if (browserVersion.startsWith("9")) {
                    desiredCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
                    desiredCapabilities.setCapability(
                            InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
                    wd = new InternetExplorerDriver(desiredCapabilities);
                } else {
                    wd = new InternetExplorerDriver(desiredCapabilities);
                }
            }
        } else if ((browser.equalsIgnoreCase("firefox") || browser.equalsIgnoreCase("*firefox"))) {
            final String ffProfileFolder = properties.getFirefoxProfileFolder();
            final String ffProfileFile = properties.getFirefoxProfileFile();
            final String path = properties.getBinaryPath();

            if (path != null) {
                FirefoxBinary fireFox = getFFBinary(path);
                FirefoxProfile ffp = null;

                if (ffProfileFolder != null) {
                    ffp = new FirefoxProfile(new File(ffProfileFolder));
                } else {
                    ffp = new FirefoxProfile();
                }

                if (ffProfileFile != null) {
                    addPreferences(ffp, ffProfileFile);
                }

                addPreferences(ffp);

                List<String> ffExtensions = properties.getFirefoxExtensions();
                if (ffExtensions != null && ffExtensions.size() > 0) {
                    addExtensionsToFirefoxProfile(ffp, ffExtensions);
                }

                wd = new FirefoxDriver(fireFox, ffp, desiredCapabilities);
            } else {
                wd = new FirefoxDriver(desiredCapabilities);
            }
        } else if (browser.equalsIgnoreCase("chrome")) {

            String webdriverChromeDriver = properties.getWebDriverChromeDriver();

            if (webdriverChromeDriver != null) {
                System.setProperty("webdriver.chrome.driver", webdriverChromeDriver);
            }

            wd = new ChromeDriver(desiredCapabilities);
        } else if (browser.equalsIgnoreCase("safari")) {
            wd = new SafariDriver(desiredCapabilities);
        } else if (browser.equalsIgnoreCase("htmlunit")) {
            wd = new HtmlUnitDriver(desiredCapabilities);
            ((HtmlUnitDriver) wd).setJavascriptEnabled(true);
        } else {
            throw new Exception("Unsupported browser type: " + browser
                    + ". Supported browser types: IE, Firefox, Chrome, Safari, HtmlUnit.");
        }

        // move browser windows to specific position. It's useful for
        // debugging...
        final int browserInitPositionX = properties.getBrowserInitPositionX();
        final int browserInitPositionY = properties.getBrowserInitPositionY();
        if (browserInitPositionX != 0 || browserInitPositionY != 0) {
            wd.manage().window().setSize(new Dimension(1280, 1024));
            wd.manage().window().setPosition(new Point(browserInitPositionX, browserInitPositionY));
        }
    }

    return wd;
}

From source file:org.fitting.selenium.fixture.CookieFixture.java

License:Apache License

/**
 * Get a cookie./*from w  w  w.j  a  va2 s  .c  o  m*/
 *
 * @param driver The Selenium web driver.
 * @param name   The name of the cookie.
 *
 * @return cookie The cookie or <code>null</code> if none was found with the given name.
 */
private Cookie getCookie(WebDriver driver, String name) {
    return driver.manage().getCookieNamed(name);
}

From source file:org.fitting.selenium.fixture.CookieFixture.java

License:Apache License

/**
 * Adds a cookie with the given data./*from w w w  . ja  v a2 s. c o m*/
 *
 * @param driver The web driver.
 * @param name   The name of the cookie.
 * @param value  The value of the cookie.
 * @param path   The path to set the cookie.
 * @param domain The domain.
 */
private void addCookie(final WebDriver driver, final String name, final String value, final String path,
        final String domain) {
    handleCookie(domain, new CookieCallback() {
        @Override
        public void execute() {
            final Cookie.Builder builder = new Cookie.Builder(name, value);
            if (isNotEmpty(path)) {
                builder.path(path);
            }
            if (isNotEmpty(domain)) {
                builder.domain(getStrippedDomain(domain));
            }

            driver.manage().addCookie(builder.build());
        }
    });
}