From 6e59f32844c2bdd9acbf14c26418093c7daa01ec Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Sat, 11 Oct 2014 01:53:03 +0400 Subject: [PATCH] #120 FIX --- pom.xml | 281 ++++++++--------- .../pagefactory/AppiumFieldDecorator.java | 283 +++++++++--------- 2 files changed, 275 insertions(+), 289 deletions(-) diff --git a/pom.xml b/pom.xml index fff038a23..4b4036ee8 100644 --- a/pom.xml +++ b/pom.xml @@ -1,148 +1,133 @@ - - - 4.0.0 - - io.appium - java-client - 2.0.0 - - - com.google.code.gson - gson - 2.2.4 - - - org.seleniumhq.selenium - selenium-java - 2.43.1 - - - junit - junit - 4.11 - - - org.apache.httpcomponents - httpclient - 4.3.3 - - - com.google.guava - guava - 17.0 - - - cglib - cglib - 3.1 - - - org.reflections - reflections - 0.9.8 - - - guava - com.google.guava - - - xml-apis - xml-apis - - - - - jar - java-client - Java client for Appium Mobile Webdriver - http://appium.io - - - - Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - repo - - - - - https://github.com/appium/java-client - scm:git:git://github.com/appium/java-client.git - scm:git:git@github.com:appium/java-client.git - - - - - jonahss@gmail.com - Jonah Stiennon - https://github.com/jonahss - jonahss - - - - - - ossrh - https://oss.sonatype.org/content/repositories/snapshots - - - ossrh - https://oss.sonatype.org/service/local/staging/deploy/maven2/ - - - - - - - org.apache.maven.plugins - maven-source-plugin - 2.2.1 - - - attach-sources - - jar-no-fork - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - - attach-javadocs - - jar - - - - - - org.apache.maven.plugins - maven-gpg-plugin - 1.5 - - - sign-artifacts - verify - - sign - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.3.2 - - 1.7 - 1.7 - - - - - + + + 4.0.0 + + io.appium + java-client + 2.0.0 + + + com.google.code.gson + gson + 2.2.4 + + + org.seleniumhq.selenium + selenium-java + 2.43.1 + + + junit + junit + 4.11 + + + org.apache.httpcomponents + httpclient + 4.3.3 + + + com.google.guava + guava + 17.0 + + + cglib + cglib + 3.1 + + + jar + java-client + Java client for Appium Mobile Webdriver + http://appium.io + + + + Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + https://github.com/appium/java-client + scm:git:git://github.com/appium/java-client.git + scm:git:git@github.com:appium/java-client.git + + + + + jonahss@gmail.com + Jonah Stiennon + https://github.com/jonahss + jonahss + + + + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + + + ossrh + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + + + + org.apache.maven.plugins + maven-source-plugin + 2.2.1 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.9.1 + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.5 + + + sign-artifacts + verify + + sign + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.7 + 1.7 + + + + + diff --git a/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java b/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java index c86e385c5..5258b2f63 100644 --- a/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java +++ b/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java @@ -1,141 +1,142 @@ -package io.appium.java_client.pagefactory; - -import io.appium.java_client.MobileElement; - -import java.lang.reflect.Field; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; - -import org.openqa.selenium.SearchContext; -import org.openqa.selenium.WebElement; -import org.openqa.selenium.remote.RemoteWebElement; -import org.openqa.selenium.support.FindAll; -import org.openqa.selenium.support.FindBy; -import org.openqa.selenium.support.FindBys; -import org.openqa.selenium.support.pagefactory.ElementLocator; -import org.openqa.selenium.support.pagefactory.FieldDecorator; -import org.reflections.Reflections; - -/** - * Default decorator for use with PageFactory. Will decorate 1) all of the - * WebElement fields and 2) List fields that have - * {@literal @AndroidFindBy}, {@literal @AndroidFindBys}, or - * {@literal @iOSFindBy/@iOSFindBys} annotation with a proxy that locates the - * elements using the passed in ElementLocatorFactory. - * - * Please pay attention: fields of {@link WebElement}, {@link RemoteWebElement} and - * {@link MobileElement} are allowed to use with this decorator - */ -public class AppiumFieldDecorator implements FieldDecorator, ResetsImplicitlyWaitTimeOut { - - private static final List> availableElementClasses = - new ArrayList>(){ - private static final long serialVersionUID = 1L; - { - add(WebElement.class); - add(RemoteWebElement.class); - add(MobileElement.class); - - Reflections r = new Reflections("io.appium"); - addAll(r.getSubTypesOf(MobileElement.class)); - } - - }; - - private final AppiumElementLocatorFactory factory; - - public static long DEFAULT_IMPLICITLY_WAIT_TIMEOUT = 1; - - public static TimeUnit DEFAULT_TIMEUNIT = TimeUnit.SECONDS; - - public AppiumFieldDecorator(SearchContext context, long implicitlyWaitTimeOut, TimeUnit timeUnit) { - factory = new AppiumElementLocatorFactory(context, implicitlyWaitTimeOut, timeUnit); - } - - public AppiumFieldDecorator(SearchContext context) { - factory = new AppiumElementLocatorFactory(context); - } - - public Object decorate(ClassLoader ignored, Field field) { - if (!(availableElementClasses.contains(field.getType()) || isDecoratableList(field))) { - return null; - } - - ElementLocator locator = factory.createLocator(field); - if (locator == null) { - return null; - } - - if (WebElement.class.isAssignableFrom(field.getType())) { - return proxyForLocator(field, locator); - } else if (List.class.isAssignableFrom(field.getType())) { - return proxyForListLocator(locator); - } else { - return null; - } - } - - private static boolean isAvailableElementClass(Type type){ - boolean result = false; - for (Class webElementClass: - availableElementClasses){ - if (!webElementClass.equals(type)){ - continue; - } - result = true; - break; - } - return result; - } - - private boolean isDecoratableList(Field field) { - if (!List.class.isAssignableFrom(field.getType())) { - return false; - } - - // Type erasure in Java isn't complete. Attempt to discover the generic - // type of the list. - Type genericType = field.getGenericType(); - if (!(genericType instanceof ParameterizedType)) { - return false; - } - - Type listType = ((ParameterizedType) genericType).getActualTypeArguments()[0]; - if (field.getAnnotation(AndroidFindBy.class) == null - && field.getAnnotation(iOSFindBy.class) == null - && field.getAnnotation(AndroidFindBys.class) == null - && field.getAnnotation(iOSFindBys.class) == null - && field.getAnnotation(FindBy.class) == null - && field.getAnnotation(FindBys.class) == null - && field.getAnnotation(FindAll.class) == null){ - return false; - } - return isAvailableElementClass(listType); - } - - private Object proxyForLocator(Field field, ElementLocator locator) { - Class type = field.getType(); - if (type.equals(WebElement.class)){ - type = RemoteWebElement.class; - } - ElementInterceptor elementInterceptor = new ElementInterceptor(locator); - return ProxyFactory.getEnhancedProxy(type, - elementInterceptor); - } - - @SuppressWarnings("unchecked") - private List proxyForListLocator( - ElementLocator locator) { - ElementListInterceptor elementInterceptor = new ElementListInterceptor(locator); - return ProxyFactory.getEnhancedProxy(ArrayList.class, - elementInterceptor); - } - - @Override - public void resetImplicitlyWaitTimeOut(long timeOut, TimeUnit timeUnit) { - factory.resetImplicitlyWaitTimeOut(timeOut, timeUnit); - } -} +package io.appium.java_client.pagefactory; + +import io.appium.java_client.MobileElement; +import io.appium.java_client.android.AndroidElement; +import io.appium.java_client.ios.IOSElement; + +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import org.openqa.selenium.SearchContext; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.remote.RemoteWebElement; +import org.openqa.selenium.support.FindAll; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.FindBys; +import org.openqa.selenium.support.pagefactory.ElementLocator; +import org.openqa.selenium.support.pagefactory.FieldDecorator; + +/** + * Default decorator for use with PageFactory. Will decorate 1) all of the + * WebElement fields and 2) List fields that have + * {@literal @AndroidFindBy}, {@literal @AndroidFindBys}, or + * {@literal @iOSFindBy/@iOSFindBys} annotation with a proxy that locates the + * elements using the passed in ElementLocatorFactory. + * + * Please pay attention: fields of {@link WebElement}, {@link RemoteWebElement}, + * {@link MobileElement}, {@link AndroidElement} and {@link IOSElement} are allowed + * to use with this decorator + */ +public class AppiumFieldDecorator implements FieldDecorator, ResetsImplicitlyWaitTimeOut { + + private static final List> availableElementClasses = + new ArrayList>(){ + private static final long serialVersionUID = 1L; + { + add(WebElement.class); + add(RemoteWebElement.class); + add(MobileElement.class); + add(AndroidElement.class); + add(IOSElement.class); + } + + }; + + private final AppiumElementLocatorFactory factory; + + public static long DEFAULT_IMPLICITLY_WAIT_TIMEOUT = 1; + + public static TimeUnit DEFAULT_TIMEUNIT = TimeUnit.SECONDS; + + public AppiumFieldDecorator(SearchContext context, long implicitlyWaitTimeOut, TimeUnit timeUnit) { + factory = new AppiumElementLocatorFactory(context, implicitlyWaitTimeOut, timeUnit); + } + + public AppiumFieldDecorator(SearchContext context) { + factory = new AppiumElementLocatorFactory(context); + } + + public Object decorate(ClassLoader ignored, Field field) { + if (!(availableElementClasses.contains(field.getType()) || isDecoratableList(field))) { + return null; + } + + ElementLocator locator = factory.createLocator(field); + if (locator == null) { + return null; + } + + if (WebElement.class.isAssignableFrom(field.getType())) { + return proxyForLocator(field, locator); + } else if (List.class.isAssignableFrom(field.getType())) { + return proxyForListLocator(locator); + } else { + return null; + } + } + + private static boolean isAvailableElementClass(Type type){ + boolean result = false; + for (Class webElementClass: + availableElementClasses){ + if (!webElementClass.equals(type)){ + continue; + } + result = true; + break; + } + return result; + } + + private boolean isDecoratableList(Field field) { + if (!List.class.isAssignableFrom(field.getType())) { + return false; + } + + // Type erasure in Java isn't complete. Attempt to discover the generic + // type of the list. + Type genericType = field.getGenericType(); + if (!(genericType instanceof ParameterizedType)) { + return false; + } + + Type listType = ((ParameterizedType) genericType).getActualTypeArguments()[0]; + if (field.getAnnotation(AndroidFindBy.class) == null + && field.getAnnotation(iOSFindBy.class) == null + && field.getAnnotation(AndroidFindBys.class) == null + && field.getAnnotation(iOSFindBys.class) == null + && field.getAnnotation(FindBy.class) == null + && field.getAnnotation(FindBys.class) == null + && field.getAnnotation(FindAll.class) == null){ + return false; + } + return isAvailableElementClass(listType); + } + + private Object proxyForLocator(Field field, ElementLocator locator) { + Class type = field.getType(); + if (type.equals(WebElement.class)){ + type = RemoteWebElement.class; + } + ElementInterceptor elementInterceptor = new ElementInterceptor(locator); + return ProxyFactory.getEnhancedProxy(type, + elementInterceptor); + } + + @SuppressWarnings("unchecked") + private List proxyForListLocator( + ElementLocator locator) { + ElementListInterceptor elementInterceptor = new ElementListInterceptor(locator); + return ProxyFactory.getEnhancedProxy(ArrayList.class, + elementInterceptor); + } + + @Override + public void resetImplicitlyWaitTimeOut(long timeOut, TimeUnit timeUnit) { + factory.resetImplicitlyWaitTimeOut(timeOut, timeUnit); + } +}