Example usage for org.openqa.selenium WebElement getText

List of usage examples for org.openqa.selenium WebElement getText

Introduction

In this page you can find the example usage for org.openqa.selenium WebElement getText.

Prototype

String getText();

Source Link

Document

Get the visible (i.e.

Usage

From source file:com.github.codewrs.selenium.FindElementAdvanced.java

License:Open Source License

/**
 * Find all elements within the WebElement using the given mechanism.
 * Returns WebElement by matching with index of the WebElement.
 * @param driver WebDriver instance./*from   w ww. j a  va 2s  .c  om*/
 * @param by The locating mechanism.
 * @param elementIndex Index of the WebElement to find.
 * @param wait Explicit Wait Time.
 * @return WebElement or null if nothing matches.
 */
protected WebElement findElementOnList(WebDriver driver, By by, int elementIndex, WebDriverWait wait) {
    try {
        List<WebElement> elementList = driver.findElements(by);
        wait.until(ExpectedConditions.visibilityOfAllElements(elementList));
        int listSize = elementList.size();
        System.out.println("The number of element counted is : " + listSize);
        if (listSize > 0) {
            int i = 0;
            for (WebElement ele : elementList) {
                if (elementIndex == i) {
                    System.out.println("The returned WebElement text : " + ele.getText());
                    return ele;
                }
                i++;
            }
        }
    } catch (NoSuchElementException e) {
        System.out.println("No Element found.");
    }
    System.out.println("No Element found.");
    return null;
}

From source file:com.github.codewrs.selenium.FindElementAdvanced.java

License:Open Source License

/**
 * Find all elements within the WebElement using the given mechanism.
 * Returns WebElement by matching with index of the WebElement.
 * @param webElement Base WebElement containing the WebElements.
 * @param by The locating mechanism.// w w  w .  jav  a 2  s. c o  m
 * @param elementIndex Index of the WebElement to find.
 * @param wait Explicit Wait Time.
 * @return WebElement or null if nothing matches.
 */
protected WebElement findElementOnList(WebElement webElement, By by, int elementIndex, WebDriverWait wait) {
    try {
        List<WebElement> elementList = webElement.findElements(by);
        wait.until(ExpectedConditions.visibilityOfAllElements(elementList));
        int listSize = elementList.size();
        System.out.println("The number of element counted is : " + listSize);
        if (listSize > 0) {
            int i = 0;
            for (WebElement ele : elementList) {
                if (elementIndex == i) {
                    System.out.println("The returned WebElement text : " + ele.getText());
                    return ele;
                }
                i++;
            }
        }
    } catch (NoSuchElementException e) {
        System.out.println("No Element found.");
    }
    System.out.println("No Element found.");
    return null;
}

From source file:com.github.codewrs.selenium.FindElementAdvanced.java

License:Open Source License

/**
 * Find all elements within the current page using the given mechanism.
 * Returns WebElement with the matching text.
 * @param driver WebDriver instance./*  w  w w  . j  a va 2 s.  c  o m*/
 * @param by The locating mechanism.
 * @param textToMatch Text to match with text of found WebElements.
 * @param wait Explicit Wait Time.
 * @return WebElement or null if nothing matches.
 */
protected WebElement findElementOnList(WebDriver driver, By by, String valueToMatch, WebDriverWait wait) {
    try {
        List<WebElement> elementList = driver.findElements(by);
        wait.until(ExpectedConditions.visibilityOfAllElements(elementList));
        int listSize = elementList.size();
        System.out.println("The number of element counted is : " + listSize);
        if (listSize > 0) {
            for (WebElement ele : elementList) {
                if (ele.getText() != null) {
                    if (ele.getText().contentEquals(valueToMatch)) {
                        System.out.println("The returned WebElement text : " + ele.getText());
                        return ele;
                    }
                }
            }
        }
    } catch (NoSuchElementException e) {
        System.out.println("No Element found.");
    }
    System.out.println("No Element found.");
    return null;
}

From source file:com.github.codewrs.selenium.FindElementAdvanced.java

License:Open Source License

/**
 * Find all the elements within the WebElement using the given mechanism.
 * Returns WebElement with the matching text.
 * @param webElement Base WebElement to find other WebElements.
 * @param by The locating mechanism.//  w  w w. ja v a  2  s.c o m
 * @param textToMatch Text to match with text of found WebElements.
 * @param wait Explicit Wait Time.
 * @return WebElement or null if nothing matches.
 */
protected WebElement findElementOnList(WebElement webElement, By by, String valueToMatch, WebDriverWait wait) {
    try {
        List<WebElement> elementList = webElement.findElements(by);
        wait.until(ExpectedConditions.visibilityOfAllElements(elementList));
        int listSize = elementList.size();
        System.out.println("The number of element counted is : " + listSize);
        if (listSize > 0) {
            for (WebElement ele : elementList) {
                if (ele.getText() != null) {
                    if (ele.getText().contentEquals(valueToMatch)) {
                        System.out.println("The returned WebElement text : " + ele.getText());
                        return ele;
                    }
                }
            }
        }
    } catch (NoSuchElementException e) {
        System.out.println("No Element found.");
    }
    System.out.println("No Element found.");
    return null;
}

From source file:com.github.codewrs.selenium.FindElementAdvanced.java

License:Open Source License

/**
 * Find all elements within the current page using the given mechanism.
 * Returns WebElement by matching the value of the attribute.
 * @param driver WebDriver instance./*  w  w w . j  a v a2 s  .  co  m*/
 * @param by The locating mechanism.
 * @param attributeToSearch The attribute to locate. 
 * @param valueToMatch Text to match with attribute's value.
 * @param wait Explicit Wait Time.
 * @return WebElement or null if nothing matches.
 */
protected WebElement findElementOnList(WebDriver driver, By by, String attributeToSearch, String valueToMatch,
        WebDriverWait wait) {
    try {
        List<WebElement> elementList = driver.findElements(by);
        wait.until(ExpectedConditions.visibilityOfAllElements(elementList));
        int listSize = elementList.size();
        System.out.println("The number of WebElements found : " + listSize);
        if (listSize > 0) {
            for (WebElement ele : elementList) {
                if (ele.getAttribute(attributeToSearch) != null) {
                    if (ele.getAttribute(attributeToSearch).contentEquals(valueToMatch)) {
                        System.out.println("The returned WebElement text : " + ele.getText());
                        return ele;
                    }
                }
            }
        }
    } catch (NoSuchElementException e) {
        System.out.println("No Element found.");
    }
    System.out.println("No Element found.");
    return null;
}

From source file:com.github.codewrs.selenium.FindElementAdvanced.java

License:Open Source License

/**
 * Find all the elements within the WebElement using the given mechanism.
 * Returns WebElement by matching with the value of the attribute.
 * @param webElement Base WebElement to find other WebElements.
 * @param by The locating mechanism./*from   w ww. java2s.c o  m*/
 * @param attributeToSearch The attribute to locate. 
 * @param valueToMatch Text to match with attribute's value.
 * @param wait Explicit Wait Time.
 * @return WebElement or null if nothing matches.
 */
protected WebElement findElementOnList(WebElement webElement, By by, String attributeToSearch,
        String valueToMatch, WebDriverWait wait) {
    try {
        List<WebElement> elementList = webElement.findElements(by);
        wait.until(ExpectedConditions.visibilityOfAllElements(elementList));
        int listSize = elementList.size();
        System.out.println("The number of WebElements found : " + listSize);
        if (listSize > 0) {
            for (WebElement ele : elementList) {
                if (ele.getAttribute(attributeToSearch) != null) {
                    if (ele.getAttribute(attributeToSearch).contentEquals(valueToMatch)) {
                        System.out.println("The returned WebElement text : " + ele.getText());
                        return ele;
                    }
                }
            }
        }
    } catch (NoSuchElementException e) {
        System.out.println("No Element found.");
    }
    System.out.println("No Element found.");
    return null;
}

From source file:com.github.codewrs.selenium.FindElementAdvanced.java

License:Open Source License

/**
 * Find all <tr> tags within the current table body.
 * Then searches <th> tags for each <tr>. 
 * Returns <tr> WebElement by matching with <th> text.
 * @param tBody WebElement of the table body.
 * @param thTextToMatch Text to match with <th> tag's text.
 * @param wait Explicit Wait Time.// w w  w .j av  a  2s  .c om
 * @return WebElement or null if nothing matches.
 */
protected WebElement findElementOnTable(WebElement tBody, String thTextToMatch, WebDriverWait wait) {
    try {
        List<WebElement> trList = tBody.findElements(By.tagName("tr"));
        wait.until(ExpectedConditions.visibilityOfAllElements(trList));
        int trSize = trList.size();
        if (trSize > 0) {
            for (WebElement webElement : trList) {
                List<WebElement> tdList = webElement.findElements(By.tagName("td"));
                int thSize = tdList.size();
                if (thSize > 0) {
                    for (WebElement ele : tdList) {
                        if (ele.getText() != null) {
                            if (ele.getText().equals(thTextToMatch)) {
                                System.out.println("The returned WebElement text : " + ele.getText());
                                return ele;
                            }
                        }
                    }
                }
            }
        }
    } catch (NoSuchElementException e) {
        System.out.println("No Element found.");
    }
    System.out.println("No Element found.");
    return null;
}

From source file:com.github.fscheffer.arras.demo.DataTableIT.java

License:Apache License

private void assertCells(List<WebElement> cells, String... values) {

    for (int i = 0; i < values.length; i++) {

        WebElement cell = cells.get(i);
        Assert.assertEquals(cell.getText(), values[i]);
    }//from   w  ww.  j av a  2 s  . com
}

From source file:com.github.licanhua.test.framework.util.PageHelper.java

License:Apache License

public static <T> T clickAndToPage(WebElement webElement, Class<T> pageClass, Element parent) {
    checkNotNull(webElement);/*from w  w  w  . j a  v  a 2s .c om*/
    checkNotNull(pageClass);
    checkNotNull(parent);

    ElementContext elementContext = parent.getElementContext();
    logger.info("Take snapshot");

    // Take snapshot
    takeSnapshot(elementContext);

    logger.info("Click on " + webElement.getText());
    webElement.click();

    // Wait for Page Load Complete

    logger.info("jump to new page " + pageClass.getName());
    return toPage(pageClass, elementContext);
}

From source file:com.github.stefaneicher.democd.HelloControllerUAT.java

License:Apache License

@Test
public void testMainPage() throws InterruptedException {
    driver.get(url);/*  w  w  w .j a  v  a 2s .  c o  m*/
    WebElement element = driver.findElement(By.id("hello"));
    assertEquals("Hello democd", element.getText());
    // Check the title of the page
    assertEquals("democd", driver.getTitle());
}