Search in sources :

Example 1 with ElementDescriptionException

use of ru.sbtqa.tag.pagefactory.exceptions.ElementDescriptionException in project page-factory-2 by sbtqa.

the class PageFactoryUtils method getElementByField.

/**
 * Get object from a field of specified parent
 *
 * @param parentObject object that contains(must contain) given field
 * @param field field to get
 * @param <T> supposed type of the field. if field cannot be cast into
 * this type, it will fail
 * @return element of requested type
 * @throws ElementDescriptionException in case if field does not belong
 * to the object, or element could not be cast to specified type
 */
@SuppressWarnings("unchecked")
public static <T> T getElementByField(Object parentObject, Field field) throws ElementDescriptionException {
    field.setAccessible(true);
    Object element;
    try {
        element = field.get(parentObject);
        return (T) element;
    } catch (IllegalArgumentException | IllegalAccessException iae) {
        throw new ElementDescriptionException("Specified parent object is not an instance of the class or " + "interface, declaring the underlying field: '" + field + "'", iae);
    } catch (ClassCastException cce) {
        throw new ElementDescriptionException("Requested type is incompatible with field '" + field.getName() + "' of '" + parentObject.getClass().getCanonicalName() + "'", cce);
    }
}
Also used : ElementDescriptionException(ru.sbtqa.tag.pagefactory.exceptions.ElementDescriptionException)

Example 2 with ElementDescriptionException

use of ru.sbtqa.tag.pagefactory.exceptions.ElementDescriptionException in project page-factory-2 by sbtqa.

the class DefaultReflection method getElementByField.

@Override
public <T> T getElementByField(Object parentObject, Field field) throws ElementDescriptionException {
    field.setAccessible(true);
    Object element;
    try {
        element = field.get(parentObject);
        return (T) element;
    } catch (IllegalArgumentException | IllegalAccessException iae) {
        throw new ElementDescriptionException("Specified parent object is not an instance of the class or " + "interface, declaring the underlying field: '" + field + "'", iae);
    } catch (ClassCastException cce) {
        throw new ElementDescriptionException("Requested type is incompatible with field '" + field.getName() + "' of '" + parentObject.getClass().getCanonicalName() + "'", cce);
    }
}
Also used : ElementDescriptionException(ru.sbtqa.tag.pagefactory.exceptions.ElementDescriptionException)

Example 3 with ElementDescriptionException

use of ru.sbtqa.tag.pagefactory.exceptions.ElementDescriptionException in project page-factory-2 by sbtqa.

the class TableAbstract method selectRowByElement.

/**
 * Selects a row in a table by element value in a column. Checks all the fields with
 * the specified element name in order, until the search text is found in one of them.
 * If several elements are found with the search text, the element with the specified number will be selected.
 * If an element with the specified text and number is not found, an exception will be thrown.
 *
 * @param columnName column name
 * @param elementName element name
 * @param elementValue search text
 * @param serialNumber sequence number for selection of matches found (1, 2, 3 ...)
 * @param doubleClick {@code true}, if you need to make a double-click selection,
 * and {@code false} if single
 */
public void selectRowByElement(String columnName, String elementName, String elementValue, int serialNumber, boolean doubleClick) {
    int columnIndex = getColumnIndexByName(columnName);
    HtmlFindUtils findUtils = Environment.getFindUtils();
    try {
        List<BaseElement> cells = findUtils.findList(this, columnName);
        if (!cells.isEmpty()) {
            Class<?> cellType = cells.get(0).getClass();
            List<Field> fields = FieldUtils.getFieldsListWithAnnotation(cellType, ElementTitle.class);
            String elementXpath = fields.stream().filter(field -> field.getAnnotation(ElementTitle.class).value().equals(elementName)).findFirst().orElseThrow(() -> new AutotestError("No cell element with name found: " + elementName)).getAnnotation(FindBy.class).xpath();
            // to glue it under special conditions, excluding these symbols
            if (elementXpath.startsWith(".") || elementXpath.startsWith("(")) {
                elementXpath = elementXpath.replaceFirst("\\.?\\(?\\.?", "").replaceFirst("\\) ?\\[", "[");
            }
            String elementWithTextXpath = getColumnXpath(columnIndex) + elementXpath + format(TEXT_PART_XPATH_TEMPLATE, elementValue);
            List<WebElement> elements = this.findElements(By.xpath(elementWithTextXpath));
            click(ElementUtils.getElementByIndex(elements, serialNumber - 1), doubleClick);
        } else {
            throw new AutotestError("No column in the table was found: " + columnName);
        }
    } catch (ElementDescriptionException ex) {
        throw new AutotestError("Error finding element in table");
    } catch (ElementNotFoundException e) {
        throw new AutotestError(format("No cell element found with value: %s. Number: %s", elementValue, serialNumber));
    }
}
Also used : AutotestError(ru.sbtqa.tag.qautils.errors.AutotestError) ElementNotFoundException(ru.sbtqa.tag.pagefactory.exceptions.ElementNotFoundException) WebElement(org.openqa.selenium.WebElement) HtmlFindUtils(ru.sbtqa.tag.pagefactory.find.HtmlFindUtils) BaseElement(ru.sbtqa.tag.pagefactory.elements.BaseElement) Field(java.lang.reflect.Field) ElementDescriptionException(ru.sbtqa.tag.pagefactory.exceptions.ElementDescriptionException) ElementTitle(ru.sbtqa.tag.pagefactory.annotations.ElementTitle) FindBy(org.openqa.selenium.support.FindBy)

Example 4 with ElementDescriptionException

use of ru.sbtqa.tag.pagefactory.exceptions.ElementDescriptionException in project page-factory-2 by sbtqa.

the class HtmlFindUtils method findComplexElement.

private <T extends WebElement> ComplexElement findComplexElement(T context, String name, boolean wait) {
    ComplexElement<T> element = new ComplexElement<>(context, name, wait);
    try {
        for (; element.getCurrentPosition() < element.getElementPath().size(); element.setCurrentPosition(element.getCurrentPosition() + 1)) {
            Field field = getField(element.getElement(), element.getCurrentName());
            if (field == null) {
                throw new ElementSearchError("No element declared on page " + formErrorMessage(element));
            }
            findElement(field, element);
        }
        if (element.getCurrentPosition() > 0) {
            element.setCurrentPosition(element.getCurrentPosition() - 1);
        }
    } catch (AutotestError | IllegalArgumentException | ElementDescriptionException ex) {
        throw new ElementSearchError("Element " + formErrorMessage(element), ex);
    }
    return element;
}
Also used : AutotestError(ru.sbtqa.tag.qautils.errors.AutotestError) Field(java.lang.reflect.Field) ElementSearchError(ru.sbtqa.tag.pagefactory.exception.ElementSearchError) ElementDescriptionException(ru.sbtqa.tag.pagefactory.exceptions.ElementDescriptionException)

Aggregations

ElementDescriptionException (ru.sbtqa.tag.pagefactory.exceptions.ElementDescriptionException)4 Field (java.lang.reflect.Field)2 AutotestError (ru.sbtqa.tag.qautils.errors.AutotestError)2 WebElement (org.openqa.selenium.WebElement)1 FindBy (org.openqa.selenium.support.FindBy)1 ElementTitle (ru.sbtqa.tag.pagefactory.annotations.ElementTitle)1 BaseElement (ru.sbtqa.tag.pagefactory.elements.BaseElement)1 ElementSearchError (ru.sbtqa.tag.pagefactory.exception.ElementSearchError)1 ElementNotFoundException (ru.sbtqa.tag.pagefactory.exceptions.ElementNotFoundException)1 HtmlFindUtils (ru.sbtqa.tag.pagefactory.find.HtmlFindUtils)1