...3import org.openqa.selenium.WebElement;4import org.openqa.selenium.support.FindBy;5import org.openqa.selenium.support.FindBys;6import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;7import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;8import org.openqa.selenium.support.pagefactory.ElementLocator;9import java.lang.reflect.*;10import java.util.List;11public class CustomFieldDecorator extends DefaultFieldDecorator {12 public CustomFieldDecorator(SearchContext searchContext) {13 super(new DefaultElementLocatorFactory(searchContext));14 }15 /**16 * ÐеÑод вÑзÑваеÑÑÑ ÑабÑикой Ð´Ð»Ñ ÐºÐ°Ð¶Ð´Ð¾Ð³Ð¾ Ð¿Ð¾Ð»Ñ Ð² клаÑÑе17 */18 @Override19 public Object decorate(ClassLoader loader, Field field) {20 Class<IElement> decoratableClass = decoratableClass(field);21 // еÑли клаÑÑ Ð¿Ð¾Ð»Ñ Ð´ÐµÐºÐ¾ÑиÑÑемÑй22 if (decoratableClass != null) {23 ElementLocator locator = factory.createLocator(field);24 if (locator == null) {25 return null;26 }27 if (List.class.isAssignableFrom(field.getType())) {28 return createList(loader, locator, decoratableClass);29 }30 return createElement(loader, locator, decoratableClass);31 }32 return super.decorate(loader, field);33 }34 /**35 * ÐозвÑаÑÐ°ÐµÑ Ð´ÐµÐºÐ¾ÑиÑÑемÑй клаÑÑ Ð¿Ð¾Ð»Ñ,36 * либо null еÑли клаÑÑ Ð½Ðµ подÑ
Ð¾Ð´Ð¸Ñ Ð´Ð»Ñ Ð´ÐµÐºÐ¾ÑаÑоÑа37 */38 @SuppressWarnings("unchecked")39 private Class<IElement> decoratableClass(Field field) {40 Class<?> clazz = field.getType();41 if (List.class.isAssignableFrom(clazz)) {42 // Ð´Ð»Ñ ÑпиÑка обÑзаÑелÑно должна бÑÑÑ Ð·Ð°Ð´Ð°Ð½Ð° анноÑаÑиÑ43 if (field.getAnnotation(FindBy.class) == null &&44 field.getAnnotation(FindBys.class) == null) {45 return null;46 }47 // СпиÑок должен бÑÑÑ Ð¿Ð°ÑамеÑÑизиÑован48 Type genericType = field.getGenericType();49 if (!(genericType instanceof ParameterizedType)) {50 return null;51 }52 // полÑÑаем клаÑÑ Ð´Ð»Ñ ÑлеменÑов ÑпиÑка53 clazz = (Class<?>) ((ParameterizedType) genericType).54 getActualTypeArguments()[0];55 }56 if (IElement.class.isAssignableFrom(clazz)) {57 return (Class<IElement>) clazz;58 }59 else {60 return null;61 }62 }63 /**64 * Создание ÑлеменÑа.65 * ÐаÑ
Ð¾Ð´Ð¸Ñ WebElement и пеÑÐµÐ´Ð°ÐµÑ ÐµÐ³Ð¾ в каÑÑомнÑй клаÑÑ66 */67 protected IElement createElement(ClassLoader loader,68 ElementLocator locator,69 Class<IElement> clazz) {70 WebElement proxy = proxyForLocator(loader, locator);71 return WrapperFactory.createInstance(clazz, proxy);72 }73 /**74 * Создание ÑпиÑка75 */76 @SuppressWarnings("unchecked")77 protected List<IElement> createList(ClassLoader loader,78 ElementLocator locator,79 Class<IElement> clazz) {80 InvocationHandler handler =81 new LocatingCustomElementListHandler(locator, clazz);82 List<IElement> elements =83 (List<IElement>) Proxy.newProxyInstance(84 loader, new Class[] {List.class}, handler);85 return elements;86 }87}88/*89import org.openqa.selenium.SearchContext;90import org.openqa.selenium.WebElement;91import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;92import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;93import org.openqa.selenium.support.pagefactory.ElementLocator;94import java.lang.reflect.Field;95public class CustomFieldDecorator extends DefaultFieldDecorator {96 public CustomFieldDecorator(SearchContext searchContext) {97 super(new DefaultElementLocatorFactory(searchContext));98 }99 @Override100 public Object decorate(ClassLoader loader, Field field) {101 Class<?> decoratableClass = decoratableClass(field);102 // еÑли клаÑÑ Ð¿Ð¾Ð»Ñ Ð´ÐµÐºÐ¾ÑиÑÑемÑй103 if (decoratableClass != null) {104 ElementLocator locator = factory.createLocator(field);105 if (locator == null) {106 return null;107 }108 // ÑлеменÑ109 return createElement(loader, locator, decoratableClass);...