Best Carina code snippet using com.qaprosoft.carina.core.foundation.webdriver.locator.internal.LocatingListHandler
Source:ExtendedFieldDecorator.java  
...36import org.slf4j.LoggerFactory;37import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedFindBy;38import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations;39import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.AbstractUIObjectListHandler;40import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.LocatingListHandler;41import com.qaprosoft.carina.core.gui.AbstractUIObject;42public class ExtendedFieldDecorator implements FieldDecorator {43    private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());44    protected ElementLocatorFactory factory;45    private WebDriver webDriver;46    47    public ExtendedFieldDecorator(ElementLocatorFactory factory, WebDriver webDriver) {48        this.factory = factory;49        this.webDriver = webDriver;50    }51    public Object decorate(ClassLoader loader, Field field) {52        if ((!field.isAnnotationPresent(FindBy.class) && !field.isAnnotationPresent(ExtendedFindBy.class))53                /*54                 * Enable field decorator logic only in case of55                 * presence the FindBy/FindByCarina/FindByAI annotation in the56                 * field57                 */ ||58                !(ExtendedWebElement.class.isAssignableFrom(field.getType()) || AbstractUIObject.class.isAssignableFrom(field.getType())59                        || isDecoratableList(field)) /*60                                                      * also verify that it is ExtendedWebElement or derived from AbstractUIObject or DecoratableList61                                                      */) {62            // returning null is ok in this method.63            return null;64        }65        ElementLocator locator;66        try {67            locator = factory.createLocator(field);68        } catch (Exception e) {69            LOGGER.error("Error while creating locator!", e);70            return null;71        }72        if (locator == null) {73            return null;74        }75        if (ExtendedWebElement.class.isAssignableFrom(field.getType())) {76            return proxyForLocator(loader, field, locator);77        }78        if (AbstractUIObject.class.isAssignableFrom(field.getType())) {79            return proxyForAbstractUIObject(loader, field, locator);80        } else if (List.class.isAssignableFrom(field.getType())) {81            Type listType = getListType(field);82            if (ExtendedWebElement.class.isAssignableFrom((Class<?>) listType)) {83                return proxyForListLocator(loader, field, locator);84            } else if (AbstractUIObject.class.isAssignableFrom((Class<?>) listType)) {85                return proxyForListUIObjects(loader, field, locator);86            } else {87                return null;88            }89        } else {90            return null;91        }92    }93    private boolean isDecoratableList(Field field) {94        if (!List.class.isAssignableFrom(field.getType())) {95            return false;96        }97        Type listType = getListType(field);98        if (listType == null) {99            return false;100        }101        try {102            if (!(ExtendedWebElement.class.equals(listType) || AbstractUIObject.class.isAssignableFrom((Class<?>) listType))) {103                return false;104            }105        } catch (ClassCastException e) {106            return false;107        }108        return true;109    }110    protected ExtendedWebElement proxyForLocator(ClassLoader loader, Field field, ElementLocator locator) {111        InvocationHandler handler = new LocatingElementHandler(locator);112        WebElement proxy = (WebElement) Proxy.newProxyInstance(loader, new Class[] { WebElement.class, WrapsElement.class, Locatable.class },113                handler);114        /**115         * Questionable place - called ExtendedWebElement constructor with no initializing searchContext116         */117        return new ExtendedWebElement(proxy, field.getName(),118                field.isAnnotationPresent(FindBy.class) || field.isAnnotationPresent(ExtendedFindBy.class)? new LocalizedAnnotations(field).buildBy() : null);119    }120    @SuppressWarnings("unchecked")121    protected <T extends AbstractUIObject> T proxyForAbstractUIObject(ClassLoader loader, Field field,122            ElementLocator locator) {123        InvocationHandler handler = new LocatingElementHandler(locator);124        WebElement proxy = (WebElement) Proxy.newProxyInstance(loader, new Class[] { WebElement.class, WrapsElement.class, Locatable.class },125                handler);126        Class<? extends AbstractUIObject> clazz = (Class<? extends AbstractUIObject>) field.getType();127        T uiObject;128        try {129            uiObject = (T) clazz.getConstructor(WebDriver.class, SearchContext.class).newInstance(130                    webDriver, proxy);131        } catch (NoSuchMethodException e) {132            throw new RuntimeException(133                    "Implement appropriate AbstractUIObject constructor for auto-initialization!", e);134        } catch (Exception e) {135            throw new RuntimeException("Error creating UIObject!", e);136        }137        uiObject.setName(field.getName());138        uiObject.setRootElement(proxy);139        uiObject.setRootBy(getLocatorBy(locator));140        return uiObject;141    }142    @SuppressWarnings("unchecked")143    protected List<ExtendedWebElement> proxyForListLocator(ClassLoader loader, Field field, ElementLocator locator) {144        InvocationHandler handler = new LocatingListHandler(loader, locator, field);145        List<ExtendedWebElement> proxies = (List<ExtendedWebElement>) Proxy.newProxyInstance(loader, new Class[] { List.class }, handler);146        return proxies;147    }148    @SuppressWarnings("unchecked")149    protected <T extends AbstractUIObject> List<T> proxyForListUIObjects(ClassLoader loader, Field field,150            ElementLocator locator) {151        InvocationHandler handler = new AbstractUIObjectListHandler<T>((Class<?>) getListType(field), webDriver,152                locator, field.getName());153        List<T> proxies = (List<T>) Proxy.newProxyInstance(loader, new Class[] { List.class }, handler);154        return proxies;155    }156    private Type getListType(Field field) {157        // Type erasure in Java isn't complete. Attempt to discover the generic158        // type of the list....Source:LocatingListHandler.java  
...28import org.openqa.selenium.internal.WrapsElement;29import org.openqa.selenium.support.pagefactory.ElementLocator;30import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;31import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations;32public class LocatingListHandler implements InvocationHandler {33    private final ElementLocator locator;34    private String name;35    private By by;36    private final ClassLoader loader;37    public LocatingListHandler(ClassLoader loader, ElementLocator locator, Field field){38        this.loader = loader;39        this.locator = locator;40        this.name = field.getName();41        this.by = new LocalizedAnnotations(field).buildBy();42    }43    public Object invoke(Object object, Method method, Object[] objects) throws Throwable {44		// Hotfix for huge and expected regression in carina: we lost managed45		// time delays with lists manipulations46		// Temporary we are going to restore explicit waiter here with hardcoded47		// timeout before we find better solution48		// Pros: super fast regression issue which block UI execution49		// Cons: there is no way to manage timeouts in this places50//    	if (!waitUntil(ExpectedConditions.or(ExpectedConditions.presenceOfElementLocated(by),51//    			ExpectedConditions.visibilityOfElementLocated(by)))) {...LocatingListHandler
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.LocatingListHandler;2import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.LocatingListHandler;3import org.openqa.selenium.By;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.WebElement;6import java.lang.reflect.Proxy;7import java.util.List;8public class LocatingListHandlerExample {9    public static void main(String[] args) {10        WebDriver driver = null;11        By by = null;12        List<WebElement> elements = (List<WebElement>) Proxy.newProxyInstance(13                WebElement.class.getClassLoader(),14                new Class[] {List.class},15                new LocatingListHandler(driver, by));16    }17}18import com.qaprosoft.carina.core.foundation.webdriver.locator.LocatingListHandler;19import org.openqa.selenium.By;20import org.openqa.selenium.WebDriver;21import org.openqa.selenium.WebElement;22import java.lang.reflect.Proxy;23import java.util.List;24public class LocatingListHandlerExample {25    public static void main(String[] args) {26        WebDriver driver = null;27        By by = null;28        List<WebElement> elements = (List<WebElement>) Proxy.newProxyInstance(29                WebElement.class.getClassLoader(),30                new Class[] {List.class},31                new LocatingListHandler(driver, by));32    }33}34import com.qaprosoft.carina.core.foundation.webdriver.locator.LocatingListHandler;35import org.openqa.selenium.By;36import org.openqa.selenium.WebDriver;37import org.openqa.selenium.WebElement;38import java.lang.reflect.Proxy;39import java.util.List;40public class LocatingListHandlerExample {41    public static void main(String[] args) {42        WebDriver driver = null;43        By by = null;44        List<WebElement> elements = (List<WebElement>) Proxy.newProxyInstance(45                WebElement.class.getClassLoader(),46                new Class[] {List.class},47                new LocatingListHandler(driver, by));48    }49}50import com.qaprosoft.carina.core.foundation.webdriver.locator.LocatingListHandler;51import org.openqa.selenium.By;52import org.openqa.selenium.WebDriver;53importLocatingListHandler
Using AI Code Generation
1package com.qaprosoft.carina.core.foundation.webdriver.locator;2import java.util.ArrayList;3import java.util.List;4import org.openqa.selenium.By;5import org.openqa.selenium.SearchContext;6import org.openqa.selenium.WebElement;7import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.LocatingListHandler;8public class ExtendedBy extends By {9    private final By by;10    private final String description;11    public ExtendedBy(By by, String description) {12        this.by = by;13        this.description = description;14    }15    public By getBy() {16        return by;17    }18    public String getDescription() {19        return description;20    }21    public List<WebElement> findElements(SearchContext context) {22        return (List<WebElement>) new LocatingListHandler(by, description).invoke(context, null);23    }24    public String toString() {25        return description;26    }27    public static ExtendedBy extendedBy(By by, String description) {28        return new ExtendedBy(by, description);29    }30    public static List<ExtendedBy> extendedBy(List<By> byList, String description) {31        List<ExtendedBy> extendedByList = new ArrayList<ExtendedBy>();32        for (By by : byList) {33            extendedByList.add(new ExtendedBy(by, description));34        }35        return extendedByList;36    }37}38package com.qaprosoft.carina.core.foundation.webdriver.locator;39import java.util.List;40import org.openqa.selenium.By;41import org.openqa.selenium.SearchContext;42import org.openqa.selenium.WebElement;43public class ExtendedBy extends By {44    private final By by;45    private final String description;46    public ExtendedBy(By by, String description) {47        this.by = by;48        this.description = description;49    }50    public By getBy() {51        return by;52    }53    public String getDescription() {54        return description;55    }56    public List<WebElement> findElements(SearchContext context) {57        return by.findElements(context);58    }59    public String toString() {60        return description;61    }62    public static ExtendedBy extendedBy(By by, String description) {63        return new ExtendedBy(by, description);64    }65    public static List<ExtendedBy> extendedBy(List<By> byList, String description) {LocatingListHandler
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.LocatingListHandler;2import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.LocatingListHandler;3import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.LocatingListHandler;4import org.openqa.selenium.By;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.remote.RemoteWebDriver;7import org.openqa.selenium.support.pagefactory.ElementLocator;8import java.lang.reflect.InvocationHandler;9import java.lang.reflect.Method;10import java.lang.reflect.Proxy;11import java.util.List;12public class ListHandler implements InvocationHandler {13    private final RemoteWebDriver driver;14    private final ElementLocator locator;15    public ListHandler(RemoteWebDriver driver, ElementLocator locator) {16        this.driver = driver;17        this.locator = locator;18    }19    public static List<WebElement> createList(RemoteWebDriver driver, ElementLocator locator) {20        InvocationHandler handler = new ListHandler(driver, locator);21        List<WebElement> elements = (List<WebElement>) Proxy.newProxyInstance(22                LocatingListHandler.class.getClassLoader(), new Class[] { List.class },23                handler);24        return elements;25    }26    public Object invoke(Object object, Method method, Object[] objects) throws Throwable {27        List<WebElement> elements = locator.findElements();28        try {29            return method.invoke(elements, objects);30        } catch (Exception e) {31            throw e.getCause();32        }33    }34}35import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.ListHandler;36import org.openqa.selenium.By;37import org.openqa.selenium.WebElement;38import org.openqa.selenium.remote.RemoteWebDriver;39import org.openqa.selenium.support.pagefactory.ElementLocator;40import java.util.List;41public class LocatingListHandler implements ElementLocator {42    private final RemoteWebDriver driver;43    private final By by;44    public LocatingListHandler(RemoteWebDriver driver, By by) {45        this.driver = driver;46        this.by = by;47    }48    public List<WebElement> findElements() {49        return ListHandler.createList(driver, this);50    }51    public WebElement findElement() {52        throw new UnsupportedOperationException(53                "Cannot locate a single element using a list locator");54    }55    public boolean isStale() {56        return false;57    }58    public By getBy() {LocatingListHandler
Using AI Code Generation
1List<WebElement> elements = handler.getWrappedList();2List<WebElement> elements = handler.getWrappedList();3public class LocatingListHandler implements InvocationHandler {4    private final WebDriver driver;5    private final By locator;6    private List<WebElement> elements;7    public LocatingListHandler(WebDriver driver, By locator) {8        this.driver = driver;9        this.locator = locator;10    }11    public List<WebElement> getWrappedList() {12        return (List<WebElement>) Proxy.newProxyInstance(LocatingListHandler.class.getClassLoader(),13                new Class[]{List.class}, this);14    }15    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {16        if ("get".equals(method.getName())) {17            if (elements == null) {18                elements = driver.findElements(locator);19            }20            return elements.get((Integer) args[0]);21        } else if ("size".equals(method.getName())) {22            if (elements == null) {23                elements = driver.findElements(locator);24            }25            return elements.size();26        } else if ("clear".equals(method.getName())) {27            if (elements == null) {28                elements = driver.findElements(locator);29            }30            elements.clear();31            return null;32        } else if ("addAll".equals(method.getName())) {33            if (elements == null) {34                elements = driver.findElements(locator);35            }36            return elements.addAll((Collection<? extends WebElement>) args[0]);37        } else if ("removeAll".equals(method.getName())) {38            if (elements == null) {39                elements = driver.findElements(locator);40            }41            return elements.removeAll((Collection<?>) args[0]);42        } else if ("containsAll".equals(method.getName())) {43            if (elements == null) {44                elements = driver.findElements(locator);45            }46            return elements.containsAll((Collection<?>) args[0]);47        } else if ("retainAll".equals(method.getName())) {48            if (elements == null) {49                elements = driver.findElements(locator);50            }Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
