1717package io .appium .java_client .pagefactory_tests ;
1818
1919import static io .appium .java_client .ChromeDriverPathUtil .getChromeDriver ;
20- import static io .appium .java_client .pagefactory .AppiumFieldDecorator .DEFAULT_TIMEOUT ;
21- import static io .appium .java_client .pagefactory .AppiumFieldDecorator .DEFAULT_TIMEUNIT ;
20+ import static io .appium .java_client .pagefactory .AppiumFieldDecorator .DEFAULT_WAITING_TIMEOUT ;
2221import static java .lang .Math .abs ;
2322import static java .lang .String .format ;
2423import static java .lang .System .currentTimeMillis ;
2524import static java .lang .System .setProperty ;
26- import static java .util .concurrent .TimeUnit .MICROSECONDS ;
27- import static java .util .concurrent .TimeUnit .MILLISECONDS ;
25+ import static java .time .Duration .ofSeconds ;
2826import static java .util .concurrent .TimeUnit .SECONDS ;
27+ import static org .apache .commons .lang3 .time .DurationFormatUtils .formatDuration ;
2928import static org .hamcrest .Matchers .lessThanOrEqualTo ;
3029import static org .junit .Assert .assertThat ;
3130import static org .openqa .selenium .support .PageFactory .initElements ;
3231
3332import io .appium .java_client .pagefactory .AppiumFieldDecorator ;
34- import io .appium .java_client .pagefactory .TimeOutDuration ;
3533import io .appium .java_client .pagefactory .WithTimeout ;
3634import org .junit .After ;
3735import org .junit .Before ;
4341import org .openqa .selenium .support .FindAll ;
4442import org .openqa .selenium .support .FindBy ;
4543
44+ import java .time .Duration ;
4645import java .util .List ;
47- import java .util .concurrent .TimeUnit ;
4846
4947public class TimeoutTest {
5048
5149 private static final long ACCEPTABLE_TIME_DIFF_MS = 1500 ;
52- private static final String MESSAGE = "Check difference from the expected waiting duration %s %s" ;
5350
5451 private WebDriver driver ;
5552
@@ -63,10 +60,10 @@ public class TimeoutTest {
6360 @ FindBy (className = "OneAnotherClassWhichDoesNotExist" )})
6461 private List <WebElement > stubElements2 ;
6562
66- private TimeOutDuration timeOutDuration ;
63+ private Duration timeOutDuration ;
6764
68- private static long getExpectedMillis (long value , TimeUnit sourceTimeUnit ) {
69- return MILLISECONDS . convert ( value , sourceTimeUnit );
65+ private static long getExpectedMillis (Duration duration ) {
66+ return duration . toMillis ( );
7067 }
7168
7269 private static long getPerformanceDiff (long expectedMs , Runnable runnable ) {
@@ -76,14 +73,19 @@ private static long getPerformanceDiff(long expectedMs, Runnable runnable) {
7673 return abs (expectedMs - (endMark - startMark ));
7774 }
7875
76+ private static String assertionMessage (Duration expectedDuration ) {
77+ return format ("Check difference from the expected waiting duration %s" ,
78+ formatDuration (expectedDuration .toMillis (), "H:mm:ss:SSS" , true ));
79+ }
80+
7981 /**
8082 * The setting up.
8183 */
8284 @ Before public void setUp () {
8385 setProperty (ChromeDriverService .CHROME_DRIVER_EXE_PROPERTY ,
8486 getChromeDriver ().getAbsolutePath ());
8587 driver = new ChromeDriver ();
86- timeOutDuration = new TimeOutDuration ( DEFAULT_TIMEOUT , DEFAULT_TIMEUNIT ) ;
88+ timeOutDuration = DEFAULT_WAITING_TIMEOUT ;
8789 initElements (new AppiumFieldDecorator (driver , timeOutDuration ), this );
8890 }
8991
@@ -94,39 +96,23 @@ private static long getPerformanceDiff(long expectedMs, Runnable runnable) {
9496 driver .quit ();
9597 }
9698
97- @ Test public void defaultTimeOutTest () {
98- assertThat (format (MESSAGE , DEFAULT_TIMEOUT , DEFAULT_TIMEUNIT ),
99- getPerformanceDiff (getExpectedMillis (DEFAULT_TIMEOUT , DEFAULT_TIMEUNIT ), () -> stubElements .size ()),
100- lessThanOrEqualTo (ACCEPTABLE_TIME_DIFF_MS ));
101-
102- timeOutDuration .setTime (15500000 , MICROSECONDS );
103- assertThat (format (MESSAGE , 15500000 , MICROSECONDS ),
104- getPerformanceDiff (getExpectedMillis (15500000 , MICROSECONDS ), () -> stubElements .size ()),
105- lessThanOrEqualTo (ACCEPTABLE_TIME_DIFF_MS ));
106-
107- timeOutDuration .setTime (3 , SECONDS );
108- assertThat (format (MESSAGE , 3 , SECONDS ),
109- getPerformanceDiff (getExpectedMillis (3 , SECONDS ), () -> stubElements .size ()),
110- lessThanOrEqualTo (ACCEPTABLE_TIME_DIFF_MS ));
111- }
112-
11399 @ Test public void withCustomizedTimeOutTest () {
114- assertThat (format ( MESSAGE , DEFAULT_TIMEOUT , DEFAULT_TIMEUNIT ),
115- getPerformanceDiff (getExpectedMillis (DEFAULT_TIMEOUT , DEFAULT_TIMEUNIT ), () -> stubElements .size ()),
100+ assertThat (assertionMessage ( DEFAULT_WAITING_TIMEOUT ),
101+ getPerformanceDiff (getExpectedMillis (DEFAULT_WAITING_TIMEOUT ), () -> stubElements .size ()),
116102 lessThanOrEqualTo (ACCEPTABLE_TIME_DIFF_MS ));
117103
118- assertThat (format ( MESSAGE , 5 , SECONDS ),
119- getPerformanceDiff (getExpectedMillis (5 , SECONDS ), () -> stubElements2 .size ()),
104+ assertThat (assertionMessage ( ofSeconds ( 5 ) ),
105+ getPerformanceDiff (getExpectedMillis (ofSeconds ( 5 ) ), () -> stubElements2 .size ()),
120106 lessThanOrEqualTo (ACCEPTABLE_TIME_DIFF_MS ));
121107
122- timeOutDuration .setTime ( 15500000 , MICROSECONDS );
108+ timeOutDuration .plus ( ofSeconds ( 10 ) );
123109
124- assertThat (format ( MESSAGE , 15500000 , MICROSECONDS ),
125- getPerformanceDiff (getExpectedMillis (15500000 , MICROSECONDS ), () -> stubElements .size ()),
110+ assertThat (assertionMessage ( timeOutDuration ),
111+ getPerformanceDiff (getExpectedMillis (timeOutDuration ), () -> stubElements .size ()),
126112 lessThanOrEqualTo (ACCEPTABLE_TIME_DIFF_MS ));
127113
128- assertThat (format ( MESSAGE , 5 , SECONDS ),
129- getPerformanceDiff (getExpectedMillis (5 , SECONDS ), () -> stubElements2 .size ()),
114+ assertThat (assertionMessage ( ofSeconds ( 5 ) ),
115+ getPerformanceDiff (getExpectedMillis (ofSeconds ( 5 ) ), () -> stubElements2 .size ()),
130116 lessThanOrEqualTo (ACCEPTABLE_TIME_DIFF_MS ));
131117 }
132118}
0 commit comments