Example usage for org.openqa.selenium WebDriver getTitle

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

Introduction

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

Prototype

String getTitle();

Source Link

Document

Get the title of the current page.

Usage

From source file:com.spambot.invisicow.SeleniumDriver.java

public void driveExample() {
    // Create a new instance of the Firefox driver
    // Notice that the remainder of the code relies on the interface, 
    // not the implementation.
    WebDriver driver = new FirefoxDriver();

    // And now use this to visit Google
    driver.get("http://www.google.com");
    // Alternatively the same thing can be done like this
    // driver.navigate().to("http://www.google.com");

    // Find the text input element by its name
    WebElement element = driver.findElement(By.name("q"));

    // Enter something to search for
    element.sendKeys("Cheese!");

    // Now submit the form. WebDriver will find the form for us from the element
    element.submit();/*from  w  w w .ja v a2  s . c  om*/

    // Check the title of the page
    System.out.println("Page title is: " + driver.getTitle());

    // Google's search is rendered dynamically with JavaScript.
    // Wait for the page to load, timeout after 10 seconds
    (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            return d.getTitle().toLowerCase().startsWith("cheese!");
        }
    });

    // Should see: "cheese! - Google Search"
    System.out.println("Page title is: " + driver.getTitle());

    //Close the browser
    driver.quit();
}

From source file:com.springer.omelet.driver.DriverUtility.java

License:Apache License

/***
 * Switching between windows.//from  ww w .  java  2  s .c o  m
 * 
 * @param driver
 * @param sString
 *            :Target window Title
 * @return:True if window switched
 */
public static boolean switchToWindow(WebDriver driver, String sString) {
    String currentHandle = driver.getWindowHandle();
    Set<String> handles = driver.getWindowHandles();
    if (handles.size() >= 1) {
        for (String handle : handles) {
            LOGGER.debug("Switching to other window");
            driver.switchTo().window(handle);
            if (driver.getTitle().contains(sString)) {
                LOGGER.info("switched to window with title:" + sString);
                return true;
            }
        }
        driver.switchTo().window(currentHandle);

        LOGGER.info("Window with title:" + sString + " Not present,Not able to switch");
        return false;
    } else {
        LOGGER.info("There is only one window handle :" + currentHandle);
        return false;
    }
}

From source file:com.sugarcrm.candybean.automation.VInterface.java

License:Open Source License

/**
 * Focus a browser window by its window title or URL if it does not
 * match the current title or URL.//  w ww .  ja  v  a2s  . c o m
 *
 * <p>If more than one window has the same title or URL, the first
 * encountered is the one that is focused.</p>
 *
 * @param titleOrUrl     the exact window title or URL to be matched
 * @throws Exception   if the specified window cannot be found
 */
public void focusWindow(String titleOrUrl) throws Exception {
    String curTitle = this.wd.getTitle();
    String curUrl = this.wd.getCurrentUrl();
    if (titleOrUrl.equals(curTitle) || titleOrUrl.equals(curUrl)) {
        candybean.log.warning(
                "No focus was made because the given string matched the current title or URL: " + titleOrUrl);
    } else {
        Set<String> windowHandlesSet = this.wd.getWindowHandles();
        String[] windowHandles = windowHandlesSet.toArray(new String[] { "" });
        int i = 0;
        boolean windowFound = false;
        while (i < windowHandles.length && !windowFound) {
            WebDriver window = this.wd.switchTo().window(windowHandles[i]);
            if (window.getTitle().equals(titleOrUrl) || window.getCurrentUrl().equals(titleOrUrl)) {
                windows.push(new Pair<Integer, String>(new Integer(i), this.wd.getWindowHandle()));
                candybean.log.info("Focused by title or URL: " + titleOrUrl + " to window: " + windows.peek());
                windowFound = true;
            }
            i++;
        }
        if (!windowFound) {
            this.wd.switchTo().window(windows.peek().y);
            throw new Exception("The given focus window string matched no title or URL: " + titleOrUrl);
        }
    }
}

From source file:com.sugarcrm.candybean.automation.webdriver.WebDriverInterface.java

License:Open Source License

/**
 * Focus a browser window by its window title or URL if it does not
 * match the current title or URL.// ww w  .j av  a2  s  .  c o  m
 *
 * <p>If more than one window has the same title or URL, the first
 * encountered is the one that is focused.</p>
 *
 * @param titleOrUrl     the exact window title or URL to be matched
 * @throws CandybeanException   if the specified window cannot be found
 */
public void focusWindow(String titleOrUrl) throws CandybeanException {
    String curTitle = this.wd.getTitle();
    String curUrl = this.wd.getCurrentUrl();
    if (titleOrUrl.equals(curTitle) || titleOrUrl.equals(curUrl)) {
        logger.warning(
                "No focus was made because the given string matched the current title or URL: " + titleOrUrl);
    } else {
        Set<String> windowHandlesSet = this.wd.getWindowHandles();
        String[] windowHandles = windowHandlesSet.toArray(new String[] { "" });
        int i = 0;
        boolean windowFound = false;
        while (i < windowHandles.length && !windowFound) {
            WebDriver window = this.wd.switchTo().window(windowHandles[i]);
            if (window.getTitle().equals(titleOrUrl) || window.getCurrentUrl().equals(titleOrUrl)) {
                windows.push(new Pair<Integer, String>(new Integer(i), this.wd.getWindowHandle()));
                logger.info("Focused by title or URL: " + titleOrUrl + " to window: " + windows.peek());
                windowFound = true;
            }
            i++;
        }
        if (!windowFound) {
            this.wd.switchTo().window(windows.peek().y);
            throw new CandybeanException(
                    "The given focus window string matched no title or URL: " + titleOrUrl);
        }
    }
}

From source file:com.thoughtworks.inproctester.tests.InProcessHtmlUnitDriverTest.java

License:Apache License

@Test
public void shouldSupportGetAndPostRequests() {

    WebDriver htmlUnitDriver = new InProcessHtmlUnitDriver(httpAppTester);

    htmlUnitDriver.get("http://localhost/contacts/add");

    assertThat(htmlUnitDriver.getTitle(), is("Test Application"));
    assertThat(htmlUnitDriver.findElement(By.tagName("h3")).getText(), is("Contact Details"));
    WebElement contactNameElement = htmlUnitDriver.findElement(By.name("contactName"));

    contactNameElement.clear();/*w ww  .j a  v  a 2s.  c  o  m*/
    contactNameElement.sendKeys("My Contact");

    htmlUnitDriver.findElement(By.tagName("form")).submit();

    assertThat(htmlUnitDriver.getCurrentUrl(), is("http://localhost/contacts/1"));
    assertThat(htmlUnitDriver.findElement(By.name("contactName")).getAttribute("value"), is("My Contact"));

}

From source file:com.thoughtworks.inproctester.tests.InProcessHtmlUnitDriverTestWithWebXml.java

License:Apache License

@Test
public void shouldSupportGetAndPostRequests() {

    WebDriver htmlUnitDriver = new InProcessHtmlUnitDriver(httpAppTester);

    htmlUnitDriver.get("http://localhost/contacts/add");

    assertThat(htmlUnitDriver.getTitle(), is("Test Application"));
    assertThat(htmlUnitDriver.findElement(By.tagName("h3")).getText(), is("Contact Details"));
    WebElement contactNameElement = htmlUnitDriver.findElement(By.name("contactName"));

    assertThat(contactNameElement.getAttribute("value"), isEmptyString());
    contactNameElement.sendKeys("My Contact");

    htmlUnitDriver.findElement(By.tagName("form")).submit();

    assertThat(htmlUnitDriver.getCurrentUrl(), is("http://localhost/contacts/1"));
    assertThat(htmlUnitDriver.findElement(By.name("contactName")).getAttribute("value"), is("My Contact"));

}

From source file:com.thoughtworks.selenium.StartTest.java

License:Apache License

@Test
public void shouldBeAbleToCreateAWebDriverBackedSeleniumInstance() throws MalformedURLException {
    URL wdServer = new URL(String.format("http://%s:%d/wd/hub", url.getHost(), url.getPort()));
    WebDriver driver = new RemoteWebDriver(wdServer, DesiredCapabilities.firefox());
    Capabilities capabilities = ((HasCapabilities) driver).getCapabilities();

    DefaultSelenium selenium = new DefaultSelenium(url.getHost(), url.getPort(), "*webdriver", root);

    try {/*from w  w w  .  j a v  a 2  s . co  m*/
        selenium.start(capabilities);
        selenium.open(wdServer.toString());

        String seleniumTitle = selenium.getTitle();
        String title = driver.getTitle();

        assertEquals(title, seleniumTitle);
    } finally {
        selenium.stop();
        // This isn't handled elegantly yet
        //      driver.quit();
    }
}

From source file:com.thoughtworks.selenium.webdriven.commands.GetAllWindowTitles.java

License:Apache License

@Override
protected String[] handleSeleneseCommand(WebDriver driver, String ignored, String alsoIgnored) {
    String current = driver.getWindowHandle();

    List<String> attributes = new ArrayList<>();
    for (String handle : driver.getWindowHandles()) {
        driver.switchTo().window(handle);
        attributes.add(driver.getTitle());
    }/*from  ww w  . ja v  a2s .  c  o  m*/

    driver.switchTo().window(current);

    return attributes.toArray(new String[attributes.size()]);

}

From source file:com.thoughtworks.selenium.webdriven.commands.GetTitle.java

License:Apache License

@Override
protected String handleSeleneseCommand(WebDriver driver, String ignored, String alsoIgnored) {
    return driver.getTitle();
}

From source file:com.thoughtworks.selenium.webdriven.commands.Windows.java

License:Apache License

private void selectWindowWithTitle(WebDriver driver, String title) {
    String current = driver.getWindowHandle();
    for (String handle : driver.getWindowHandles()) {
        driver.switchTo().window(handle);
        if (title.equals(driver.getTitle())) {
            return;
        }//w  w w . j  ava 2  s. c om
    }

    driver.switchTo().window(current);
    throw new SeleniumException("Unable to select window with title: " + title);
}