How to use Interface FieldDecorator class of org.openqa.selenium.support.pagefactory package

Best Selenium code snippet using org.openqa.selenium.support.pagefactory.Interface FieldDecorator

Source:CustomElementFieldDecorator.java Github

copy

Full Screen

1package webelement.customElementsDecorator;2import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler;3import webelement.customElements.concreteElements.WebPageElement;4import webelement.customElements.superElements.CustomWebElement;5import webelement.modules.WebElementTransformer;6import net.sf.cglib.proxy.Enhancer;7import net.sf.cglib.proxy.MethodInterceptor;8import org.openqa.selenium.By;9import org.openqa.selenium.SearchContext;10import org.openqa.selenium.WebDriver;11import org.openqa.selenium.support.FindBy;12import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;13import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;14import org.openqa.selenium.support.pagefactory.ElementLocator;15import org.openqa.selenium.support.pagefactory.FieldDecorator;16import java.lang.reflect.Field;17import java.lang.reflect.InvocationHandler;18import java.lang.reflect.Proxy;19import java.util.List;20/*21 * Good sources:22 * http://www.alechenninger.com/2014/07/a-case-study-of-javas-dynamic-proxies_14.html23 * http://www.mograblog.com/2013/08/extending-selenium-in-java.html24 */25/*26 * Short summary:27 * The page factory runs through all fields of a class an calls the "decorate()" method of the "FieldDecorator" interface.28 * The "decorate()" method has to return an object which has the correct type.29 * 30 * For that it needs the locator of the field so that the webelement can be identified on the website.31 * 32 * It should work like this:33 * 34 * Page class should be instantiated -> PageFactory.init() -> decorate() for each field35 * choice 1: decorate for a default webelement: DefaultFieldDecorator is used for default behavior36 * choice 2: decorate has create proper custom webelement -> getLocator() to be able do get the webelement37 * -> getEnhancedObject() to add callback method to lazy initialize and configure the custom webelement38 * -> getElementHandler(), which implements the callback method to add callback method to lazy initialize and configure the custom webelement.39 * */40/**41 * Idea and code (partly) was taken from: http://www.mograblog.com/2013/08/extending-selenium-in-java.html.42 * <p>43 * An implementation of a FieldDecorator to enable the usage of custom webelements via a page factory.44 * Custom webelements will be created via lazy initialisation.45 **/46public class CustomElementFieldDecorator implements FieldDecorator {47 /**48 * The decorator which is used when a default WebElement is used and the default behavior can be used.49 **/50 private final DefaultFieldDecorator defaultFieldDecorator;51 /**52 * The search context for the (custom) webelement. Mostly the webdriver.53 **/54 private final SearchContext searchContext;55 /**56 * The webdriver.57 **/58 private final WebDriver webDriver;59 /**60 * The constructor. It constructs.61 *62 * @param searchContext The search context for the (custom) webelement. Mostly just a webdriver object.63 * Used to find webelements on a webpage.64 * @param webDriver The webDriver which will be used to create the webelement.65 **/66 public CustomElementFieldDecorator(SearchContext searchContext, WebDriver webDriver) {67 this.searchContext = searchContext;68 this.webDriver = webDriver;69 defaultFieldDecorator = new DefaultFieldDecorator(new DefaultElementLocatorFactory(searchContext));70 }71 /**72 * This method is called by the Selenium PageFactory on all fields to decide how to decorate the field.73 *74 * @param loader The class loader that was used for the page object75 * @param field The field which should be decorated. Should be an FindBy annotated (custom) webelement.76 * @return Value to decorate the field with.77 **/78 @Override79 public Object decorate(ClassLoader loader, Field field) {80 // If it is a custom annotated webelement, then ensure proper initialisation via the adding of the callback method81 if (CustomWebElement.class.isAssignableFrom(field.getType()) && field.isAnnotationPresent(FindBy.class)) {82 return getEnhancedObject(field.getType(), getElementHandler(field), field.getAnnotation(FindBy.class));83 }84 // If it is a normal webelement, then use the default FieldDecorator implementation85 else {86 return defaultFieldDecorator.decorate(loader, field);87 }88 }89 /**90 * Creates the class with the callback method. The callback method will be called when a method is called on91 * the given field object (e.g. a click() method call on a button).92 *93 * @return The class which contains the callback method.94 **/95 private CustomElementLocator getElementHandler(Field field) {96 return new CustomElementLocator(getLocator(field));97 }98 /**99 * Returns the element handler for the field which melds the field and the locator (aka FindBy) together for further100 * usage. An ElementLocator locator can find a webelement on a webpage without any parameters since all the needed information101 * is already there.102 *103 * @param field The annotated field from which the element locator will be created.104 * @return The element locator object.105 **/106 private ElementLocator getLocator(Field field) {107 return new DefaultElementLocatorFactory(searchContext).createLocator(field);108 }109 /**110 * Enhances the class to call a specific method callback when a method of that class is called. Example: A button is111 * clicked -> Since the class might be a custom webelement, the callback method is called to handle the112 * initialisation of the custom webelement.113 *114 * @param clzz The class which should be enhanced with the callback method (e.g. a custom button). If a method in115 * that class is called, the callback method will be triggered.116 * @param methodInterceptor The class which implements the callback method.117 * @param locator The locator which was used to identify the webelement via the FindBy annotation.118 **/119 private Object getEnhancedObject(Class<?> clzz, MethodInterceptor methodInterceptor, FindBy locator) {120 Enhancer e = new Enhancer();121 WebElementTransformer transformer = new WebElementTransformer();122 e.setSuperclass(clzz);123 e.setCallback(methodInterceptor);124 return e.create(new Class[]{WebDriver.class, By.class}, new Object[]{webDriver, transformer.transformFindByToBy(locator)});125 }126}...

Full Screen

Full Screen

Source:SmartFieldDecorator.java Github

copy

Full Screen

1package net.thucydides.core.annotations.locators;2import com.google.common.collect.ImmutableList;3import net.thucydides.core.annotations.findby.FindBy;4import net.thucydides.core.pages.PageObject;5import net.thucydides.core.pages.WebElementFacade;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.internal.Locatable;9import org.openqa.selenium.internal.WrapsElement;10import org.openqa.selenium.support.FindBys;11import org.openqa.selenium.support.pagefactory.ElementLocator;12import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;13import org.openqa.selenium.support.pagefactory.FieldDecorator;14import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler;15import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler;16import java.lang.annotation.Annotation;17import java.lang.reflect.*;18import java.util.List;19public class SmartFieldDecorator implements FieldDecorator {20 protected ElementLocatorFactory factory;21 protected WebDriver driver;22 protected PageObject pageObject;23 public SmartFieldDecorator(ElementLocatorFactory factory, WebDriver driver,24 PageObject pageObject) {25 this.driver = driver;26 this.factory = factory;27 this.pageObject = pageObject;28 }29 public Object decorate(ClassLoader loader, Field field) {30 if (!(WebElement.class.isAssignableFrom(field.getType()) || isDecoratableList(field))) {31 return null;32 }33 ElementLocator locator = factory.createLocator(field);34 if (locator == null) {35 return null;36 }37 Class<?> fieldType = field.getType();38 if (WebElement.class.isAssignableFrom(fieldType)) {39 return proxyForLocator(loader, fieldType, locator);40 } else if (List.class.isAssignableFrom(fieldType)) {41 Class<?> erasureClass = getErasureClass(field);42 return proxyForListLocator(loader, erasureClass, locator);43 } else {44 return null;45 }46 }47 @SuppressWarnings("rawtypes")48 private Class getErasureClass(Field field) {49 Type genericType = field.getGenericType();50 if (!(genericType instanceof ParameterizedType)) {51 return null;52 }53 return (Class) ((ParameterizedType) genericType).getActualTypeArguments()[0];54 }55 @SuppressWarnings("rawtypes")56 private boolean isDecoratableList(Field field) {57 if (!List.class.isAssignableFrom(field.getType())) {58 return false;59 }60 Class erasureClass = getErasureClass(field);61 if (erasureClass == null || !WebElement.class.isAssignableFrom(erasureClass)) {62 return false;63 }64 return annotatedByLegalFindByAnnotation(field);65 }66 private final static List<Class<? extends Annotation>> LEGAL_ANNOTATIONS67 = ImmutableList.of(FindBy.class,68 org.openqa.selenium.support.FindBy.class,69 FindBys.class);70 private boolean annotatedByLegalFindByAnnotation(Field field) {71 for (Annotation annotation : field.getAnnotations()) {72 if (LEGAL_ANNOTATIONS.contains(annotation.annotationType())) {73 return true;74 }75 }76 return false;77 }78 /* Generate a type-parameterized locator proxy for the element in question. */79 @SuppressWarnings("unchecked")80 protected <T> T proxyForLocator(ClassLoader loader, Class<T> interfaceType, ElementLocator locator) {81 InvocationHandler handler;82 T proxy = null;83 if (WebElementFacade.class.isAssignableFrom(interfaceType)) {84 handler = new SmartElementHandler(interfaceType, locator, driver, pageObject.waitForTimeoutInMilliseconds());85 proxy = (T) Proxy.newProxyInstance(loader, new Class[]{interfaceType}, handler);86 } else {87 handler = new LocatingElementHandler(locator);88 proxy = (T) Proxy.newProxyInstance(loader,89 new Class[]{WebElement.class, WrapsElement.class, Locatable.class}, handler);90 }91 return proxy;92 }93 /* generates a proxy for a list of elements to be wrapped. */94 @SuppressWarnings("unchecked")95 protected <T> List<T> proxyForListLocator(ClassLoader loader, Class<T> interfaceType, ElementLocator locator) {96 InvocationHandler handler = new LocatingElementListHandler(locator);97 List<T> proxy;98 proxy = (List<T>) Proxy.newProxyInstance(99 loader, new Class[]{List.class}, handler);100 return proxy;101 }102}...

Full Screen

Full Screen

Source:FieldDecoratorImpl.java Github

copy

Full Screen

12package com.mh.ta.core.config.driver.services.internal;34import java.lang.reflect.Field;5import java.lang.reflect.InvocationHandler;6import java.lang.reflect.ParameterizedType;7import java.lang.reflect.Proxy;8import java.lang.reflect.Type;9import java.util.List;1011import org.openqa.selenium.WebElement;12import org.openqa.selenium.internal.Locatable;13import org.openqa.selenium.internal.WrapsElement;14import org.openqa.selenium.support.FindAll;15import org.openqa.selenium.support.FindBy;16import org.openqa.selenium.support.FindBys;17import org.openqa.selenium.support.pagefactory.ElementLocator;18import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;19import org.openqa.selenium.support.pagefactory.FieldDecorator;2021import com.mh.ta.core.config.GuiceInjectFactory;22import com.mh.ta.core.config.annotation.FindByUntil;23import com.mh.ta.core.config.driver.services.ElementLocatorFactoryImpl;24import com.mh.ta.core.config.element.Element;25import com.mh.ta.core.exception.TestContextException;2627/**28 * @author minhhoang29 *30 */31public class FieldDecoratorImpl implements FieldDecorator {3233 protected ElementLocatorFactory factory;3435 public FieldDecoratorImpl() {36 factory = GuiceInjectFactory.instance().createObjectInstance(ElementLocatorFactoryImpl.class);37 }3839 @Override40 public Object decorate(ClassLoader loader, Field field) {4142 if (!elementField(field)) {43 return null;44 }4546 if (!field.getType().isInterface()) {47 throw new TestContextException("Field element is not interface type. Please check field " + field.getName()48 + " in " + field.getDeclaringClass());49 }5051 ElementLocator locator = factory.createLocator(field);52 if (locator == null) {53 return null;54 }5556 Class<?> fieldType = field.getType();57 if (WebElement.class.equals(fieldType)) {58 fieldType = Element.class;59 }6061 if (WebElement.class.isAssignableFrom(fieldType)) {62 return proxyForLocator(loader, fieldType, locator);63 } else if (List.class.isAssignableFrom(fieldType)) {64 Class<?> erasureClass = getErasureClass(field);65 return proxyForListLocator(loader, erasureClass, locator);66 } else {67 return null;68 }69 }7071 private boolean elementField(Field field) {72 boolean isElementField = WebElement.class.isAssignableFrom(field.getType());73 boolean isListField = isDecoratableList(field);74 return isElementField || isListField;75 }7677 private Class<?> getErasureClass(Field field) {78 // Type erasure in Java isn't complete. Attempt to discover the generic79 // interfaceType of the list.80 Type genericType = field.getGenericType();81 if (!(genericType instanceof ParameterizedType)) {82 return null;83 }84 return (Class<?>) ((ParameterizedType) genericType).getActualTypeArguments()[0];85 }8687 private boolean isDecoratableList(Field field) {88 if (!List.class.isAssignableFrom(field.getType())) {89 return false;90 }9192 Class<?> erasureClass = getErasureClass(field);93 if (erasureClass == null) {94 return false;95 }9697 if (!WebElement.class.isAssignableFrom(erasureClass)) {98 return false;99 } 100 return !isNullAnnotationPageObjectFind(field);101 }102103 private boolean isNullAnnotationPageObjectFind(Field field) {104 boolean isFindBy = field.getAnnotation(FindBy.class) == null;105 boolean isFindBys = field.getAnnotation(FindBys.class) == null;106 boolean isFindAll = field.getAnnotation(FindAll.class) == null;107 boolean isFindByUntil = field.getAnnotation(FindByUntil.class) == null;108 return isFindBy && isFindBys && isFindAll && isFindByUntil;109 }110111 protected <T> T proxyForLocator(ClassLoader loader, Class<T> interfaceType, ElementLocator locator) {112 InvocationHandler handler = new ElementHandler(interfaceType, locator);113114 T proxy;115 proxy = interfaceType.cast(Proxy.newProxyInstance(loader,116 new Class[] { interfaceType, WebElement.class, WrapsElement.class, Locatable.class }, handler));117 return proxy;118 }119120 @SuppressWarnings("unchecked")121 protected <T> List<T> proxyForListLocator(ClassLoader loader, Class<T> interfaceType, ElementLocator locator) {122 InvocationHandler handler = new ElementListHandler(interfaceType, locator);123 List<T> proxy;124 proxy = (List<T>) Proxy.newProxyInstance(loader, new Class[] { List.class }, handler);125 return proxy;126 }127128} ...

Full Screen

Full Screen

Source:MyPageFactory.java Github

copy

Full Screen

1package pageObject;23import java.lang.reflect.Constructor;4import java.lang.reflect.Field;5import java.lang.reflect.InvocationTargetException;6import java.lang.reflect.ParameterizedType;7import java.util.ArrayList;8import java.util.List;910import org.openqa.selenium.WebDriver;11import org.openqa.selenium.WebElement;12import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;13import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;14import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;15import org.openqa.selenium.support.pagefactory.FieldDecorator;1617public class MyPageFactory{18 /**19 * Initialize the pageObject with input pageObject class20 * @param <T>21 * @param driver22 * @param pageClassToProxy23 * @return24 */25 public static <T> T initElements(WebDriver driver, Class<T> pageClassToProxy) {26 T page = instantiatePage(driver, pageClassToProxy);27 initElements(driver, page);28 return page;29 }30 /**31 * Initialize all the field of one page object 32 * with @FindBy tag whose type are "MyWebElement"33 * @param driver34 * @param page35 */36 public static void initElements(WebDriver driver, Object page) {37 final WebDriver driverRef = driver;38 ElementLocatorFactory factoryRef = new DefaultElementLocatorFactory(driverRef);39 FieldDecorator decorator = new DefaultFieldDecorator(factoryRef);40 Class<?> proxyIn = page.getClass();41 while (proxyIn != Object.class) {42 proxyFields(decorator, page, proxyIn);43 proxyIn = proxyIn.getSuperclass();44 }45 }46 /**47 * The logic to initialize the field whose type are MyWebElement48 * @param decorator49 * @param page50 * @param proxyIn51 */5253 @SuppressWarnings("unchecked")54 private static void proxyFields(FieldDecorator decorator, Object page, Class<?> proxyIn) {55 Field[] fields = proxyIn.getDeclaredFields();56 for (Field field : fields) {57 58 Object value = decorator.decorate(page.getClass().getClassLoader(), field);59 60 // Cast the value to probable interface61 Object castValue = null;62 String fieldName = field.getType().getName();63 if(fieldName.contains("MyWebElement")){ // private WebElement xxx;64 castValue = new MyElement((WebElement)value);65 }else if(field.getType().getName().contains("WebElement")){ // private MyWebElement xxx;66 castValue = (WebElement)value;67 }else if(fieldName.contains("java.util.List")){ // private List<E> xxx;68 ParameterizedType pt = (ParameterizedType)field.getGenericType();69 String parameterName = pt.getActualTypeArguments()[0].toString();70 if (parameterName.contains("WebElement")){ // private List<WebElement> xxx;71 castValue = (List<WebElement>)value;72 }else if(parameterName.contains("MyWebElement")){ // private List<MyWebElement> xxx;73 List<WebElement> elements = (List<WebElement>)value;74 List<MyWebElement> myelements = new ArrayList<MyWebElement>();75 for(WebElement element : elements){76 myelements.add(new MyElement(element));77 }78 castValue = myelements;79 }80 }81 82 83 if (value != null && castValue != null) {84 try {85 field.setAccessible(true);86 field.set(page, castValue);87 } catch (IllegalAccessException e) {88 throw new RuntimeException(e);89 }90 }91 }92 }93 /**94 * Calling constructor to initialize page instace95 * @param <T>96 * @param driver97 * @param pageClassToProxy98 * @return99 */100 private static <T> T instantiatePage(WebDriver driver, Class<T> pageClassToProxy) {101 try {102 try {103 Constructor<T> constructor = pageClassToProxy.getConstructor(WebDriver.class);104 return constructor.newInstance(driver);105 } catch (NoSuchMethodException e) {106 return pageClassToProxy.newInstance();107 }108 } catch (InstantiationException e) {109 throw new RuntimeException(e);110 } catch (IllegalAccessException e) {111 throw new RuntimeException(e);112 } catch (InvocationTargetException e) {113 throw new RuntimeException(e);114 }115 } ...

Full Screen

Full Screen

Source:ElementDecorator.java Github

copy

Full Screen

1package com.ita.edu.teachua.ui.pagefactory;2import com.ita.edu.teachua.ui.elements.base_element.BaseElement;3import com.ita.edu.teachua.ui.elements.base_element.Element;4import com.ita.edu.teachua.ui.elements.base_element.ImplementedBy;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.WrapsElement;7import org.openqa.selenium.interactions.Locatable;8import org.openqa.selenium.support.FindAll;9import org.openqa.selenium.support.FindBy;10import org.openqa.selenium.support.FindBys;11import org.openqa.selenium.support.pagefactory.ElementLocator;12import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;13import org.openqa.selenium.support.pagefactory.FieldDecorator;14import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler;15import java.lang.reflect.*;16import java.util.List;17public class ElementDecorator implements FieldDecorator {18 private ElementLocatorFactory factory;19 public ElementDecorator(ElementLocatorFactory factory) {20 this.factory = factory;21 }22 @Override23 public Object decorate(ClassLoader loader, Field field) {24 if (!(WebElement.class.isAssignableFrom(field.getType()) || isDecoratableList(field))) {25 return null;26 }27 if (field.getDeclaringClass() == BaseElement.class) {28 return null;29 }30 ElementLocator locator = factory.createLocator(field);31 if (locator == null) {32 return null;33 }34 Class<?> fieldType = field.getType();35 if (WebElement.class.equals(fieldType)) {36 fieldType = Element.class;37 }38 if (WebElement.class.isAssignableFrom(fieldType)) {39 return proxyForLocator(loader, fieldType, locator);40 } else if (List.class.isAssignableFrom(fieldType)) {41 Class<?> erasureClass = getErasureClass(field);42 return proxyForListLocator(loader, erasureClass, locator);43 } else {44 return null;45 }46 }47 private Class<?> getErasureClass(Field field) {48 Type genericType = field.getGenericType();49 if (!(genericType instanceof ParameterizedType)) {50 return null;51 }52 return (Class<?>) ((ParameterizedType) genericType).getActualTypeArguments()[0];53 }54 private boolean isDecoratableList(Field field) {55 if (!List.class.isAssignableFrom(field.getType())) {56 return false;57 }58 Class<?> erasureClass = getErasureClass(field);59 if (erasureClass == null) {60 return false;61 }62 if (!WebElement.class.isAssignableFrom(erasureClass)) {63 return false;64 }65 if (field.getAnnotation(FindBy.class) == null && field.getAnnotation(FindBys.class) == null &&66 field.getAnnotation(FindAll.class) == null) {67 return false;68 }69 return true;70 }71 protected <T> T proxyForLocator(ClassLoader loader, Class<T> interfaceType, ElementLocator locator) {72 InvocationHandler handler = new ElementHandler(interfaceType, locator);73 T proxy;74 proxy = interfaceType.cast(Proxy.newProxyInstance(75 loader, new Class[]{interfaceType, WebElement.class, WrapsElement.class, Locatable.class}, handler));76 return proxy;77 }78 @SuppressWarnings("unchecked")79 protected <T> List<T> proxyForListLocator(ClassLoader loader, Class<T> interfaceType, ElementLocator locator) {80 InvocationHandler handler;81 if (interfaceType.getAnnotation(ImplementedBy.class) != null) {82 handler = new ElementListHandler(interfaceType, locator);83 } else {84 handler = new LocatingElementListHandler(locator);85 }86 List<T> proxy;87 proxy = (List<T>) Proxy.newProxyInstance(88 loader, new Class[]{List.class}, handler);89 return proxy;90 }91}...

Full Screen

Full Screen

Source:PageFactory.java Github

copy

Full Screen

1package pageObject;2import java.lang.reflect.Constructor;3import java.lang.reflect.Field;4import java.lang.reflect.InvocationTargetException;5import java.lang.reflect.ParameterizedType;6import java.util.ArrayList;7import java.util.List;8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.WebElement;10import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;11import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;12import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;13import org.openqa.selenium.support.pagefactory.FieldDecorator;14public class PageFactory{15 /**16 * Initialize the pageObject with input pageObject class17 * @param <T>18 * @param driver19 * @param pageClassToProxy20 * @return21 */22 public static <T> T initElements(WebDriver driver, Class<T> pageClassToProxy) {23 T page = instantiatePage(driver, pageClassToProxy);24 initElements(driver, page);25 return page;26 }27 /**28 * Initialize all the field of one page object 29 * with @FindBy tag whose type are "MyWebElement"30 * @param driver31 * @param page32 */33 public static void initElements(WebDriver driver, Object page) {34 final WebDriver driverRef = driver;35 ElementLocatorFactory factoryRef = new DefaultElementLocatorFactory(driverRef);36 FieldDecorator decorator = new DefaultFieldDecorator(factoryRef);37 Class<?> proxyIn = page.getClass();38 while (proxyIn != Object.class) {39 proxyFields(decorator, page, proxyIn);40 proxyIn = proxyIn.getSuperclass();41 }42 }43 /**44 * The logic to initialize the field whose type are MyWebElement45 * @param decorator46 * @param page47 * @param proxyIn48 */49 @SuppressWarnings("unchecked")50 private static void proxyFields(FieldDecorator decorator, Object page, Class<?> proxyIn) {51 Field[] fields = proxyIn.getDeclaredFields();52 for (Field field : fields) {53 54 Object value = decorator.decorate(page.getClass().getClassLoader(), field);55 56 // Cast the value to probable interface57 Object castValue = null;58 String fieldName = field.getType().getName();59 if(fieldName.contains("MyWebElement")){ // private WebElement xxx;60 castValue = new MyElement((WebElement)value);61 }else if(field.getType().getName().contains("WebElement")){ // private MyWebElement xxx;62 castValue = (WebElement)value;63 }else if(fieldName.contains("java.util.List")){ // private List<E> xxx;64 ParameterizedType pt = (ParameterizedType)field.getGenericType();65 String parameterName = pt.getActualTypeArguments()[0].toString();66 if (parameterName.contains("WebElement")){ // private List<WebElement> xxx;67 castValue = (List<WebElement>)value;68 }else if(parameterName.contains("MyWebElement")){ // private List<MyWebElement> xxx;69 List<WebElement> elements = (List<WebElement>)value;70 List<MyWebElement> myelements = new ArrayList<MyWebElement>();71 for(WebElement element : elements){72 myelements.add(new MyElement(element));73 }74 castValue = myelements;75 }76 }77 78 79 if (value != null && castValue != null) {80 try {81 field.setAccessible(true);82 field.set(page, castValue);83 } catch (IllegalAccessException e) {84 throw new RuntimeException(e);85 }86 }87 }88 }89 /**90 * Calling constructor to initialize page instance91 * @param <T>92 * @param driver93 * @param pageClassToProxy94 * @return95 */96 private static <T> T instantiatePage(WebDriver driver, Class<T> pageClassToProxy) {97 try {98 try {99 Constructor<T> constructor = pageClassToProxy.getConstructor(WebDriver.class);100 return constructor.newInstance(driver);101 } catch (NoSuchMethodException e) {102 return pageClassToProxy.newInstance();103 }104 } catch (InstantiationException e) {105 throw new RuntimeException(e);106 } catch (IllegalAccessException e) {107 throw new RuntimeException(e);108 } catch (InvocationTargetException e) {109 throw new RuntimeException(e);110 }111 }112}...

Full Screen

Full Screen

Source:ControlFieldDecorator.java Github

copy

Full Screen

1package com.ea.Framework.Controls.api;2/*3 * Internal Page Factory modification to align with the framework4 */5import com.ea.Framework.Controls.internals.Control;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.WrapsElement;8import org.openqa.selenium.interactions.Locatable;9import org.openqa.selenium.support.FindAll;10import org.openqa.selenium.support.FindBy;11import org.openqa.selenium.support.FindBys;12import org.openqa.selenium.support.pagefactory.ElementLocator;13import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;14import org.openqa.selenium.support.pagefactory.FieldDecorator;15import java.lang.reflect.*;16import java.util.List;17public class ControlFieldDecorator implements FieldDecorator {18 protected ElementLocatorFactory factory;19 public ControlFieldDecorator(ElementLocatorFactory factory) {20 this.factory = factory;21 }22 @Override23 public Object decorate(ClassLoader loader, Field field) {24 if (!(WebElement.class.isAssignableFrom(field.getType()) || isDecoratableList(field))) {25 return null;26 }27 ElementLocator locator = factory.createLocator(field);28 if (locator == null) {29 return null;30 }31 Class<?> fieldType = field.getType();32 if (WebElement.class.equals(fieldType)) {33 fieldType = Control.class;34 }35 if (WebElement.class.isAssignableFrom(fieldType)) {36 return proxyForLocator(loader, fieldType, locator);37 } else if (List.class.isAssignableFrom(fieldType)) {38 Class<?> erasureClass = getErasureClass(field);39 return proxyForListLocator(loader, erasureClass, locator);40 } else {41 return null;42 }43 }44 private Class getErasureClass(Field field) {45 Type genericType = field.getGenericType();46 if (!(genericType instanceof ParameterizedType)) {47 return null;48 }49 return (Class) ((ParameterizedType) genericType).getActualTypeArguments()[0];50 }51 protected boolean isDecoratableList(Field field) {52 if(!List.class.isAssignableFrom(field.getType())) {53 return false;54 } else {55 Type genericType = field.getGenericType();56 if(!(genericType instanceof ParameterizedType)) {57 return false;58 } else {59 Type listType = ((ParameterizedType)genericType).getActualTypeArguments()[0];60 return !WebElement.class.equals(listType)?false:field.getAnnotation(FindBy.class) != null || field.getAnnotation(FindBys.class) != null || field.getAnnotation(FindAll.class) != null;61 }62 }63 }64 protected <T> T proxyForLocator(ClassLoader loader, Class<T> interfaceType, ElementLocator locator) {65 InvocationHandler handler = new ControlHandler(interfaceType, locator);66 T proxy;67 proxy = interfaceType.cast(Proxy.newProxyInstance(68 loader, new Class[]{interfaceType, WebElement.class, WrapsElement.class, Locatable.class}, handler));69 return proxy;70 }71 protected <T> List<T> proxyForListLocator(ClassLoader loader, Class<T> interfaceType, ElementLocator locator) {72 InvocationHandler handler = new ControlListHandler(interfaceType, locator);73 List<T> proxy;74 proxy = (List<T>) Proxy.newProxyInstance(75 loader, new Class[]{List.class}, handler);76 return proxy;77 }78}...

Full Screen

Full Screen

Source:CustomFieldDecorator.java Github

copy

Full Screen

1package com.cashkaro.element.wrappers;2import java.lang.reflect.Field;3import java.lang.reflect.InvocationHandler;4import java.lang.reflect.ParameterizedType;5import java.lang.reflect.Proxy;6import java.lang.reflect.Type;7import java.util.List;8import org.openqa.selenium.WebElement;9import org.openqa.selenium.internal.Locatable;10import org.openqa.selenium.internal.WrapsElement;11import org.openqa.selenium.support.FindAll;12import org.openqa.selenium.support.FindBy;13import org.openqa.selenium.support.FindBys;14import org.openqa.selenium.support.pagefactory.ElementLocator;15import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;16import org.openqa.selenium.support.pagefactory.FieldDecorator;17public class CustomFieldDecorator implements FieldDecorator {18 protected ElementLocatorFactory factory;19 public CustomFieldDecorator(ElementLocatorFactory factory) {20 this.factory = factory;21 }22 public Object decorate(ClassLoader loader, Field field) {23 if (!(WebElement.class.isAssignableFrom(field.getType())24 || isDecoratableList(field))) {25 return null;26 }27 ElementLocator locator = factory.createLocator(field);28 if (locator == null) {29 return null;30 }31 32 Class<?> fieldType = field.getType();33 if (WebElement.class.equals(fieldType)) {34 fieldType = HtmlField.class;35 }36 37 if (WebElement.class.isAssignableFrom(field.getType())) {38 return proxyForLocator(loader,fieldType, locator);39 } else if (List.class.isAssignableFrom(field.getType())) {40 return proxyForListLocator(loader,fieldType, locator);41 } else {42 return null;43 }44 }45 protected boolean isDecoratableList(Field field) {46 if (!List.class.isAssignableFrom(field.getType())) {47 return false;48 }49 // Type erasure in Java isn't complete. Attempt to discover the generic50 // type of the list.51 Type genericType = field.getGenericType();52 if (!(genericType instanceof ParameterizedType)) {53 return false;54 }55 Type listType = ((ParameterizedType) genericType).getActualTypeArguments()[0];56 if (!WebElement.class.equals(listType)) {57 return false;58 }59 if (field.getAnnotation(FindBy.class) == null &&60 field.getAnnotation(FindBys.class) == null &&61 field.getAnnotation(FindAll.class) == null) {62 return false;63 }64 return true;65 }66 /* Generate a type-parameterized locator proxy for the element in question. */67 protected <T> T proxyForLocator(ClassLoader loader, Class<T> interfaceType, ElementLocator locator) {68 InvocationHandler handler = new CustomElementHandler(interfaceType, locator);69 T proxy;70 proxy = interfaceType.cast(Proxy.newProxyInstance(71 loader, new Class[]{interfaceType, WebElement.class, WrapsElement.class, Locatable.class}, handler));72 return proxy;73 }74 /* generates a proxy for a list of elements to be wrapped. */75 @SuppressWarnings("unchecked")76 protected <T> List<T> proxyForListLocator(ClassLoader loader, Class<T> interfaceType, ElementLocator locator) {77 InvocationHandler handler = new CustomElementListHandler(interfaceType, locator);78 List<T> proxy;79 proxy = (List<T>) Proxy.newProxyInstance(80 loader, new Class[]{List.class}, handler);81 return proxy;82 }83}...

Full Screen

Full Screen

Interface FieldDecorator

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.support.FindBy;5import org.openqa.selenium.support.PageFactory;6import org.openqa.selenium.support.pagefactory.AbstractAnnotations;7import org.openqa.selenium.support.pagefactory.Annotations;8import org.openqa.selenium.support.pagefactory.ElementLocator;9import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;10import java.lang.reflect.Field;11import java.lang.reflect.InvocationHandler;12import java.lang.reflect.InvocationTargetException;13import java.lang.reflect.Method;14import java.lang.reflect.Proxy;15import java.lang.reflect.Type;16import java.util.List;17public class CustomFieldDecorator implements FieldDecorator {18 private final WebDriver driver;19 private final ElementLocatorFactory factory;20 public CustomFieldDecorator(WebDriver driver, ElementLocatorFactory factory) {21 this.driver = driver;22 this.factory = factory;23 }24 public Object decorate(ClassLoader loader, Field field) {25 if (!(WebElement.class.isAssignableFrom(field.getType()) || isDecoratableList(field))) {26 return null;27 }28 ElementLocator locator = factory.createLocator(field);29 if (locator == null) {30 return null;31 }32 if (WebElement.class.isAssignableFrom(field.getType())) {33 return proxyForLocator(loader, locator);34 } else if (List.class.isAssignableFrom(field.getType())) {35 return proxyForListLocator(loader, locator);36 } else {37 return null;38 }39 }40 protected WebElement proxyForLocator(ClassLoader loader, ElementLocator locator) {41 InvocationHandler handler = new LocatingElementHandler(locator);42 WebElement proxy;43 proxy = (WebElement) Proxy.newProxyInstance(44 loader, new Class[]{WebElement.class, WrapsElement.class, Locatable.class}, handler);45 return proxy;46 }47 protected List<WebElement> proxyForListLocator(ClassLoader loader, ElementLocator locator) {48 InvocationHandler handler = new LocatingElementListHandler(locator);49 List<WebElement> proxy;50 proxy = (List<WebElement>) Proxy.newProxyInstance(51 loader, new Class[]{List.class}, handler);52 return proxy;53 }54 protected boolean isDecoratableList(Field field) {55 if (!List.class.isAssignableFrom(field.getType())) {56 return false;57 }58 Type genericType = field.getGenericType();59 if (!(genericType instanceof ParameterizedType)) {60 return false;61 }62 Type listType = ((ParameterizedType) genericType).getActualTypeArguments()[0];63 if (

Full Screen

Full Screen

Interface FieldDecorator

Using AI Code Generation

copy

Full Screen

1package com.test;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.support.FindBy;5import org.openqa.selenium.support.PageFactory;6import org.openqa.selenium.support.pagefactory.FieldDecorator;7import org.openqa.selenium.support.pagefactory.FieldDecorator;8import org.openqa.selenium.support.pagefactory.FieldLocator;9import org.openqa.selenium.support.pagefactory.FieldLocatorFactory;10import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler;11import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler;12public class PageObject {13 private WebDriver driver;14 public PageObject(WebDriver driver) {15 this.driver = driver;16 PageFactory.initElements(new FieldDecorator() {17 public Object decorate(ClassLoader loader, Field field) {18 if (!(WebElement.class.isAssignableFrom(field.getType()) || isDecoratableList(field))) {19 return null;20 }21 FieldLocator locator = factory.createLocator(field);22 if (locator == null) {23 return null;24 }25 if (WebElement.class.isAssignableFrom(field.getType())) {26 return proxyForLocator(loader, locator);27 } else if (List.class.isAssignableFrom(field.getType())) {28 return proxyForListLocator(loader, locator);29 } else {30 return null;31 }32 }33 }, this);34 }35 private Object proxyForLocator(ClassLoader loader, FieldLocator locator) {36 InvocationHandler handler = new LocatingElementHandler(locator);37 return Proxy.newProxyInstance(38 loader, new Class[] {WebElement.class, WrapsElement.class, Locatable.class}, handler);39 }40 private Object proxyForListLocator(ClassLoader loader, FieldLocator locator) {41 InvocationHandler handler = new LocatingElementListHandler(locator);42 return Proxy.newProxyInstance(43 loader, new Class[] {List.class}, handler);44 }45 private boolean isDecoratableList(Field field) {46 if (!List.class.isAssignableFrom(field.getType())) {47 return false;48 }49 if (!(field.getGenericType() instanceof ParameterizedType)) {50 return false;51 }52 Type listType = ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];53 if (!(listType instanceof Class)) {54 return false;55 }56 return WebElement.class.isAssignableFrom((Class<?>) listType);57 }58 @FindBy(id = "foo")59 private WebElement foo;60 @FindBy(id = "bar")61 private List<WebElement> bar;62}

Full Screen

Full Screen

Interface FieldDecorator

Using AI Code Generation

copy

Full Screen

1public class FieldDecorator implements org.openqa.selenium.support.pagefactory.FieldDecorator {2 private final org.openqa.selenium.support.pagefactory.ElementLocatorFactory factory;3 public FieldDecorator(org.openqa.selenium.support.pagefactory.ElementLocatorFactory factory) {4 this.factory = factory;5 }6 public Object decorate(ClassLoader loader, Field field) {7 if (!(org.openqa.selenium.support.pagefactory.Annotations.isDecoratableList(field)8 || org.openqa.selenium.support.pagefactory.Annotations.isDecoratableElement(field))) {9 return null;10 }11 org.openqa.selenium.support.pagefactory.ElementLocator locator = factory.createLocator(field);12 if (org.openqa.selenium.support.pagefactory.Annotations.isDecoratableList(field)) {13 return org.openqa.selenium.support.pagefactory.ElementLocatorListHandler.createHandlerForListLocator(loader, locator);14 } else {15 return org.openqa.selenium.support.pagefactory.ElementLocatorHandler.createHandlerForLocator(loader, locator);16 }17 }18}19[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ selenium ---

Full Screen

Full Screen

Interface FieldDecorator

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.WebDriver;2import org.openqa.selenium.support.PageFactory;3import org.openqa.selenium.support.pagefactory.FieldDecorator;4import org.openqa.selenium.support.pagefactory.FindBy;5import org.openqa.selenium.support.pagefactory.Page;6import org.openqa.selenium.WebElement;7public class PageFactoryExample {8 private WebDriver driver;9 @FindBy(id = "foo")10 private WebElement fooElement;11 public PageFactoryExample(WebDriver driver) {12 this.driver = driver;13 PageFactory.initElements(driver, this);14 }15 public WebElement getFooElement() {16 return fooElement;17 }18}19public interface FieldDecorator {20 Object decorate(ClassLoader loader, Field field);21}22public @interface FindBy {23 How how() default How.ID;24 String using() default "";25 String id() default "";26 String name() default "";27 String xpath() default "";28 String css() default "";29 String linkText() default "";30 String partialLinkText() default "";31 String tagName() default "";32 String className() default "";33 String[] value() default {};34}35public class PageFactory {36 public static void initElements(WebDriver driver, Object page) {37 initElements(new DefaultElementLocatorFactory(driver), page);38 }39 public static void initElements(SearchContext searchContext, Object page) {40 initElements(new DefaultElementLocatorFactory(searchContext), page);41 }42 public static void initElements(ElementLocatorFactory factory, Object page) {43 initElements(new DefaultFieldDecorator(factory), page);44 }45 public static void initElements(FieldDecorator decorator, Object page) {46 Class<?> proxyIn = Page.class.isAssignableFrom(page.getClass()) ? page.getClass().getSuperclass() : page.getClass();47 InvocationHandler handler = new LocatingElementHandler(decorator);48 PageFactory.initElements(new ElementLocatorFactory() {49 public ElementLocator createLocator(Field field) {50 return null;51 }52 }, page);53 PageFactory.initElements(new ElementLocatorFactory() {54 public ElementLocator createLocator(Field field) {55 return null;56 }57 }, page);58 PageFactory.initElements(new ElementLocatorFactory() {

Full Screen

Full Screen
copy
1<build>2 <plugins>3 <plugin>4 <artifactId>maven-jar-plugin</artifactId>5 <configuration>6 <archive>7 <manifest>8 <mainClass>class name us.com.test.abc.MyMainClass</mainClass>9 </manifest>10 </archive>11 </configuration>12 </plugin>13 </plugins>14</build>15
Full Screen

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

...Most popular Stackoverflow questions on Interface-FieldDecorator

Most used methods in Interface-FieldDecorator

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful