com.github.codewrs.selenium.FindElementAdvanced.java Source code

Java tutorial

Introduction

Here is the source code for com.github.codewrs.selenium.FindElementAdvanced.java

Source

package com.github.codewrs.selenium;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/** 
* FindElementAdvanced.java
* GNU GENERAL PUBLIC LICENSE
* Copyright (C) 2007 Free Software Foundation, Inc.
* All rights reserved.
*/

/**
 * @author CodeWRS
 *
 */
public class FindElementAdvanced {
    /**
     * Find all elements within the WebElement using the given mechanism.
     * Returns WebElement by matching with index of the WebElement.
     * @param driver WebDriver instance.
     * @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;
    }

    /**
     * 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.
     * @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;
    }

    /**
     * Find all elements within the current page using the given mechanism.
     * Returns WebElement with the matching text.
     * @param driver WebDriver instance.
     * @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;
    }

    /**
     * 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.
     * @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;
    }

    /**
     * Find all elements within the current page using the given mechanism.
     * Returns WebElement by matching the value of the attribute.
     * @param driver WebDriver instance.
     * @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;
    }

    /**
     * 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.
     * @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;
    }

    /**
     * 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.
     * @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;
    }
}