Skip to content

Commit ddac8ff

Browse files
committed
Merge pull request #210 from TikhomirovSergey/#203-fix
Fix #203
2 parents fdefd65 + 1dbcbe2 commit ddac8ff

File tree

11 files changed

+139
-82
lines changed

11 files changed

+139
-82
lines changed

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

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

54-
private final TimeOutContainer timeOutContainer;
54+
private final TimeOutDuration timeOutDuration;
5555

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

7979
AppiumAnnotations annotations = new AppiumAnnotations(field, platform,
8080
automation);
81-
this.timeOutContainer = timeOutContainer;
81+
if (field.isAnnotationPresent(WithTimeout.class)){
82+
WithTimeout withTimeout = field.getAnnotation(WithTimeout.class);
83+
this.timeOutDuration = new TimeOutDuration(withTimeout.time(), withTimeout.unit());
84+
}
85+
else
86+
this.timeOutDuration = timeOutDuration;
8287
shouldCache = annotations.isLookupCached();
8388
by = annotations.buildBy();
8489
}
@@ -98,14 +103,14 @@ private List<WebElement> waitFor() {
98103
try {
99104
changeImplicitlyWaitTimeOut(0, TimeUnit.SECONDS);
100105
FluentWait<By> wait = new FluentWait<By>(by);
101-
wait.withTimeout(timeOutContainer.getTimeValue(),
102-
timeOutContainer.getTimeUnitValue());
106+
wait.withTimeout(timeOutDuration.getTime(),
107+
timeOutDuration.getTimeUnit());
103108
return wait.until(new WaitingFunction(searchContext));
104109
} catch (TimeoutException e) {
105110
return new ArrayList<WebElement>();
106111
} finally {
107-
changeImplicitlyWaitTimeOut(timeOutContainer.getTimeValue(),
108-
timeOutContainer.getTimeUnitValue());
112+
changeImplicitlyWaitTimeOut(timeOutDuration.getTime(),
113+
timeOutDuration.getTimeUnit());
109114
}
110115
}
111116

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
package io.appium.java_client.pagefactory;
22

33
import java.lang.reflect.Field;
4-
import java.util.concurrent.TimeUnit;
54

65
import org.openqa.selenium.SearchContext;
76
import org.openqa.selenium.support.pagefactory.ElementLocator;
87
import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;
98

10-
class AppiumElementLocatorFactory implements ElementLocatorFactory, ResetsImplicitlyWaitTimeOut {
9+
class AppiumElementLocatorFactory implements ElementLocatorFactory {
1110
private final SearchContext searchContext;
12-
private final TimeOutContainer timeOutContainer;
11+
private final TimeOutDuration timeOutDuration;
1312

1413
public AppiumElementLocatorFactory(SearchContext searchContext,
15-
long implicitlyWaitTimeOut, TimeUnit timeUnit) {
14+
TimeOutDuration timeOutDuration) {
1615
this.searchContext = searchContext;
17-
this.timeOutContainer = new TimeOutContainer(implicitlyWaitTimeOut, timeUnit);
16+
this.timeOutDuration = timeOutDuration;
1817
}
1918

2019
public AppiumElementLocatorFactory(SearchContext searchContext) {
21-
this(searchContext, AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT,
22-
AppiumFieldDecorator.DEFAULT_TIMEUNIT);
20+
this(searchContext, new TimeOutDuration(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT,
21+
AppiumFieldDecorator.DEFAULT_TIMEUNIT));
2322
}
2423

2524
public ElementLocator createLocator(Field field) {
26-
return new AppiumElementLocator(searchContext, field, timeOutContainer);
27-
}
28-
29-
@Override
30-
public void resetImplicitlyWaitTimeOut(long timeOut, TimeUnit timeUnit) {
31-
timeOutContainer.resetImplicitlyWaitTimeOut(timeOut, timeUnit);
25+
return new AppiumElementLocator(searchContext, field, timeOutDuration);
3226
}
3327
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.appium.java_client.pagefactory;
22

3-
import io.appium.java_client.AppiumDriver;
43
import io.appium.java_client.MobileElement;
54
import io.appium.java_client.TouchableElement;
65
import io.appium.java_client.android.AndroidDriver;
@@ -31,7 +30,7 @@
3130
* {@link MobileElement}, {@link AndroidElement} and {@link IOSElement} are allowed
3231
* to use with this decorator
3332
*/
34-
public class AppiumFieldDecorator implements FieldDecorator, ResetsImplicitlyWaitTimeOut {
33+
public class AppiumFieldDecorator implements FieldDecorator {
3534

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

6766
public AppiumFieldDecorator(SearchContext context, long implicitlyWaitTimeOut, TimeUnit timeUnit) {
6867
this.context = context;
69-
factory = new AppiumElementLocatorFactory(this.context, implicitlyWaitTimeOut, timeUnit);
68+
factory = new AppiumElementLocatorFactory(this.context, new TimeOutDuration(implicitlyWaitTimeOut, timeUnit));
7069
}
70+
71+
public AppiumFieldDecorator(SearchContext context, TimeOutDuration timeOutDuration) {
72+
this.context = context;
73+
factory = new AppiumElementLocatorFactory(this.context, timeOutDuration);
74+
}
7175

7276
public AppiumFieldDecorator(SearchContext context) {
7377
this.context = context;
@@ -150,9 +154,4 @@ private List<WebElement> proxyForListLocator(
150154
return ProxyFactory.getEnhancedProxy(ArrayList.class,
151155
elementInterceptor);
152156
}
153-
154-
@Override
155-
public void resetImplicitlyWaitTimeOut(long timeOut, TimeUnit timeUnit) {
156-
factory.resetImplicitlyWaitTimeOut(timeOut, timeUnit);
157-
}
158157
}

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

Lines changed: 0 additions & 7 deletions
This file was deleted.

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

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.appium.java_client.pagefactory;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
import static com.google.common.base.Preconditions.checkArgument;
6+
import static com.google.common.base.Preconditions.checkNotNull;
7+
8+
/**
9+
* Represents an duration of waiting for element rendering.
10+
*/
11+
public class TimeOutDuration {
12+
13+
private long time;
14+
private TimeUnit unit;
15+
16+
/**
17+
* @param time The amount of time.
18+
* @param unit The unit of time.
19+
*/
20+
public TimeOutDuration(long time, TimeUnit unit) {
21+
setTime(time, unit);
22+
}
23+
24+
public long getTime(){
25+
return time;
26+
}
27+
28+
public TimeUnit getTimeUnit(){
29+
return unit;
30+
}
31+
32+
public void setTime(long newTime){
33+
checkArgument(newTime >= 0, "Duration < 0: %d", newTime);
34+
time = newTime;
35+
}
36+
37+
public void setTime(TimeUnit newTimeUnit){
38+
checkNotNull(newTimeUnit);
39+
unit = newTimeUnit;
40+
}
41+
42+
public void setTime(long newTime, TimeUnit newTimeUnit){
43+
setTime(newTime);
44+
setTime(newTimeUnit);
45+
}
46+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.appium.java_client.pagefactory;
2+
3+
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
import java.util.concurrent.TimeUnit;
9+
10+
@Retention(RetentionPolicy.RUNTIME)
11+
@Target({ElementType.FIELD, ElementType.TYPE})
12+
/**
13+
This annotation is used when some element waits for time
14+
that differs from defined by default
15+
*/
16+
public @interface WithTimeout {
17+
long time();
18+
TimeUnit unit();
19+
}

src/test/java/io/appium/java_client/pagefactory_tests/AndroidPageObjectTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.List;
1919
import java.util.concurrent.TimeUnit;
2020

21-
import org.apache.xpath.operations.And;
2221
import org.junit.After;
2322
import org.junit.Assert;
2423
import org.junit.Before;
@@ -343,7 +342,8 @@ public void areTouchableElements(){
343342

344343
@Test
345344
public void isTheFieldAndroidElement(){
346-
AndroidElement androidElement = (AndroidElement) mobiletextVieW; //declared as MobileElement
345+
@SuppressWarnings("unused")
346+
AndroidElement androidElement = (AndroidElement) mobiletextVieW; //declared as MobileElement
347347
androidElement = (AndroidElement) androidTextView; //declared as WedElement
348348
androidElement = (AndroidElement) remotetextVieW; //declared as RemoteWedElement
349349
androidElement = (AndroidElement) touchabletextVieW; //declared as TouchABLEElement

src/test/java/io/appium/java_client/pagefactory_tests/IOSfMobileBrowserCompatibilityTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void setUp() throws Exception {
5151
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, MobileBrowserType.SAFARI);
5252
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.1");
5353
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone Simulator");
54-
driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
54+
driver = new IOSDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
5555
PageFactory.initElements(new AppiumFieldDecorator(driver, 5, TimeUnit.SECONDS), this);
5656
}
5757

src/test/java/io/appium/java_client/pagefactory_tests/TimeOutResetTest.java

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.util.List;
77
import java.util.concurrent.TimeUnit;
88

9+
import io.appium.java_client.pagefactory.TimeOutDuration;
10+
import io.appium.java_client.pagefactory.WithTimeout;
911
import org.junit.After;
1012
import org.junit.Assert;
1113
import org.junit.Before;
@@ -25,14 +27,20 @@ public class TimeOutResetTest {
2527
@FindAll({@FindBy(className = "ClassWhichDoesNotExist"),
2628
@FindBy(className = "OneAnotherClassWhichDoesNotExist")})
2729
private List<WebElement> stubElements;
28-
private AppiumFieldDecorator afd;
30+
31+
@WithTimeout(time = 5, unit = TimeUnit.SECONDS)
32+
@FindAll({@FindBy(className = "ClassWhichDoesNotExist"),
33+
@FindBy(className = "OneAnotherClassWhichDoesNotExist")})
34+
private List<WebElement> stubElements2;
35+
36+
private TimeOutDuration timeOutDuration;
2937

3038
@Before
3139
public void setUp() throws Exception {
3240
driver = new FirefoxDriver();
33-
afd = new AppiumFieldDecorator(driver);
34-
35-
PageFactory.initElements(afd, this);
41+
timeOutDuration = new TimeOutDuration(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT,
42+
AppiumFieldDecorator.DEFAULT_TIMEUNIT);
43+
PageFactory.initElements(new AppiumFieldDecorator(driver, timeOutDuration), this);
3644
}
3745

3846
@After
@@ -56,7 +64,7 @@ private static void checkTimeDifference(long etalonTime,
5664
}
5765
}
5866

59-
private long getBenchMark() {
67+
private long getBenchMark(List<WebElement> stubElements) {
6068
long startMark = Calendar.getInstance().getTimeInMillis();
6169
stubElements.size();
6270
long endMark = Calendar.getInstance().getTimeInMillis();
@@ -66,20 +74,45 @@ private long getBenchMark() {
6674
@Test
6775
public void test() {
6876
checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, AppiumFieldDecorator.DEFAULT_TIMEUNIT,
69-
getBenchMark());
77+
getBenchMark(stubElements));
7078
System.out.println(String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT)
7179
+ " " + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine");
7280

73-
afd.resetImplicitlyWaitTimeOut(15500000, TimeUnit.MICROSECONDS);
74-
checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark());
81+
timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS);
82+
checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(stubElements));
7583
System.out.println("Change time: " + String.valueOf(15500000) + " "
7684
+ TimeUnit.MICROSECONDS.toString() + ": Fine");
7785

78-
afd.resetImplicitlyWaitTimeOut(3, TimeUnit.SECONDS);
79-
checkTimeDifference(3, TimeUnit.SECONDS, getBenchMark());
86+
timeOutDuration.setTime(3, TimeUnit.SECONDS);
87+
checkTimeDifference(3, TimeUnit.SECONDS, getBenchMark(stubElements));
8088
System.out.println("Change time: " + String.valueOf(3) + " "
8189
+ TimeUnit.SECONDS.toString() + ": Fine");
8290

8391
}
8492

93+
@Test
94+
public void test2() {
95+
checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, AppiumFieldDecorator.DEFAULT_TIMEUNIT,
96+
getBenchMark(stubElements));
97+
System.out.println(String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT)
98+
+ " " + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine");
99+
100+
checkTimeDifference(5, TimeUnit.SECONDS,
101+
getBenchMark(stubElements2));
102+
System.out.println(String.valueOf(5)
103+
+ " " + TimeUnit.SECONDS.toString() + ": Fine");
104+
105+
106+
timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS);
107+
checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(stubElements));
108+
System.out.println("Change time: " + String.valueOf(15500000) + " "
109+
+ TimeUnit.MICROSECONDS.toString() + ": Fine");
110+
111+
checkTimeDifference(5, TimeUnit.SECONDS,
112+
getBenchMark(stubElements2));
113+
System.out.println(String.valueOf(5)
114+
+ " " + TimeUnit.SECONDS.toString() + ": Fine");
115+
116+
}
117+
85118
}

0 commit comments

Comments
 (0)