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;
}
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);
}
Aggregations