Search in sources :

Example 1 with SeleniumWComponentInputWebElement

use of com.github.bordertech.wcomponents.test.selenium.element.SeleniumWComponentInputWebElement 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)

Example 2 with SeleniumWComponentInputWebElement

use of com.github.bordertech.wcomponents.test.selenium.element.SeleniumWComponentInputWebElement in project wcomponents by BorderTech.

the class SeleniumWComponentsUtil method wrapInputElementWithTypedWebElement.

/**
 * Analyze the input element and attempt to wrap it in the appropriate component-specific subclass. If not component
 * specific subclass can be identified then the element will be wrapped in a SeleniumWComponentWebElement.
 *
 * @param driver the WebDriver.
 * @param element the default Selenium WebElement.
 * @return a subtype of SeleniumWComponentWebElement specific to the element type.
 */
public static SeleniumWComponentInputWebElement wrapInputElementWithTypedWebElement(final WebDriver driver, final WebElement element) {
    String tag = element.getTagName();
    if (tag.equals(SeleniumWComponentInputWebElement.EDITABLE_TAG)) {
        String type = element.getAttribute("type");
        WebElement el = element.findElement(By.xpath(".."));
        switch(type) {
            case SeleniumWCheckBoxWebElement.TYPE:
                return new SeleniumWCheckBoxWebElement(el, driver);
            case SeleniumWTextFieldWebElement.TYPE:
                // Text fields have a wrapping Span, we want to wrap that
                return new SeleniumWTextFieldWebElement(el, driver);
            case SeleniumWEmailFieldWebElement.TYPE:
                return new SeleniumWEmailFieldWebElement(el, driver);
            case SeleniumWRadioButtonWebElement.TYPE:
                return new SeleniumWRadioButtonWebElement(el, driver);
            default:
                return new SeleniumWComponentInputWebElement(el, driver);
        }
    } else if (tag.equals(SeleniumWTextAreaWebElement.TEXTAREA_TAG)) {
        return new SeleniumWTextAreaWebElement(element.findElement(By.xpath("..")), driver);
    } else if (tag.equals(SeleniumWSelectWebElement.SELECT_TAG)) {
        return new SeleniumWSelectWebElement(element.findElement(By.xpath("..")), driver);
    }
    return new SeleniumWComponentInputWebElement(element, driver);
}
Also used : SeleniumWCheckBoxWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWCheckBoxWebElement) SeleniumWSelectWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWSelectWebElement) SeleniumWTextAreaWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWTextAreaWebElement) SeleniumWEmailFieldWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWEmailFieldWebElement) SeleniumWTextFieldWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWTextFieldWebElement) WebElement(org.openqa.selenium.WebElement) SeleniumWTextAreaWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWTextAreaWebElement) SeleniumWCheckBoxWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWCheckBoxWebElement) SeleniumWComponentWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWComponentWebElement) SeleniumWDialogWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWDialogWebElement) SeleniumWRadioButtonWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWRadioButtonWebElement) SeleniumWComponentInputWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWComponentInputWebElement) SeleniumWEmailFieldWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWEmailFieldWebElement) SeleniumWSelectWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWSelectWebElement) SeleniumWRadioButtonWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWRadioButtonWebElement) SeleniumWTextFieldWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWTextFieldWebElement) SeleniumWComponentInputWebElement(com.github.bordertech.wcomponents.test.selenium.element.SeleniumWComponentInputWebElement)

Aggregations

SeleniumWComponentInputWebElement (com.github.bordertech.wcomponents.test.selenium.element.SeleniumWComponentInputWebElement)2 WebElement (org.openqa.selenium.WebElement)2 SeleniumWCheckBoxWebElement (com.github.bordertech.wcomponents.test.selenium.element.SeleniumWCheckBoxWebElement)1 SeleniumWComponentWebElement (com.github.bordertech.wcomponents.test.selenium.element.SeleniumWComponentWebElement)1 SeleniumWDialogWebElement (com.github.bordertech.wcomponents.test.selenium.element.SeleniumWDialogWebElement)1 SeleniumWEmailFieldWebElement (com.github.bordertech.wcomponents.test.selenium.element.SeleniumWEmailFieldWebElement)1 SeleniumWRadioButtonWebElement (com.github.bordertech.wcomponents.test.selenium.element.SeleniumWRadioButtonWebElement)1 SeleniumWSelectWebElement (com.github.bordertech.wcomponents.test.selenium.element.SeleniumWSelectWebElement)1 SeleniumWTextAreaWebElement (com.github.bordertech.wcomponents.test.selenium.element.SeleniumWTextAreaWebElement)1 SeleniumWTextFieldWebElement (com.github.bordertech.wcomponents.test.selenium.element.SeleniumWTextFieldWebElement)1 ArrayList (java.util.ArrayList)1 FindsById (org.openqa.selenium.internal.FindsById)1