Skip to content

Commit 2cc72ae

Browse files
Merge pull request #611 from mykola-mokhnach/duration_arg
Use java.time.Duration to represent time deltas instead of elementary types
2 parents 66afce3 + d1026e9 commit 2cc72ae

File tree

14 files changed

+213
-33
lines changed

14 files changed

+213
-33
lines changed

src/main/java/io/appium/java_client/InteractsWithApps.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import com.google.common.collect.ImmutableMap;
2929

30+
import java.time.Duration;
3031
import java.util.AbstractMap;
3132

3233
public interface InteractsWithApps extends ExecutesMethod {
@@ -68,11 +69,24 @@ default void resetApp() {
6869
* Runs the current app as a background app for the number of seconds
6970
* requested. This is a synchronous method, it returns after the back has
7071
* been returned to the foreground.
72+
* This method is deprecated. Please use {@link #runAppInBackground(Duration)} instead.
7173
*
7274
* @param seconds Number of seconds to run App in background.
7375
*/
76+
@Deprecated
7477
default void runAppInBackground(int seconds) {
75-
execute(RUN_APP_IN_BACKGROUND, ImmutableMap.of("seconds", seconds));
78+
runAppInBackground(Duration.ofSeconds(seconds));
79+
}
80+
81+
/**
82+
* Runs the current app as a background app for the time
83+
* requested. This is a synchronous method, it returns after the back has
84+
* been returned to the foreground.
85+
*
86+
* @param duration The time to run App in background. Minimum time resolution is one second
87+
*/
88+
default void runAppInBackground(Duration duration) {
89+
execute(RUN_APP_IN_BACKGROUND, ImmutableMap.of("seconds", duration.getSeconds()));
7690
}
7791

7892
/**

src/main/java/io/appium/java_client/MobileCommand.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.commons.lang3.StringUtils;
2222
import org.openqa.selenium.remote.http.HttpMethod;
2323

24+
import java.time.Duration;
2425
import java.util.AbstractMap;
2526
import java.util.HashMap;
2627
import java.util.Map;
@@ -330,13 +331,27 @@ public static ImmutableMap<String, Object> prepareArguments(String[] params,
330331
/**
331332
* This method forms a {@link java.util.Map} of parameters for the
332333
* device locking.
334+
* The method is deprecated. Please use {@link #lockDeviceCommand(Duration)} instead.
333335
*
334336
* @param seconds seconds number of seconds to lock the screen for
335337
* @return a key-value pair. The key is the command name. The value is a
336338
* {@link java.util.Map} command arguments.
337339
*/
338-
public static Map.Entry<String, Map<String, ?>> lockDeviceCommand(int seconds) {
340+
@Deprecated
341+
public static Map.Entry<String, Map<String, ?>> lockDeviceCommand(int seconds) {
342+
return lockDeviceCommand(Duration.ofSeconds(seconds));
343+
}
344+
345+
/**
346+
* This method forms a {@link java.util.Map} of parameters for the
347+
* device locking.
348+
*
349+
* @param duration for how long to lock the screen for. Minimum time resolution is one second
350+
* @return a key-value pair. The key is the command name. The value is a
351+
* {@link java.util.Map} command arguments.
352+
*/
353+
public static Map.Entry<String, Map<String, ?>> lockDeviceCommand(Duration duration) {
339354
return new AbstractMap.SimpleEntry<>(
340-
LOCK, prepareArguments("seconds", seconds));
355+
LOCK, prepareArguments("seconds", duration.getSeconds()));
341356
}
342357
}

src/main/java/io/appium/java_client/SwipeElementDirection.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ static void checkXCoordinate(int x, Point location, Dimension size, int offSet)
191191

192192
/**
193193
* Creates the swiping action. It is supposed to be performed inside the given element.
194+
* The method is deprecated and has no effect.
194195
*
195196
* @param driver an instance that extends {@link AppiumDriver}
196197
* @param element the element that is going to be swiped
@@ -199,6 +200,7 @@ static void checkXCoordinate(int x, Point location, Dimension size, int offSet)
199200
* @param duration in milliseconds
200201
* @throws IllegalCoordinatesException when starting/ending coordinates are outside of the given element
201202
*/
203+
@Deprecated
202204
public void swipe(AppiumDriver<?> driver, MobileElement element, int offset1, int offset2,
203205
int duration) throws IllegalCoordinatesException {
204206
Point p = element.getCenter();

src/main/java/io/appium/java_client/TouchAction.java

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.openqa.selenium.WebElement;
2323
import org.openqa.selenium.internal.HasIdentity;
2424

25+
import java.time.Duration;
26+
2527
/**
2628
* Used for Webdriver 3 touch actions
2729
* See the Webriver 3 spec
@@ -197,13 +199,25 @@ public TouchAction waitAction() {
197199

198200
/**
199201
* Waits for specified amount of time to pass before continue to next touch action.
202+
* The method is deprecated. Please use {@link #waitAction(Duration)} instead.
200203
*
201204
* @param ms time in milliseconds to wait.
202205
* @return this TouchAction, for chaining.
203206
*/
207+
@Deprecated
204208
public TouchAction waitAction(int ms) {
209+
return waitAction(Duration.ofMillis(ms));
210+
}
211+
212+
/**
213+
* Waits for specified amount of time to pass before continue to next touch action.
214+
*
215+
* @param duration of the wait action. Minimum time reolution unit is one millisecond.
216+
* @return this TouchAction, for chaining.
217+
*/
218+
public TouchAction waitAction(Duration duration) {
205219
ActionParameter action = new ActionParameter("wait");
206-
action.addParameter("ms", ms);
220+
action.addParameter("ms", duration.toMillis());
207221
parameterBuilder.add(action);
208222
return this;
209223
}
@@ -222,14 +236,27 @@ public TouchAction longPress(WebElement el) {
222236

223237
/**
224238
* Press and hold the at the center of an element until the contextmenu event has fired.
239+
* This method is deprecated. Please use {@link #longPress(WebElement, Duration)} instead.
225240
*
226241
* @param el element to long-press.
227242
* @param duration of the long-press, in milliseconds.
228243
* @return this TouchAction, for chaining.
229244
*/
245+
@Deprecated
230246
public TouchAction longPress(WebElement el, int duration) {
247+
return longPress(el, Duration.ofMillis(duration));
248+
}
249+
250+
/**
251+
* Press and hold the at the center of an element until the contextmenu event has fired.
252+
*
253+
* @param el element to long-press.
254+
* @param duration of the long-press. Minimum time resolution unit is one millisecond.
255+
* @return this TouchAction, for chaining.
256+
*/
257+
public TouchAction longPress(WebElement el, Duration duration) {
231258
ActionParameter action = new ActionParameter("longPress", (HasIdentity) el);
232-
action.addParameter("duration", duration);
259+
action.addParameter("duration", duration.toMillis());
233260
parameterBuilder.add(action);
234261
return this;
235262
}
@@ -253,21 +280,37 @@ public TouchAction longPress(int x, int y) {
253280
/**
254281
* Press and hold the at an absolute position on the screen until the
255282
* contextmenu event has fired.
283+
* The method is deprecated. Please use {@link #longPress(int, int, Duration)} instead.
256284
*
257285
* @param x x coordinate.
258286
* @param y y coordinate.
259287
* @param duration of the long-press, in milliseconds.
260288
* @return this TouchAction, for chaining.
261289
*/
290+
@Deprecated
262291
public TouchAction longPress(int x, int y, int duration) {
292+
return longPress(x, y, Duration.ofMillis(duration));
293+
}
294+
295+
/**
296+
* Press and hold the at an absolute position on the screen until the
297+
* contextmenu event has fired.
298+
*
299+
* @param x x coordinate.
300+
* @param y y coordinate.
301+
* @param duration of the long-press. Minimum time resolution unit is one millisecond.
302+
* @return this TouchAction, for chaining.
303+
*/
304+
public TouchAction longPress(int x, int y, Duration duration) {
263305
ActionParameter action = new ActionParameter("longPress");
264306
action.addParameter("x", x);
265307
action.addParameter("y", y);
266-
action.addParameter("duration", duration);
308+
action.addParameter("duration", duration.toMillis());
267309
parameterBuilder.add(action);
268310
return this;
269311
}
270312

313+
271314
/**
272315
* Press and hold the at an elements upper-left corner, offset by the given amount,
273316
* until the contextmenu event has fired.
@@ -288,18 +331,34 @@ public TouchAction longPress(WebElement el, int x, int y) {
288331
/**
289332
* Press and hold the at an elements upper-left corner, offset by the
290333
* given amount, until the contextmenu event has fired.
334+
* The method is deprecated. Please use {@link #longPress(WebElement, int, int, Duration)} instead.
291335
*
292336
* @param el element to long-press.
293337
* @param x x offset.
294338
* @param y y offset.
295339
* @param duration of the long-press, in milliseconds.
296340
* @return this TouchAction, for chaining.
297341
*/
342+
@Deprecated
298343
public TouchAction longPress(WebElement el, int x, int y, int duration) {
344+
return longPress(el, x, y, Duration.ofMillis(duration));
345+
}
346+
347+
/**
348+
* Press and hold the at an elements upper-left corner, offset by the
349+
* given amount, until the contextmenu event has fired.
350+
*
351+
* @param el element to long-press.
352+
* @param x x offset.
353+
* @param y y offset.
354+
* @param duration of the long-press. Minimum time resolution unit is one millisecond.
355+
* @return this TouchAction, for chaining.
356+
*/
357+
public TouchAction longPress(WebElement el, int x, int y, Duration duration) {
299358
ActionParameter action = new ActionParameter("longPress", (HasIdentity) el);
300359
action.addParameter("x", x);
301360
action.addParameter("y", y);
302-
action.addParameter("duration", duration);
361+
action.addParameter("duration", duration.toMillis());
303362
parameterBuilder.add(action);
304363
return this;
305364
}

src/main/java/io/appium/java_client/android/HasSettings.java

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.openqa.selenium.remote.Response;
2828

29+
import java.time.Duration;
2930
import java.util.Map;
3031

3132
interface HasSettings extends ExecutesMethod {
@@ -53,7 +54,7 @@ default Map<String, Object> getSettings() {
5354
Map.Entry<String, Map<String, ?>> keyValuePair = getSettingsCommand();
5455
Response response = execute(keyValuePair.getKey(), keyValuePair.getValue());
5556

56-
return ImmutableMap.<String, Object>builder()
57+
return ImmutableMap.<String, Object>builder()
5758
.putAll(Map.class.cast(response.getValue())).build();
5859
}
5960

@@ -72,46 +73,106 @@ default void ignoreUnimportantViews(Boolean compress) {
7273

7374
/**
7475
* invoke {@code setWaitForIdleTimeout} in {@code com.android.uiautomator.core.Configurator}
76+
* This method is deprecated. Please use {@link #configuratorSetWaitForIdleTimeout(Duration)} instead.
7577
*
7678
* @param timeout in milliseconds. A negative value would reset to its default value
7779
*/
80+
@Deprecated
7881
default void configuratorSetWaitForIdleTimeout(int timeout) {
79-
setSetting(Setting.WAIT_FOR_IDLE_TIMEOUT, timeout);
82+
configuratorSetWaitForIdleTimeout(Duration.ofMillis(timeout));
83+
}
84+
85+
/**
86+
* invoke {@code setWaitForIdleTimeout} in {@code com.android.uiautomator.core.Configurator}
87+
*
88+
* @param timeout A negative value would reset to its default value. Minimum time unit
89+
* resolution is one millisecond
90+
*/
91+
default void configuratorSetWaitForIdleTimeout(Duration timeout) {
92+
setSetting(Setting.WAIT_FOR_IDLE_TIMEOUT, timeout.toMillis());
8093
}
8194

8295
/**
8396
* invoke {@code setWaitForSelectorTimeout} in {@code com.android.uiautomator.core.Configurator}
97+
* This method is deprecated. Please use {@link #configuratorSetWaitForSelectorTimeout(Duration)} instead.
8498
*
8599
* @param timeout in milliseconds. A negative value would reset to its default value
86100
*/
101+
@Deprecated
87102
default void configuratorSetWaitForSelectorTimeout(int timeout) {
88-
setSetting(Setting.WAIT_FOR_SELECTOR_TIMEOUT, timeout);
103+
configuratorSetWaitForSelectorTimeout(Duration.ofMillis(timeout));
104+
}
105+
106+
/**
107+
* invoke {@code setWaitForSelectorTimeout} in {@code com.android.uiautomator.core.Configurator}
108+
*
109+
* @param timeout A negative value would reset to its default value. Minimum time unit
110+
* resolution is one millisecond
111+
*/
112+
default void configuratorSetWaitForSelectorTimeout(Duration timeout) {
113+
setSetting(Setting.WAIT_FOR_SELECTOR_TIMEOUT, timeout.toMillis());
89114
}
90115

91116
/**
92117
* invoke {@code setScrollAcknowledgmentTimeout} in {@code com.android.uiautomator.core.Configurator}
118+
* This method is deprecated. Please use {@link #configuratorSetScrollAcknowledgmentTimeout(Duration)} instead.
93119
*
94120
* @param timeout in milliseconds. A negative value would reset to its default value
95121
*/
122+
@Deprecated
96123
default void configuratorSetScrollAcknowledgmentTimeout(int timeout) {
97-
setSetting(Setting.WAIT_SCROLL_ACKNOWLEDGMENT_TIMEOUT, timeout);
124+
configuratorSetScrollAcknowledgmentTimeout(Duration.ofMillis(timeout));
125+
}
126+
127+
/**
128+
* invoke {@code setScrollAcknowledgmentTimeout} in {@code com.android.uiautomator.core.Configurator}
129+
*
130+
* @param timeout A negative value would reset to its default value. Minimum time unit
131+
* resolution is one millisecond
132+
*/
133+
default void configuratorSetScrollAcknowledgmentTimeout(Duration timeout) {
134+
setSetting(Setting.WAIT_SCROLL_ACKNOWLEDGMENT_TIMEOUT, timeout.toMillis());
98135
}
99136

100137
/**
101138
* invoke {@code configuratorSetKeyInjectionDelay} in {@code com.android.uiautomator.core.Configurator}
139+
* This method is deprecated. Please use {@link #configuratorSetKeyInjectionDelay(Duration)} instead.
102140
*
103141
* @param delay in milliseconds. A negative value would reset to its default value
104142
*/
143+
@Deprecated
105144
default void configuratorSetKeyInjectionDelay(int delay) {
106-
setSetting(Setting.KEY_INJECTION_DELAY, delay);
145+
configuratorSetKeyInjectionDelay(Duration.ofMillis(delay));
146+
}
147+
148+
/**
149+
* invoke {@code configuratorSetKeyInjectionDelay} in {@code com.android.uiautomator.core.Configurator}
150+
*
151+
* @param delay A negative value would reset to its default value. Minimum time unit
152+
* resolution is one millisecond
153+
*/
154+
default void configuratorSetKeyInjectionDelay(Duration delay) {
155+
setSetting(Setting.KEY_INJECTION_DELAY, delay.toMillis());
107156
}
108157

109158
/**
110159
* invoke {@code setActionAcknowledgmentTimeout} in {@code com.android.uiautomator.core.Configurator}
160+
* This method is deprecated. Please use {@link #configuratorSetActionAcknowledgmentTimeout(Duration)} instead.
111161
*
112162
* @param timeout in milliseconds. A negative value would reset to its default value
113163
*/
164+
@Deprecated
114165
default void configuratorSetActionAcknowledgmentTimeout(int timeout) {
115-
setSetting(Setting.WAIT_ACTION_ACKNOWLEDGMENT_TIMEOUT, timeout);
166+
configuratorSetActionAcknowledgmentTimeout(Duration.ofMillis(timeout));
167+
}
168+
169+
/**
170+
* invoke {@code setActionAcknowledgmentTimeout} in {@code com.android.uiautomator.core.Configurator}
171+
*
172+
* @param timeout A negative value would reset to its default value. Minimum time unit
173+
* resolution is one millisecond
174+
*/
175+
default void configuratorSetActionAcknowledgmentTimeout(Duration timeout) {
176+
setSetting(Setting.WAIT_ACTION_ACKNOWLEDGMENT_TIMEOUT, timeout.toMillis());
116177
}
117178
}

src/main/java/io/appium/java_client/ios/LocksIOSDevice.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,28 @@
2121
import io.appium.java_client.CommandExecutionHelper;
2222
import io.appium.java_client.ExecutesMethod;
2323

24+
import java.time.Duration;
25+
2426
public interface LocksIOSDevice extends ExecutesMethod {
2527
/**
2628
* Lock the device (bring it to the lock screen) for a given number of
2729
* seconds.
30+
* Thsi method is deprecated. Please use {@link #lockDevice(Duration)} instead.
2831
*
2932
* @param seconds number of seconds to lock the screen for
3033
*/
34+
@Deprecated
3135
default void lockDevice(int seconds) {
32-
CommandExecutionHelper.execute(this, lockDeviceCommand(seconds));
36+
lockDevice(Duration.ofSeconds(seconds));
37+
}
38+
39+
/**
40+
* Lock the device (bring it to the lock screen) for a given number of
41+
* seconds.
42+
*
43+
* @param duration for how long to lock the screen. Minimum time resolution is one second
44+
*/
45+
default void lockDevice(Duration duration) {
46+
CommandExecutionHelper.execute(this, lockDeviceCommand(duration));
3347
}
3448
}

0 commit comments

Comments
 (0)