Skip to content

Commit 3efe96e

Browse files
TikhomirovSergeyJonahss
authored andcommitted
1 parent 15ba3c2 commit 3efe96e

File tree

4 files changed

+132
-15
lines changed

4 files changed

+132
-15
lines changed

src/main/java/io/appium/java_client/pagefactory/AppiumElementLocator.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public List<WebElement> apply(By by) {
5252
private WebElement cachedElement;
5353
private List<WebElement> cachedElementList;
5454

55-
private final long implicitlyWaitTimeOut;
56-
private final TimeUnit timeUnit ;
55+
private final TimeOutContainer timeOutContainer;
5756

5857
/**
5958
* Creates a new mobile element locator. It instantiates {@link WebElement}
@@ -64,17 +63,16 @@ public List<WebElement> apply(By by) {
6463
* @param field
6564
* The field on the Page Object that will hold the located value
6665
*/
67-
public AppiumElementLocator(SearchContext searchContext, Field field,
68-
long implicitlyWaitTimeOut, TimeUnit timeUnit) {
66+
AppiumElementLocator(SearchContext searchContext, Field field,
67+
TimeOutContainer timeOutContainer) {
6968
this.searchContext = searchContext;
7069
// All known webdrivers implement HasCapabilities
7170
String platform = String
7271
.valueOf(((HasCapabilities) unpackWebDriverFromSearchContext())
7372
.getCapabilities().getCapability(
7473
MobileCapabilityType.PLATFORM_NAME));
7574
AppiumAnnotations annotations = new AppiumAnnotations(field, platform);
76-
this.implicitlyWaitTimeOut = implicitlyWaitTimeOut;
77-
this.timeUnit = timeUnit;
75+
this.timeOutContainer = timeOutContainer;
7876
shouldCache = annotations.isLookupCached();
7977
by = annotations.buildBy();
8078
}
@@ -109,14 +107,15 @@ private List<WebElement> waitFor(){
109107
try{
110108
changeImplicitlyWaitTimeOut(0, TimeUnit.SECONDS);
111109
FluentWait<By> wait = new FluentWait<By>(by);
112-
wait.withTimeout(implicitlyWaitTimeOut, timeUnit);
110+
wait.withTimeout(timeOutContainer.getTimeValue(), timeOutContainer.getTimeUnitValue());
113111
return wait.until(new WaitingFunction(searchContext));
114112
}
115113
catch (TimeoutException e){
116114
return new ArrayList<WebElement>();
117115
}
118116
finally{
119-
changeImplicitlyWaitTimeOut(implicitlyWaitTimeOut, timeUnit);
117+
changeImplicitlyWaitTimeOut(timeOutContainer.getTimeValue(),
118+
timeOutContainer.getTimeUnitValue());
120119
}
121120
}
122121

src/main/java/io/appium/java_client/pagefactory/AppiumElementLocatorFactory.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,24 @@ class AppiumElementLocatorFactory implements ElementLocatorFactory, ResetsImplic
1212
private static TimeUnit DEFAULT_TIMEUNIT = TimeUnit.SECONDS;
1313

1414
private final SearchContext searchContext;
15-
private long implicitlyWaitTimeOut;
16-
private TimeUnit timeUnit;
15+
private final TimeOutContainer timeOutContainer;
1716

1817
public AppiumElementLocatorFactory(SearchContext searchContext,
1918
long implicitlyWaitTimeOut, TimeUnit timeUnit) {
2019
this.searchContext = searchContext;
21-
this.implicitlyWaitTimeOut = implicitlyWaitTimeOut;
22-
this.timeUnit = timeUnit;
20+
this.timeOutContainer = new TimeOutContainer(implicitlyWaitTimeOut, timeUnit);
2321
}
2422

2523
public AppiumElementLocatorFactory(SearchContext searchContext) {
2624
this(searchContext, DEFAULT_IMPLICITLY_WAIT_TIMEOUT, DEFAULT_TIMEUNIT);
2725
}
2826

2927
public ElementLocator createLocator(Field field) {
30-
return new AppiumElementLocator(searchContext, field, implicitlyWaitTimeOut, timeUnit);
28+
return new AppiumElementLocator(searchContext, field, timeOutContainer);
3129
}
3230

3331
@Override
3432
public void resetImplicitlyWaitTimeOut(long timeOut, TimeUnit timeUnit) {
35-
implicitlyWaitTimeOut = timeOut;
36-
this.timeUnit = timeUnit;
33+
timeOutContainer.resetImplicitlyWaitTimeOut(timeOut, timeUnit);
3734
}
3835
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.appium.java_client.pagefactory;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* Instances of this class contain
7+
* implicit time outs which are used by {@link AppiumElementLocator}
8+
*/
9+
class TimeOutContainer implements ResetsImplicitlyWaitTimeOut{
10+
private long timeOutValue;
11+
private TimeUnit timeUnit;
12+
13+
TimeOutContainer(long initialTimeOutValue, TimeUnit initialTimeUnit){
14+
this.timeOutValue = initialTimeOutValue;
15+
this.timeUnit = initialTimeUnit;
16+
}
17+
18+
@Override
19+
public void resetImplicitlyWaitTimeOut(long timeOut, TimeUnit timeUnit) {
20+
this.timeOutValue = timeOut;
21+
this.timeUnit = timeUnit;
22+
}
23+
24+
long getTimeValue(){
25+
return timeOutValue;
26+
}
27+
28+
TimeUnit getTimeUnitValue(){
29+
return timeUnit;
30+
}
31+
32+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package io.appium.java_client.pagefactory_tests;
2+
3+
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
4+
5+
import java.util.Calendar;
6+
import java.util.List;
7+
import java.util.concurrent.TimeUnit;
8+
9+
import org.junit.After;
10+
import org.junit.Assert;
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
import org.openqa.selenium.WebDriver;
14+
import org.openqa.selenium.WebElement;
15+
import org.openqa.selenium.firefox.FirefoxDriver;
16+
import org.openqa.selenium.support.FindBy;
17+
import org.openqa.selenium.support.PageFactory;
18+
19+
public class TimeOutResetTest {
20+
private WebDriver driver;
21+
private final static long ACCEPTABLE_DELTA_MILLS = 500;
22+
23+
/**
24+
* Default time out parameters
25+
*/
26+
27+
private static long DEFAULT_IMPLICITLY_WAIT_TIMEOUT = 1;
28+
private static TimeUnit DEFAULT_TIMEUNIT = TimeUnit.SECONDS;
29+
30+
@FindBy(className = "ClassWhichDoesNotExist")
31+
private List<WebElement> stubElements;
32+
private AppiumFieldDecorator afd;
33+
34+
@Before
35+
public void setUp() throws Exception {
36+
driver = new FirefoxDriver();
37+
afd = new AppiumFieldDecorator(driver);
38+
39+
PageFactory.initElements(afd, this);
40+
}
41+
42+
@After
43+
public void tearDown() throws Exception {
44+
driver.quit();
45+
}
46+
47+
private static void checkTimeDifference(long etalonTime,
48+
TimeUnit etalonTimeUnit, long currentMillis) {
49+
long etalonMillis = TimeUnit.MILLISECONDS.convert(etalonTime,
50+
etalonTimeUnit);
51+
try{
52+
Assert.assertEquals(true,
53+
((currentMillis - etalonMillis) < ACCEPTABLE_DELTA_MILLS)
54+
&& ((currentMillis - etalonMillis) >= 0));
55+
}
56+
catch (Error e){
57+
String message = String.valueOf(etalonTime) + " " + etalonTimeUnit.toString() + " current duration in millis " +
58+
String.valueOf(currentMillis) + " Failed";
59+
throw new RuntimeException(message, e);
60+
}
61+
}
62+
63+
private long getBenchMark() {
64+
long startMark = Calendar.getInstance().getTimeInMillis();
65+
stubElements.size();
66+
long endMark = Calendar.getInstance().getTimeInMillis();
67+
return endMark - startMark;
68+
}
69+
70+
@Test
71+
public void test() {
72+
checkTimeDifference(DEFAULT_IMPLICITLY_WAIT_TIMEOUT, DEFAULT_TIMEUNIT,
73+
getBenchMark());
74+
System.out.println(String.valueOf(DEFAULT_IMPLICITLY_WAIT_TIMEOUT)
75+
+ " " + DEFAULT_TIMEUNIT.toString() + ": Fine");
76+
77+
afd.resetImplicitlyWaitTimeOut(15500000, TimeUnit.MICROSECONDS);
78+
checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark());
79+
System.out.println("Change time: " + String.valueOf(15500000) + " "
80+
+ TimeUnit.MICROSECONDS.toString() + ": Fine");
81+
82+
afd.resetImplicitlyWaitTimeOut(3, TimeUnit.SECONDS);
83+
checkTimeDifference(3, TimeUnit.SECONDS, getBenchMark());
84+
System.out.println("Change time: " + String.valueOf(3) + " "
85+
+ TimeUnit.SECONDS.toString() + ": Fine");
86+
87+
}
88+
89+
}

0 commit comments

Comments
 (0)