Best Carina code snippet using com.qaprosoft.carina.core.foundation.webdriver.locator.internal.AbstractUIObjectListHandler
Source:ExtendedFieldDecorator.java
...31import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;32import org.openqa.selenium.support.pagefactory.FieldDecorator;33import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler;34import com.qaprosoft.carina.core.foundation.webdriver.locator.LocalizedAnnotations;35import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.AbstractUIObjectListHandler;36import com.qaprosoft.carina.core.foundation.webdriver.locator.internal.LocatingElementListHandler;37import com.qaprosoft.carina.core.gui.AbstractUIObject;38public class ExtendedFieldDecorator implements FieldDecorator39{40 private Logger LOGGER = Logger.getLogger(ExtendedFieldDecorator.class);41 protected ElementLocatorFactory factory;42 private WebDriver webDriver;43 public ExtendedFieldDecorator(ElementLocatorFactory factory, WebDriver webDriver)44 {45 this.factory = factory;46 this.webDriver = webDriver;47 }48 public Object decorate(ClassLoader loader, Field field)49 {50 if (!field.isAnnotationPresent(FindBy.class) /*Enable field decorator logic only in case of presence the FindBy annotation in the field*/ ||51 !(ExtendedWebElement.class.isAssignableFrom(field.getType()) || AbstractUIObject.class.isAssignableFrom(field.getType()) || isDecoratableList(field)) /*also verify that it is ExtendedWebElement or derived from AbstractUIObject or DecoratableList*/)52 {53 // returning null is ok in this method.54 return null;55 }56 ElementLocator locator;57 try58 {59 locator = factory.createLocator(field);60 } catch (Exception e)61 {62 LOGGER.error(e.getMessage(), e);63 return null;64 }65 if (locator == null)66 {67 return null;68 }69 if (ExtendedWebElement.class.isAssignableFrom(field.getType()))70 {71 return proxyForLocator(loader, field, locator);72 }73 if (AbstractUIObject.class.isAssignableFrom(field.getType()))74 {75 return proxyForAbstractUIObject(loader, field, locator);76 }77 else if (List.class.isAssignableFrom(field.getType()))78 {79 Type listType = getListType(field);80 if (ExtendedWebElement.class.isAssignableFrom((Class<?>) listType))81 {82 return proxyForListLocator(loader, field, locator);83 }84 else if (AbstractUIObject.class.isAssignableFrom((Class<?>) listType))85 {86 return proxyForListUIObjects(loader, field, locator);87 }88 else89 {90 return null;91 }92 } else93 {94 return null;95 }96 }97 private boolean isDecoratableList(Field field)98 {99 if (!List.class.isAssignableFrom(field.getType()))100 {101 return false;102 }103 Type listType = getListType(field);104 if (listType == null)105 {106 return false;107 }108 try109 {110 if (!(ExtendedWebElement.class.equals(listType) || AbstractUIObject.class.isAssignableFrom((Class<?>) listType)))111 {112 return false;113 }114 } catch (ClassCastException e)115 {116 return false;117 }118 return true;119 }120 protected ExtendedWebElement proxyForLocator(ClassLoader loader, Field field, ElementLocator locator)121 {122 InvocationHandler handler = new LocatingElementHandler(locator);123 WebElement proxy = (WebElement) Proxy.newProxyInstance(loader, new Class[]124 { WebElement.class, WrapsElement.class, Locatable.class }, handler);125 return new ExtendedWebElement(proxy, field.getName(), new LocalizedAnnotations(field).buildBy(), webDriver);126 }127 @SuppressWarnings("unchecked")128 protected <T extends AbstractUIObject> T proxyForAbstractUIObject(ClassLoader loader, Field field,129 ElementLocator locator)130 {131 InvocationHandler handler = new LocatingElementHandler(locator);132 WebElement proxy = (WebElement) Proxy.newProxyInstance(loader, new Class[]133 { WebElement.class, WrapsElement.class, Locatable.class }, handler);134 Class<? extends AbstractUIObject> clazz = (Class<? extends AbstractUIObject>) field.getType();135 T uiObject;136 try137 {138 uiObject = (T) clazz.getConstructor(WebDriver.class, SearchContext.class).newInstance(139 webDriver, proxy);140 } catch (NoSuchMethodException e)141 {142 LOGGER.error("Implement appropriate AbstractUIObject constructor for auto-initialization: "143 + e.getMessage());144 throw new RuntimeException(145 "Implement appropriate AbstractUIObject constructor for auto-initialization: "146 + e.getMessage(), e);147 } catch (Exception e)148 {149 LOGGER.error("Error creating UIObject: " + e.getMessage());150 throw new RuntimeException("Error creating UIObject: " + e.getMessage(), e);151 }152 uiObject.setName(field.getName());153 uiObject.setRootElement(proxy);154 return uiObject;155 }156 @SuppressWarnings("unchecked")157 protected List<ExtendedWebElement> proxyForListLocator(ClassLoader loader, Field field, ElementLocator locator)158 {159 InvocationHandler handler = new LocatingElementListHandler(locator, field.getName(), new LocalizedAnnotations(field).buildBy(), webDriver);160 List<ExtendedWebElement> proxies = (List<ExtendedWebElement>) Proxy.newProxyInstance(loader, new Class[]161 { List.class }, handler);162 return proxies;163 }164 @SuppressWarnings("unchecked")165 protected <T extends AbstractUIObject> List<T> proxyForListUIObjects(ClassLoader loader, Field field,166 ElementLocator locator)167 {168 InvocationHandler handler = new AbstractUIObjectListHandler<T>((Class<?>) getListType(field), webDriver,169 locator, field.getName());170 List<T> proxies = (List<T>) Proxy.newProxyInstance(loader, new Class[]171 { List.class }, handler);172 return proxies;173 }174 private Type getListType(Field field)175 {176 // Type erasure in Java isn't complete. Attempt to discover the generic177 // type of the list.178 Type genericType = field.getGenericType();179 if (!(genericType instanceof ParameterizedType))180 {181 return null;182 }...
Source:AbstractUIObjectListHandler.java
...25import org.openqa.selenium.WebElement;26import org.openqa.selenium.support.pagefactory.ElementLocator;27import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedFieldDecorator;28import com.qaprosoft.carina.core.gui.AbstractUIObject;29public class AbstractUIObjectListHandler<T extends AbstractUIObject> implements InvocationHandler30{31 private Class<?> clazz;32 private WebDriver webDriver;33 private final ElementLocator locator;34 private String name;35 private Logger LOGGER = Logger.getLogger(ExtendedFieldDecorator.class);36 public AbstractUIObjectListHandler(Class<?> clazz, WebDriver webDriver, ElementLocator locator, String name)37 {38 this.clazz = clazz;39 this.webDriver = webDriver;40 this.locator = locator;41 this.name = name;42 }43 @SuppressWarnings("unchecked")44 public Object invoke(Object object, Method method, Object[] objects) throws Throwable45 {46 List<WebElement> elements = locator.findElements();47 List<T> uIObjects = new ArrayList<T>();48 int index = 0;49 if (elements != null)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!!