Search in sources :

Example 1 with ElementSearchError

use of ru.sbtqa.tag.pagefactory.exception.ElementSearchError in project page-factory-2 by sbtqa.

the class HtmlFindUtils method findList.

/**
 * Gets a list of elements by name or path
 * <p>
 * As a path, an enumeration of elements in a nested structure can be used
 * through the separator "{@code ->}", for example:
 * <ul>
 * <li> element in the block: Block{@code ->}Element
 * <li> element in the list with the sequence number 3: List{@code ->}3
 * </ul>
 * <p>
 *
 * @param <T> type of the returned element
 * @param context current context - where to start searching for an element.
 * Specify {@code null} if you need to perform a search in the context of
 * the current page, or an element to search for
 * @param name list name or path to it
 * @return Returns a list from a page by name or path
 * @throws ru.sbtqa.tag.pagefactory.exceptions.ElementDescriptionException Error get element by field
 */
public <T extends WebElement, E extends WebElement> List<E> findList(T context, String name) throws ElementDescriptionException {
    try {
        ComplexElement element;
        Field field;
        if (name.contains(ELEMENT_SEPARATOR)) {
            int lastSeparatorIndex = name.lastIndexOf(ELEMENT_SEPARATOR);
            element = findComplexElement(context, name.substring(0, lastSeparatorIndex), false);
            String listName = name.substring(lastSeparatorIndex + ELEMENT_SEPARATOR.length());
            field = getField(element.getElement(), listName);
        } else {
            element = new ComplexElement<>(context, name, false);
            field = getField(context, name);
        }
        if (field == null) {
            throw new ElementSearchError("Element list not found");
        }
        if (!field.getType().isAssignableFrom(List.class)) {
            throw new IncorrectElementTypeError(format("The element was found, " + "but it is not a list. The search was performed along the way: %s", name));
        }
        return (List<E>) getElementByField(element, field);
    } catch (ElementSearchError e) {
        return (List<E>) TestIdUtils.findListWithEmptyResult(name);
    }
}
Also used : Field(java.lang.reflect.Field) ElementSearchError(ru.sbtqa.tag.pagefactory.exception.ElementSearchError) List(java.util.List) IncorrectElementTypeError(ru.sbtqa.tag.pagefactory.exception.IncorrectElementTypeError)

Example 2 with ElementSearchError

use of ru.sbtqa.tag.pagefactory.exception.ElementSearchError 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)

Example 3 with ElementSearchError

use of ru.sbtqa.tag.pagefactory.exception.ElementSearchError in project page-factory-2 by sbtqa.

the class HtmlReflection method executeMethodByTitleInBlock.

/**
 * Execute method with one or more parameters inside of the given block
 * element
 *
 * @param blockPath block title, or a block chain string separated with
 * {@code ->} symbols
 * @param actionTitle title of the action to execute
 * @param parameters parameters that will be passed to method
 */
public void executeMethodByTitleInBlock(String blockPath, String actionTitle, Object... parameters) {
    try {
        HtmlElement block = ((HtmlFindUtils) Environment.getFindUtils()).find(blockPath, HtmlElement.class);
        executeMethodByTitle(block, actionTitle, parameters);
    } catch (ElementSearchError ex) {
        throw new ElementSearchError(format("Block not found by path '%s'", blockPath), ex);
    }
}
Also used : HtmlFindUtils(ru.sbtqa.tag.pagefactory.find.HtmlFindUtils) ElementSearchError(ru.sbtqa.tag.pagefactory.exception.ElementSearchError) HtmlElement(ru.yandex.qatools.htmlelements.element.HtmlElement)

Aggregations

ElementSearchError (ru.sbtqa.tag.pagefactory.exception.ElementSearchError)3 Field (java.lang.reflect.Field)2 List (java.util.List)1 IncorrectElementTypeError (ru.sbtqa.tag.pagefactory.exception.IncorrectElementTypeError)1 ElementDescriptionException (ru.sbtqa.tag.pagefactory.exceptions.ElementDescriptionException)1 HtmlFindUtils (ru.sbtqa.tag.pagefactory.find.HtmlFindUtils)1 AutotestError (ru.sbtqa.tag.qautils.errors.AutotestError)1 HtmlElement (ru.yandex.qatools.htmlelements.element.HtmlElement)1