Skip to content

Commit 2da0108

Browse files
appium#846: migration to java.time.Duration
- tests were updated
1 parent 9cdff3d commit 2da0108

7 files changed

Lines changed: 36 additions & 45 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public class AppiumFieldDecorator implements FieldDecorator {
7979
private final Duration duration;
8080

8181

82+
@Deprecated
8283
public AppiumFieldDecorator(SearchContext context, long timeout,
8384
TimeUnit timeUnit) {
8485
this(context, new TimeOutDuration(timeout, timeUnit));

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
@@ -17,6 +17,7 @@
1717
package io.appium.java_client.pagefactory_tests;
1818

1919
import static io.appium.java_client.pagefactory.LocatorGroupStrategy.ALL_POSSIBLE;
20+
import static java.time.Duration.ofSeconds;
2021
import static junit.framework.TestCase.assertNotNull;
2122
import static org.junit.Assert.assertEquals;
2223
import static org.junit.Assert.assertNotEquals;
@@ -46,7 +47,6 @@
4647

4748
import java.util.ArrayList;
4849
import java.util.List;
49-
import java.util.concurrent.TimeUnit;
5050

5151
public class AndroidPageObjectTest extends BaseAndroidTest {
5252

@@ -242,7 +242,7 @@ public class AndroidPageObjectTest extends BaseAndroidTest {
242242
@Before public void setUp() {
243243
if (!populated) {
244244
//This time out is set because test can be run on slow Android SDK emulator
245-
PageFactory.initElements(new AppiumFieldDecorator(driver, 5, TimeUnit.SECONDS), this);
245+
PageFactory.initElements(new AppiumFieldDecorator(driver, ofSeconds(5)), this);
246246
}
247247

248248
populated = true;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static io.appium.java_client.ChromeDriverPathUtil.getChromeDriver;
2020
import static io.appium.java_client.pagefactory.LocatorGroupStrategy.ALL_POSSIBLE;
2121
import static java.lang.System.setProperty;
22+
import static java.time.Duration.ofSeconds;
2223
import static org.junit.Assert.assertEquals;
2324
import static org.junit.Assert.assertNotEquals;
2425

@@ -39,7 +40,6 @@
3940

4041
import java.io.File;
4142
import java.util.List;
42-
import java.util.concurrent.TimeUnit;
4343

4444
public class DesktopBrowserCompatibilityTest {
4545

@@ -64,7 +64,7 @@ public class DesktopBrowserCompatibilityTest {
6464
WebDriver driver = new ChromeDriver();
6565
try {
6666
PageFactory
67-
.initElements(new AppiumFieldDecorator(driver, 15, TimeUnit.SECONDS),
67+
.initElements(new AppiumFieldDecorator(driver, ofSeconds(15)),
6868
this);
6969
driver.get(new File("src/test/java/io/appium/java_client/hello appium - saved page.htm")
7070
.toURI().toString());

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
import org.openqa.selenium.support.PageFactory;
3737

3838
import java.util.List;
39-
import java.util.concurrent.TimeUnit;
39+
40+
import static java.time.Duration.ofSeconds;
4041

4142
public class IOSMobileBrowserCompatibilityTest {
4243

@@ -65,7 +66,7 @@ public class IOSMobileBrowserCompatibilityTest {
6566
//sometimes environment has performance problems
6667
capabilities.setCapability(IOSMobileCapabilityType.LAUNCH_TIMEOUT, 500000);
6768
driver = new IOSDriver<>(service.getUrl(), capabilities);
68-
PageFactory.initElements(new AppiumFieldDecorator(driver, 5, TimeUnit.SECONDS), this);
69+
PageFactory.initElements(new AppiumFieldDecorator(driver, ofSeconds(5)), this);
6970
}
7071

7172
/**

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@
3434
import org.openqa.selenium.support.FindBys;
3535
import org.openqa.selenium.support.PageFactory;
3636

37+
import java.time.Duration;
3738
import java.util.List;
3839
import java.util.concurrent.TimeUnit;
3940

41+
import static java.time.Duration.ofSeconds;
42+
4043
public class MobileBrowserCompatibilityTest {
4144

4245
private WebDriver driver;
@@ -67,7 +70,7 @@ public class MobileBrowserCompatibilityTest {
6770
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, MobileBrowserType.BROWSER);
6871
driver = new AndroidDriver<RemoteWebElement>(service.getUrl(), capabilities);
6972
//This time out is set because test can be run on slow Android SDK emulator
70-
PageFactory.initElements(new AppiumFieldDecorator(driver, 5, TimeUnit.SECONDS), this);
73+
PageFactory.initElements(new AppiumFieldDecorator(driver, ofSeconds(5)), this);
7174
}
7275

7376
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.appium.java_client.pagefactory_tests;
1818

1919
import static io.appium.java_client.pagefactory.LocatorGroupStrategy.ALL_POSSIBLE;
20+
import static java.time.Duration.ofSeconds;
2021
import static org.junit.Assert.assertEquals;
2122
import static org.junit.Assert.assertNotEquals;
2223
import static org.junit.Assert.assertNotNull;
@@ -46,7 +47,6 @@
4647

4748
import java.io.File;
4849
import java.util.List;
49-
import java.util.concurrent.TimeUnit;
5050

5151
public class SelendroidModeTest {
5252
private static int SELENDROID_PORT = 9999;
@@ -130,7 +130,7 @@ public class SelendroidModeTest {
130130
@Before public void setUp() {
131131
if (!populated) {
132132
//This time out is set because test can be run on slow Android SDK emulator
133-
PageFactory.initElements(new AppiumFieldDecorator(driver, 5, TimeUnit.SECONDS), this);
133+
PageFactory.initElements(new AppiumFieldDecorator(driver, ofSeconds(5)), this);
134134
}
135135

136136
populated = true;

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

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,19 @@
1717
package io.appium.java_client.pagefactory_tests;
1818

1919
import 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;
2221
import static java.lang.Math.abs;
2322
import static java.lang.String.format;
2423
import static java.lang.System.currentTimeMillis;
2524
import 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;
2826
import static java.util.concurrent.TimeUnit.SECONDS;
27+
import static org.apache.commons.lang3.time.DurationFormatUtils.formatDuration;
2928
import static org.hamcrest.Matchers.lessThanOrEqualTo;
3029
import static org.junit.Assert.assertThat;
3130
import static org.openqa.selenium.support.PageFactory.initElements;
3231

3332
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
34-
import io.appium.java_client.pagefactory.TimeOutDuration;
3533
import io.appium.java_client.pagefactory.WithTimeout;
3634
import org.junit.After;
3735
import org.junit.Before;
@@ -43,13 +41,12 @@
4341
import org.openqa.selenium.support.FindAll;
4442
import org.openqa.selenium.support.FindBy;
4543

44+
import java.time.Duration;
4645
import java.util.List;
47-
import java.util.concurrent.TimeUnit;
4846

4947
public 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

Comments
 (0)