1package ru.yandex.qatools.htmlelements.loader.decorator;2import org.openqa.selenium.SearchContext;3import org.openqa.selenium.support.pagefactory.AjaxElementLocator;4import org.openqa.selenium.support.pagefactory.ElementLocator;5import ru.yandex.qatools.htmlelements.annotations.Timeout;6import ru.yandex.qatools.htmlelements.pagefactory.CustomElementLocatorFactory;7import java.lang.reflect.Field;8import java.lang.reflect.InvocationTargetException;9import java.lang.reflect.Method;10import java.lang.reflect.ParameterizedType;11/**12 * A factory for producing locator instances.13 *14 * @author Alexander Tolmachev starlight@yandex-team.ru15 * Date: 15.08.1216 */17public class HtmlElementLocatorFactory implements CustomElementLocatorFactory {18 private final SearchContext searchContext;19 public HtmlElementLocatorFactory(SearchContext searchContext) {20 this.searchContext = searchContext;21 }22 /**23 * Creates locator for the given field. Created locator will process {@link org.openqa.selenium.support.FindBy},24 * {@link org.openqa.selenium.support.FindBy}, {@link org.openqa.selenium.support.FindBys},25 * {@link org.openqa.selenium.support.FindAll} and {@link org.openqa.selenium.support.CacheLookup} annotations.26 *27 * @param field Field for which locator will be created.28 */29 public ElementLocator createLocator(Field field) {30 return new AjaxElementLocator(searchContext, getTimeOut(field), new HtmlElementFieldAnnotationsHandler(field));31 }32 /**33 * Creates locator for the given field. Created locator will process {@link org.openqa.selenium.support.FindBy},34 * {@link org.openqa.selenium.support.FindBy}, {@link org.openqa.selenium.support.FindBys},35 * {@link org.openqa.selenium.support.FindAll} and {@link org.openqa.selenium.support.CacheLookup} annotations.36 *37 * @param clazz Class for which locator will be created.38 */39 public ElementLocator createLocator(Class clazz) {40 //noinspection unchecked41 return new AjaxElementLocator(searchContext, getTimeOut(clazz), new HtmlElementClassAnnotationsHandler(clazz));42 }43 public int getTimeOut(Field field) {44 if (field.isAnnotationPresent(Timeout.class)) {45 return field.getAnnotation(Timeout.class).value();46 }47 if (field.getGenericType() instanceof Class) {48 return getTimeOut((Class) field.getGenericType());49 }50 return getTimeOut((Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]);51 }52 public int getTimeOut(Class clazz) {53 //noinspection EmptyCatchBlock54 try {55 Method method = Timeout.class.getMethod("value");56 do {57 if (clazz.isAnnotationPresent(Timeout.class)) {58 return (Integer) method.invoke(clazz.getAnnotation(Timeout.class));59 }60 clazz = clazz.getSuperclass();61 } while (clazz != Object.class && clazz != null);62 } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {63 }64 return Integer.getInteger("webdriver.timeouts.implicitlywait", 5);65 }66}...