Search in sources :

Example 1 with TypifiedElementListNamedProxyHandler

use of ru.yandex.qatools.htmlelements.loader.decorator.proxyhandlers.TypifiedElementListNamedProxyHandler in project page-factory-2 by sbtqa.

the class CustomHtmlElementDecorator method decorateTypifiedElementList.

@Override
protected <T extends TypifiedElement> List<T> decorateTypifiedElementList(ClassLoader loader, Field field) {
    final Class<T> elementClass = (Class<T>) HtmlElementUtils.getGenericParameterClass(field);
    final ElementLocator locator = factory.createLocator(field);
    final String name = getElementName(field);
    InvocationHandler handler = new TypifiedElementListNamedProxyHandler(elementClass, locator, name) {

        @Override
        public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
            if ("toString".equals(method.getName())) {
                return name;
            }
            List<T> elements = new LinkedList<>();
            int elementNumber = 0;
            for (WebElement element : locator.findElements()) {
                String newName = String.format("%s [%d]", name, elementNumber++);
                elements.add(createTypifiedElement(elementClass, element, newName));
            }
            try {
                return method.invoke(elements, objects);
            } catch (InvocationTargetException e) {
                throw new AutotestError("Error initializing elements.", e);
            }
        }
    };
    return createTypifiedElementListProxy(loader, handler);
}
Also used : AutotestError(ru.sbtqa.tag.qautils.errors.AutotestError) ElementLocator(org.openqa.selenium.support.pagefactory.ElementLocator) Method(java.lang.reflect.Method) WebElement(org.openqa.selenium.WebElement) InvocationHandler(java.lang.reflect.InvocationHandler) LinkedList(java.util.LinkedList) InvocationTargetException(java.lang.reflect.InvocationTargetException) TypifiedElementListNamedProxyHandler(ru.yandex.qatools.htmlelements.loader.decorator.proxyhandlers.TypifiedElementListNamedProxyHandler)

Aggregations

InvocationHandler (java.lang.reflect.InvocationHandler)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 LinkedList (java.util.LinkedList)1 WebElement (org.openqa.selenium.WebElement)1 ElementLocator (org.openqa.selenium.support.pagefactory.ElementLocator)1 AutotestError (ru.sbtqa.tag.qautils.errors.AutotestError)1 TypifiedElementListNamedProxyHandler (ru.yandex.qatools.htmlelements.loader.decorator.proxyhandlers.TypifiedElementListNamedProxyHandler)1