Skip to content

Fix #203 #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public List<WebElement> apply(By by) {
private WebElement cachedElement;
private List<WebElement> cachedElementList;

private final TimeOutContainer timeOutContainer;
private final TimeOutDuration timeOutDuration;

/**
* Creates a new mobile element locator. It instantiates {@link WebElement}
Expand All @@ -64,7 +64,7 @@ public List<WebElement> apply(By by) {
* The field on the Page Object that will hold the located value
*/
AppiumElementLocator(SearchContext searchContext, Field field,
TimeOutContainer timeOutContainer) {
TimeOutDuration timeOutDuration) {
this.searchContext = searchContext;
// All known webdrivers implement HasCapabilities
Capabilities capabilities = ((HasCapabilities) WebDriverUnpackUtility.
Expand All @@ -78,7 +78,12 @@ public List<WebElement> apply(By by) {

AppiumAnnotations annotations = new AppiumAnnotations(field, platform,
automation);
this.timeOutContainer = timeOutContainer;
if (field.isAnnotationPresent(WithTimeout.class)){
WithTimeout withTimeout = field.getAnnotation(WithTimeout.class);
this.timeOutDuration = new TimeOutDuration(withTimeout.time(), withTimeout.unit());
}
else
this.timeOutDuration = timeOutDuration;
shouldCache = annotations.isLookupCached();
by = annotations.buildBy();
}
Expand All @@ -98,14 +103,14 @@ private List<WebElement> waitFor() {
try {
changeImplicitlyWaitTimeOut(0, TimeUnit.SECONDS);
FluentWait<By> wait = new FluentWait<By>(by);
wait.withTimeout(timeOutContainer.getTimeValue(),
timeOutContainer.getTimeUnitValue());
wait.withTimeout(timeOutDuration.getTime(),
timeOutDuration.getTimeUnit());
return wait.until(new WaitingFunction(searchContext));
} catch (TimeoutException e) {
return new ArrayList<WebElement>();
} finally {
changeImplicitlyWaitTimeOut(timeOutContainer.getTimeValue(),
timeOutContainer.getTimeUnitValue());
changeImplicitlyWaitTimeOut(timeOutDuration.getTime(),
timeOutDuration.getTimeUnit());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
package io.appium.java_client.pagefactory;

import java.lang.reflect.Field;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.SearchContext;
import org.openqa.selenium.support.pagefactory.ElementLocator;
import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;

class AppiumElementLocatorFactory implements ElementLocatorFactory, ResetsImplicitlyWaitTimeOut {
class AppiumElementLocatorFactory implements ElementLocatorFactory {
private final SearchContext searchContext;
private final TimeOutContainer timeOutContainer;
private final TimeOutDuration timeOutDuration;

public AppiumElementLocatorFactory(SearchContext searchContext,
long implicitlyWaitTimeOut, TimeUnit timeUnit) {
TimeOutDuration timeOutDuration) {
this.searchContext = searchContext;
this.timeOutContainer = new TimeOutContainer(implicitlyWaitTimeOut, timeUnit);
this.timeOutDuration = timeOutDuration;
}

public AppiumElementLocatorFactory(SearchContext searchContext) {
this(searchContext, AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT,
AppiumFieldDecorator.DEFAULT_TIMEUNIT);
this(searchContext, new TimeOutDuration(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT,
AppiumFieldDecorator.DEFAULT_TIMEUNIT));
}

public ElementLocator createLocator(Field field) {
return new AppiumElementLocator(searchContext, field, timeOutContainer);
}

@Override
public void resetImplicitlyWaitTimeOut(long timeOut, TimeUnit timeUnit) {
timeOutContainer.resetImplicitlyWaitTimeOut(timeOut, timeUnit);
return new AppiumElementLocator(searchContext, field, timeOutDuration);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.appium.java_client.pagefactory;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchableElement;
import io.appium.java_client.android.AndroidDriver;
Expand Down Expand Up @@ -31,7 +30,7 @@
* {@link MobileElement}, {@link AndroidElement} and {@link IOSElement} are allowed
* to use with this decorator
*/
public class AppiumFieldDecorator implements FieldDecorator, ResetsImplicitlyWaitTimeOut {
public class AppiumFieldDecorator implements FieldDecorator {

private static final List<Class<? extends WebElement>> availableElementClasses =
new ArrayList<Class<? extends WebElement>>(){
Expand Down Expand Up @@ -66,8 +65,13 @@ public class AppiumFieldDecorator implements FieldDecorator, ResetsImplicitlyWai

public AppiumFieldDecorator(SearchContext context, long implicitlyWaitTimeOut, TimeUnit timeUnit) {
this.context = context;
factory = new AppiumElementLocatorFactory(this.context, implicitlyWaitTimeOut, timeUnit);
factory = new AppiumElementLocatorFactory(this.context, new TimeOutDuration(implicitlyWaitTimeOut, timeUnit));
}

public AppiumFieldDecorator(SearchContext context, TimeOutDuration timeOutDuration) {
this.context = context;
factory = new AppiumElementLocatorFactory(this.context, timeOutDuration);
}

public AppiumFieldDecorator(SearchContext context) {
this.context = context;
Expand Down Expand Up @@ -150,9 +154,4 @@ private List<WebElement> proxyForListLocator(
return ProxyFactory.getEnhancedProxy(ArrayList.class,
elementInterceptor);
}

@Override
public void resetImplicitlyWaitTimeOut(long timeOut, TimeUnit timeUnit) {
factory.resetImplicitlyWaitTimeOut(timeOut, timeUnit);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.appium.java_client.pagefactory;

import java.util.concurrent.TimeUnit;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

/**
* Represents an duration of waiting for element rendering.
*/
public class TimeOutDuration {

private long time;
private TimeUnit unit;

/**
* @param time The amount of time.
* @param unit The unit of time.
*/
public TimeOutDuration(long time, TimeUnit unit) {
setTime(time, unit);
}

public long getTime(){
return time;
}

public TimeUnit getTimeUnit(){
return unit;
}

public void setTime(long newTime){
checkArgument(newTime >= 0, "Duration < 0: %d", newTime);
time = newTime;
}

public void setTime(TimeUnit newTimeUnit){
checkNotNull(newTimeUnit);
unit = newTimeUnit;
}

public void setTime(long newTime, TimeUnit newTimeUnit){
setTime(newTime);
setTime(newTimeUnit);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.appium.java_client.pagefactory;


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.TimeUnit;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
/**
This annotation is used when some element waits for time
that differs from defined by default
*/
public @interface WithTimeout {
long time();
TimeUnit unit();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.xpath.operations.And;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -343,7 +342,8 @@ public void areTouchableElements(){

@Test
public void isTheFieldAndroidElement(){
AndroidElement androidElement = (AndroidElement) mobiletextVieW; //declared as MobileElement
@SuppressWarnings("unused")
AndroidElement androidElement = (AndroidElement) mobiletextVieW; //declared as MobileElement
androidElement = (AndroidElement) androidTextView; //declared as WedElement
androidElement = (AndroidElement) remotetextVieW; //declared as RemoteWedElement
androidElement = (AndroidElement) touchabletextVieW; //declared as TouchABLEElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void setUp() throws Exception {
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, MobileBrowserType.SAFARI);
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.1");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone Simulator");
driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver = new IOSDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
PageFactory.initElements(new AppiumFieldDecorator(driver, 5, TimeUnit.SECONDS), this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.appium.java_client.pagefactory.TimeOutDuration;
import io.appium.java_client.pagefactory.WithTimeout;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -25,14 +27,20 @@ public class TimeOutResetTest {
@FindAll({@FindBy(className = "ClassWhichDoesNotExist"),
@FindBy(className = "OneAnotherClassWhichDoesNotExist")})
private List<WebElement> stubElements;
private AppiumFieldDecorator afd;

@WithTimeout(time = 5, unit = TimeUnit.SECONDS)
@FindAll({@FindBy(className = "ClassWhichDoesNotExist"),
@FindBy(className = "OneAnotherClassWhichDoesNotExist")})
private List<WebElement> stubElements2;

private TimeOutDuration timeOutDuration;

@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
afd = new AppiumFieldDecorator(driver);

PageFactory.initElements(afd, this);
timeOutDuration = new TimeOutDuration(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT,
AppiumFieldDecorator.DEFAULT_TIMEUNIT);
PageFactory.initElements(new AppiumFieldDecorator(driver, timeOutDuration), this);
}

@After
Expand All @@ -56,7 +64,7 @@ private static void checkTimeDifference(long etalonTime,
}
}

private long getBenchMark() {
private long getBenchMark(List<WebElement> stubElements) {
long startMark = Calendar.getInstance().getTimeInMillis();
stubElements.size();
long endMark = Calendar.getInstance().getTimeInMillis();
Expand All @@ -66,20 +74,45 @@ private long getBenchMark() {
@Test
public void test() {
checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, AppiumFieldDecorator.DEFAULT_TIMEUNIT,
getBenchMark());
getBenchMark(stubElements));
System.out.println(String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT)
+ " " + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine");

afd.resetImplicitlyWaitTimeOut(15500000, TimeUnit.MICROSECONDS);
checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark());
timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS);
checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(stubElements));
System.out.println("Change time: " + String.valueOf(15500000) + " "
+ TimeUnit.MICROSECONDS.toString() + ": Fine");

afd.resetImplicitlyWaitTimeOut(3, TimeUnit.SECONDS);
checkTimeDifference(3, TimeUnit.SECONDS, getBenchMark());
timeOutDuration.setTime(3, TimeUnit.SECONDS);
checkTimeDifference(3, TimeUnit.SECONDS, getBenchMark(stubElements));
System.out.println("Change time: " + String.valueOf(3) + " "
+ TimeUnit.SECONDS.toString() + ": Fine");

}

@Test
public void test2() {
checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, AppiumFieldDecorator.DEFAULT_TIMEUNIT,
getBenchMark(stubElements));
System.out.println(String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT)
+ " " + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine");

checkTimeDifference(5, TimeUnit.SECONDS,
getBenchMark(stubElements2));
System.out.println(String.valueOf(5)
+ " " + TimeUnit.SECONDS.toString() + ": Fine");


timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS);
checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(stubElements));
System.out.println("Change time: " + String.valueOf(15500000) + " "
+ TimeUnit.MICROSECONDS.toString() + ": Fine");

checkTimeDifference(5, TimeUnit.SECONDS,
getBenchMark(stubElements2));
System.out.println(String.valueOf(5)
+ " " + TimeUnit.SECONDS.toString() + ": Fine");

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchableElement;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.IOSElement;
import io.appium.java_client.pagefactory.AndroidFindBy;
Expand Down Expand Up @@ -275,7 +274,8 @@ public void areTouchAbleElements(){

@Test
public void isTheFieldIOSElement(){
IOSElement iOSElement = (IOSElement) mobileButton; //declared as MobileElement
@SuppressWarnings("unused")
IOSElement iOSElement = (IOSElement) mobileButton; //declared as MobileElement
iOSElement = (IOSElement) iosUIAutomatorButton; //declared as WebElement
iOSElement = (IOSElement) remotetextVieW; //declared as RemoteWebElement
iOSElement = (IOSElement) touchableButton; //declared as TouchABLEElement
Expand Down