Skip to content
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 @@ -52,8 +52,7 @@ public List<WebElement> apply(By by) {
private WebElement cachedElement;
private List<WebElement> cachedElementList;

private final long implicitlyWaitTimeOut;
private final TimeUnit timeUnit ;
private final TimeOutContainer timeOutContainer;

/**
* Creates a new mobile element locator. It instantiates {@link WebElement}
Expand All @@ -64,17 +63,16 @@ public List<WebElement> apply(By by) {
* @param field
* The field on the Page Object that will hold the located value
*/
public AppiumElementLocator(SearchContext searchContext, Field field,
long implicitlyWaitTimeOut, TimeUnit timeUnit) {
AppiumElementLocator(SearchContext searchContext, Field field,
TimeOutContainer timeOutContainer) {
this.searchContext = searchContext;
// All known webdrivers implement HasCapabilities
String platform = String
.valueOf(((HasCapabilities) unpackWebDriverFromSearchContext())
.getCapabilities().getCapability(
MobileCapabilityType.PLATFORM_NAME));
AppiumAnnotations annotations = new AppiumAnnotations(field, platform);
this.implicitlyWaitTimeOut = implicitlyWaitTimeOut;
this.timeUnit = timeUnit;
this.timeOutContainer = timeOutContainer;
shouldCache = annotations.isLookupCached();
by = annotations.buildBy();
}
Expand Down Expand Up @@ -109,14 +107,15 @@ private List<WebElement> waitFor(){
try{
changeImplicitlyWaitTimeOut(0, TimeUnit.SECONDS);
FluentWait<By> wait = new FluentWait<By>(by);
wait.withTimeout(implicitlyWaitTimeOut, timeUnit);
wait.withTimeout(timeOutContainer.getTimeValue(), timeOutContainer.getTimeUnitValue());
return wait.until(new WaitingFunction(searchContext));
}
catch (TimeoutException e){
return new ArrayList<WebElement>();
}
finally{
changeImplicitlyWaitTimeOut(implicitlyWaitTimeOut, timeUnit);
changeImplicitlyWaitTimeOut(timeOutContainer.getTimeValue(),
timeOutContainer.getTimeUnitValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,24 @@ class AppiumElementLocatorFactory implements ElementLocatorFactory, ResetsImplic
private static TimeUnit DEFAULT_TIMEUNIT = TimeUnit.SECONDS;

private final SearchContext searchContext;
private long implicitlyWaitTimeOut;
private TimeUnit timeUnit;
private final TimeOutContainer timeOutContainer;

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

public AppiumElementLocatorFactory(SearchContext searchContext) {
this(searchContext, DEFAULT_IMPLICITLY_WAIT_TIMEOUT, DEFAULT_TIMEUNIT);
}

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

@Override
public void resetImplicitlyWaitTimeOut(long timeOut, TimeUnit timeUnit) {
implicitlyWaitTimeOut = timeOut;
this.timeUnit = timeUnit;
timeOutContainer.resetImplicitlyWaitTimeOut(timeOut, timeUnit);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.appium.java_client.pagefactory;

import java.util.concurrent.TimeUnit;

/**
* Instances of this class contain
* implicit time outs which are used by {@link AppiumElementLocator}
*/
class TimeOutContainer implements ResetsImplicitlyWaitTimeOut{
private long timeOutValue;
private TimeUnit timeUnit;

TimeOutContainer(long initialTimeOutValue, TimeUnit initialTimeUnit){
this.timeOutValue = initialTimeOutValue;
this.timeUnit = initialTimeUnit;
}

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

long getTimeValue(){
return timeOutValue;
}

TimeUnit getTimeUnitValue(){
return timeUnit;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package io.appium.java_client.pagefactory_tests;

import io.appium.java_client.pagefactory.AppiumFieldDecorator;

import java.util.Calendar;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class TimeOutResetTest {
private WebDriver driver;
private final static long ACCEPTABLE_DELTA_MILLS = 500;

/**
* Default time out parameters
*/

private static long DEFAULT_IMPLICITLY_WAIT_TIMEOUT = 1;
private static TimeUnit DEFAULT_TIMEUNIT = TimeUnit.SECONDS;

@FindBy(className = "ClassWhichDoesNotExist")
private List<WebElement> stubElements;
private AppiumFieldDecorator afd;

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

PageFactory.initElements(afd, this);
}

@After
public void tearDown() throws Exception {
driver.quit();
}

private static void checkTimeDifference(long etalonTime,
TimeUnit etalonTimeUnit, long currentMillis) {
long etalonMillis = TimeUnit.MILLISECONDS.convert(etalonTime,
etalonTimeUnit);
try{
Assert.assertEquals(true,
((currentMillis - etalonMillis) < ACCEPTABLE_DELTA_MILLS)
&& ((currentMillis - etalonMillis) >= 0));
}
catch (Error e){
String message = String.valueOf(etalonTime) + " " + etalonTimeUnit.toString() + " current duration in millis " +
String.valueOf(currentMillis) + " Failed";
throw new RuntimeException(message, e);
}
}

private long getBenchMark() {
long startMark = Calendar.getInstance().getTimeInMillis();
stubElements.size();
long endMark = Calendar.getInstance().getTimeInMillis();
return endMark - startMark;
}

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

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

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

}

}