Search in sources :

Example 1 with FindsById

use of org.openqa.selenium.internal.FindsById in project wcomponents by BorderTech.

the class ByLabel method findElements.

/**
 * Find the elements by label.
 *
 * @param context the search context.
 * @return the matching elements.
 */
@Override
public List<WebElement> findElements(final SearchContext context) {
    List<WebElement> labels;
    if (labelId != null) {
        labels = ((FindsById) context).findElementsById(labelId);
    } else {
        String xpath;
        if (partialMatch) {
            xpath = String.format((relative ? XPATH_LABEL_TEXT_CONTAINS_RELATIVE : XPATH_LABEL_TEXT_CONTAINS_ROOT), labelText);
        } else {
            xpath = String.format((relative ? XPATH_LABEL_TEXT_EXACT_RELATIVE : XPATH_LABEL_TEXT_EXACT_ROOT), labelText);
        }
        labels = ((FindsByXPath) context).findElementsByXPath(xpath);
    }
    if (CollectionUtils.isEmpty(labels)) {
        return labels;
    }
    List<WebElement> results = new ArrayList<>();
    for (WebElement label : labels) {
        WebElement nestedElement;
        if ("legend".equalsIgnoreCase(label.getTagName())) {
            // the labelled element is the label's parent element
            results.add(label.findElement(By.xpath("..")));
            continue;
        }
        String elementId = label.getAttribute(SeleniumWComponentWebProperties.ATTRIBUTE_LABEL_FOR.toString());
        if (StringUtils.isEmpty(elementId)) {
            elementId = label.getAttribute(SeleniumWComponentWebProperties.ATTRIBUTE_LABEL_FAUX_FOR.toString());
        }
        if (StringUtils.isEmpty(elementId)) {
            elementId = label.getAttribute(SeleniumWComponentWebProperties.ATTRIBUTE_LABEL_FOR_READ_ONLY.toString());
        }
        if ("span".equalsIgnoreCase(label.getTagName()) && OPTION_INNER_SPAN_CLASSNAME.equals(label.getAttribute("class"))) {
            label = label.findElement(By.xpath(".."));
        }
        if (StringUtils.isEmpty(elementId)) {
            if ("label".equalsIgnoreCase(label.getTagName())) {
                nestedElement = label.findElement(By.tagName("input"));
                if (nestedElement == null) {
                    nestedElement = label.findElement(By.tagName("select"));
                }
                if (nestedElement == null) {
                    nestedElement = label.findElement(By.tagName("textarea"));
                }
                if (nestedElement != null) {
                    results.add(nestedElement);
                }
            }
            // otherwise the search has probably picked up a non-label span element.
            continue;
        }
        WebElement element = ((FindsById) context).findElementById(elementId);
        if (elementId.endsWith(SeleniumWComponentWebProperties.ID_SUFFIX.toString())) {
            SeleniumWComponentInputWebElement wrapped = SeleniumWComponentsUtil.wrapInputElementWithTypedWebElement((WebDriver) context, element);
            results.add(wrapped);
        } else {
            results.add(element);
        }
    }
    return results;
}
Also used : FindsById(org.openqa.selenium.internal.FindsById) ArrayList(java.util.ArrayList) SeleniumWComponentInputWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWComponentInputWebElement) WebElement(org.openqa.selenium.WebElement) SeleniumWComponentInputWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWComponentInputWebElement)

Aggregations

SeleniumWComponentInputWebElement (com.github.bordertech.wcomponents.test.selenium.element.SeleniumWComponentInputWebElement)1 ArrayList (java.util.ArrayList)1 WebElement (org.openqa.selenium.WebElement)1 FindsById (org.openqa.selenium.internal.FindsById)1