Skip to content

Commit 7042073

Browse files
SrinivasanTargetTikhomirovSergey
authored andcommitted
The addition to #513 (#514)
* Splitting of TouchActions * Codacy Fixes * #456 FIX #454 FIX: API redesign. - new interfaces were added - deprecated API * #456 FIX #454 FIX: MultiTouchAction refactoring - new methods were added to MultiTouchAction - AppiumDriver methods which perform multiple touch actions were marked as Deprecated - Constructors of TouchAction and MultiTouchAction were changed. Now it accepts any instance that can perform touch action and multiple touch actions. * #456 FIX #454 FIX: New CreatesSwipeAction API - the new interface CreatesSwipeAction was added. - the reversion of last changes of TouchableElement. - the `swipe` is deprecated method. * #456 FIX #454 FIX: Forgot to commit this change * #456 FIX #454 FIX: CreatesSwipeAction API was implemented - CreatesSwipeAction API was implemented - SwipeElementDirection was redesigned - constructors of TouchAction and MultiTouchAction were improved. * #456 FIX #454 FIX: AndroidTouchActions were covered with tests. - also code issues were got fixed * #456 FIX #454 FIX: Refactoring of MobileElement * #456 FIX #454 FIX: IOSGesturesTest was redesigned. - IOSSwipeGestureTest was added * #456 FIX #454 FIX: Checkstyle issues were got fixed * #456 FIX #454 FIX: The additional test on Android. - the swiping combined with the tapping. * Issues that found by codecy were got fixed * The addition #513 Fixed Codacy errors Fixed Codacy errors * Fixed tests Fixed tests Fixed tests Fixed tests
1 parent 28d10c8 commit 7042073

35 files changed

+972
-373
lines changed

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

Lines changed: 37 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import org.openqa.selenium.By;
2929
import org.openqa.selenium.Capabilities;
3030
import org.openqa.selenium.DeviceRotation;
31-
import org.openqa.selenium.Dimension;
32-
import org.openqa.selenium.Point;
3331
import org.openqa.selenium.ScreenOrientation;
3432
import org.openqa.selenium.WebDriver;
3533
import org.openqa.selenium.WebDriverException;
@@ -67,7 +65,7 @@
6765
* for each target mobile OS (still Android and iOS)
6866
*/
6967
@SuppressWarnings("unchecked")
70-
public abstract class AppiumDriver<T extends WebElement>
68+
public class AppiumDriver<T extends WebElement>
7169
extends DefaultGenericMobileDriver<T> {
7270

7371
private static final ErrorHandler errorHandler = new ErrorHandler(new ErrorCodesMobile(), true);
@@ -214,9 +212,11 @@ public List<T> findElementsByXPath(String using) {
214212
}
215213

216214
/**
217-
* @see TouchShortcuts#tap(int, WebElement, int).
215+
* This method is deprecated and it is going to be removed soon.
216+
* Please use {@link MultiTouchAction#tap(int, WebElement, int)}.
218217
*/
219-
@Override public void tap(int fingers, WebElement element, int duration) {
218+
@Deprecated
219+
public void tap(int fingers, WebElement element, int duration) {
220220
MultiTouchAction multiTouch = new MultiTouchAction(this);
221221

222222
for (int i = 0; i < fingers; i++) {
@@ -227,81 +227,49 @@ public List<T> findElementsByXPath(String using) {
227227
}
228228

229229
/**
230-
* @see TouchShortcuts#tap(int, int, int, int).
230+
* This method is deprecated and it is going to be removed soon.
231+
* Please use {@link MultiTouchAction#tap(int, int, int, int)}.
231232
*/
232-
@Override public void tap(int fingers, int x, int y, int duration) {
233+
@Deprecated
234+
public void tap(int fingers, int x, int y, int duration) {
233235
MultiTouchAction multiTouch = new MultiTouchAction(this);
234236

235237
for (int i = 0; i < fingers; i++) {
236238
multiTouch.add(createTap(x, y, duration));
237239
}
238-
239240
multiTouch.perform();
240241
}
241242

242-
protected void doSwipe(int startx, int starty, int endx, int endy, int duration) {
243-
TouchAction touchAction = new TouchAction(this);
244-
245-
// appium converts press-wait-moveto-release to a swipe action
246-
touchAction.press(startx, starty).waitAction(duration).moveTo(endx, endy).release();
247-
248-
touchAction.perform();
249-
}
250-
251243
/**
252-
* @see TouchShortcuts#swipe(int, int, int, int, int).
244+
* This method is deprecated.
245+
* It was moved to {@link CreatesSwipeAction#swipe(int, int, int, int, int)}.
253246
*/
254-
@Override public abstract void swipe(int startx, int starty, int endx, int endy, int duration);
247+
@Deprecated
248+
public void swipe(int startx, int starty, int endx, int endy, int duration) {
249+
//does nothing
250+
}
255251

256252
/**
257-
* Convenience method for pinching an element on the screen.
258-
* "pinching" refers to the action of two appendages pressing the
259-
* screen and sliding towards each other.
260-
* NOTE:
261-
* This convenience method places the initial touches around the element, if this would
262-
* happen to place one of them off the screen, appium with return an outOfBounds error.
263-
* In this case, revert to using the MultiTouchAction api instead of this method.
264-
*
265-
* @param el The element to pinch.
253+
* This method is deprecated and it is going to be removed soon.
254+
* Please use {@link MultiTouchAction#pinch(WebElement)}.
266255
*/
256+
@Deprecated
267257
public void pinch(WebElement el) {
268258
MultiTouchAction multiTouch = new MultiTouchAction(this);
269259

270-
Dimension dimensions = el.getSize();
271-
Point upperLeft = el.getLocation();
272-
Point center = new Point(upperLeft.getX() + dimensions.getWidth() / 2,
273-
upperLeft.getY() + dimensions.getHeight() / 2);
274-
int yOffset = center.getY() - upperLeft.getY();
275-
276-
TouchAction action0 =
277-
new TouchAction(this).press(el, center.getX(), center.getY() - yOffset).moveTo(el)
278-
.release();
279-
TouchAction action1 =
280-
new TouchAction(this).press(el, center.getX(), center.getY() + yOffset).moveTo(el)
281-
.release();
282-
283-
multiTouch.add(action0).add(action1);
284-
285-
multiTouch.perform();
260+
multiTouch.pinch(el).perform();
286261
}
287262

288263
/**
289-
* Convenience method for pinching an element on the screen.
290-
* "pinching" refers to the action of two appendages pressing the screen and
291-
* sliding towards each other.
292-
* NOTE:
293-
* This convenience method places the initial touches around the element at a distance,
294-
* if this would happen to place one of them off the screen, appium will return an
295-
* outOfBounds error. In this case, revert to using the MultiTouchAction api instead of this
296-
* method.
297-
*
298-
* @param x x coordinate to terminate the pinch on.
299-
* @param y y coordinate to terminate the pinch on.
264+
* This method is deprecated and it is going to be removed soon.
265+
* Please use {@link MultiTouchAction#pinch(int, int, int, int)} or
266+
* {@link MultiTouchAction#pinch(int, int, int)}
300267
*/
268+
@Deprecated
301269
public void pinch(int x, int y) {
302270
MultiTouchAction multiTouch = new MultiTouchAction(this);
303271

304-
int scrHeight = manage().window().getSize().getHeight();
272+
int scrHeight = this.manage().window().getSize().getHeight();
305273
int yOffset = 100;
306274

307275
if (y - 100 < 0) {
@@ -313,57 +281,30 @@ public void pinch(int x, int y) {
313281
TouchAction action0 = new TouchAction(this).press(x, y - yOffset).moveTo(x, y).release();
314282
TouchAction action1 = new TouchAction(this).press(x, y + yOffset).moveTo(x, y).release();
315283

316-
multiTouch.add(action0).add(action1);
317-
318-
multiTouch.perform();
284+
multiTouch.add(action0).add(action1).perform();
319285
}
320286

321287
/**
322-
* Convenience method for "zooming in" on an element on the screen.
323-
* "zooming in" refers to the action of two appendages pressing the screen and sliding
324-
* away from each other.
325-
* NOTE:
326-
* This convenience method slides touches away from the element, if this would happen
327-
* to place one of them off the screen, appium will return an outOfBounds error.
328-
* In this case, revert to using the MultiTouchAction api instead of this method.
329-
*
330-
* @param el The element to pinch.
288+
* This method is deprecated and it is going to be removed soon.
289+
* Please use {@link MultiTouchAction#zoom(WebElement)}.
331290
*/
291+
@Deprecated
332292
public void zoom(WebElement el) {
333293
MultiTouchAction multiTouch = new MultiTouchAction(this);
334294

335-
Dimension dimensions = el.getSize();
336-
Point upperLeft = el.getLocation();
337-
Point center = new Point(upperLeft.getX() + dimensions.getWidth() / 2,
338-
upperLeft.getY() + dimensions.getHeight() / 2);
339-
int yOffset = center.getY() - upperLeft.getY();
340-
341-
TouchAction action0 = new TouchAction(this).press(center.getX(), center.getY())
342-
.moveTo(el, center.getX(), center.getY() - yOffset).release();
343-
TouchAction action1 = new TouchAction(this).press(center.getX(), center.getY())
344-
.moveTo(el, center.getX(), center.getY() + yOffset).release();
345-
346-
multiTouch.add(action0).add(action1);
347-
348-
multiTouch.perform();
295+
multiTouch.zoom(el).perform();
349296
}
350297

351298
/**
352-
* Convenience method for "zooming in" on an element on the screen.
353-
* "zooming in" refers to the action of two appendages pressing the screen
354-
* and sliding away from each other.
355-
* NOTE:
356-
* This convenience method slides touches away from the element, if this would happen to
357-
* place one of them off the screen, appium will return an outOfBounds error. In this case,
358-
* revert to using the MultiTouchAction api instead of this method.
359-
*
360-
* @param x x coordinate to start zoom on.
361-
* @param y y coordinate to start zoom on.
299+
* This method is deprecated and it is going to be removed soon.
300+
* Please use {@link MultiTouchAction#zoom(int, int, int, int)} or
301+
* {@link MultiTouchAction#zoom(int, int, int)}.
362302
*/
303+
@Deprecated
363304
public void zoom(int x, int y) {
364305
MultiTouchAction multiTouch = new MultiTouchAction(this);
365306

366-
int scrHeight = manage().window().getSize().getHeight();
307+
int scrHeight = this.manage().window().getSize().getHeight();
367308
int yOffset = 100;
368309

369310
if (y - 100 < 0) {
@@ -375,9 +316,7 @@ public void zoom(int x, int y) {
375316
TouchAction action0 = new TouchAction(this).press(x, y).moveTo(0, -yOffset).release();
376317
TouchAction action1 = new TouchAction(this).press(x, y).moveTo(0, yOffset).release();
377318

378-
multiTouch.add(action0).add(action1);
379-
380-
multiTouch.perform();
319+
multiTouch.add(action0).add(action1).perform();
381320
}
382321

383322
@Override public WebDriver context(String name) {
@@ -447,11 +386,13 @@ public void zoom(int x, int y) {
447386
locationContext.setLocation(location);
448387
}
449388

389+
@Deprecated
450390
private TouchAction createTap(WebElement element, int duration) {
451391
TouchAction tap = new TouchAction(this);
452392
return tap.press(element).waitAction(duration).release();
453393
}
454394

395+
@Deprecated
455396
private TouchAction createTap(int x, int y, int duration) {
456397
TouchAction tap = new TouchAction(this);
457398
return tap.press(x, y).waitAction(duration).release();
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client;
18+
19+
import org.openqa.selenium.WebElement;
20+
21+
public interface CreatesSwipeAction {
22+
23+
/**
24+
* Creates combined touch action for the swiping by start/end coordinates.
25+
*
26+
* @param startX x-coordinate to swipe from
27+
* @param startY y-coordinate to swipe from
28+
* @param endX x-coordinate to swipe to
29+
* @param endY y-coordinate to swipe to
30+
* @param duration in milliseconds
31+
* @return an instance of combined {@link TouchAction}
32+
*/
33+
TouchAction swipe(int startX, int startY, int endX, int endY, int duration);
34+
35+
/**
36+
* Creates combined touch action for the swiping from given coordinates to the given element.
37+
*
38+
* @param startX x-coordinate to swipe from
39+
* @param startY y-coordinate to swipe from
40+
* @param element an element to swipe to
41+
* @param duration in milliseconds
42+
* @return an instance of combined {@link TouchAction}
43+
*/
44+
TouchAction swipe(int startX, int startY, WebElement element, int duration);
45+
46+
/**
47+
* Creates combined touch action for the swiping from one element to another.
48+
*
49+
* @param element1 an element to swipe to
50+
* @param element2 an element to swipe to
51+
* @param duration in milliseconds
52+
* @return an instance of combined {@link TouchAction}
53+
*/
54+
TouchAction swipe(WebElement element1, WebElement element2, int duration);
55+
56+
57+
/**
58+
* Creates combined touch action for the swiping inside an element.
59+
*
60+
* @param element an element where the swiping is performed
61+
* @param direction is the direction to perform the swiping inside the element
62+
* @param duration in milliseconds
63+
* @return an instance of combined {@link TouchAction}
64+
*/
65+
default TouchAction swipe(MobileElement element, SwipeElementDirection direction, int duration) {
66+
return direction.swipe(this, element, 0, 0, duration);
67+
}
68+
69+
/**
70+
* Creates combined touch action for the swiping inside an element using some offset from its
71+
* borders.
72+
*
73+
* @param element an element where the swiping is performed
74+
* @param direction is the direction to perform the swiping inside the element
75+
* @param offsetFromStartBorder is the offset from the border of the element where the
76+
* swiping should be started. If direction is UP then
77+
* this is offset from the bottom of the element.
78+
* If direction is DOWN then this is offset from the top of
79+
* the element. If direction is RIGHT then this is offset from
80+
* the left border of the element. If direction is LEFT then
81+
* this is offset from the right border of the element.
82+
* @param offsetFromEndBorder is the offset from the border of the element where
83+
* the swiping should be finished. If direction is UP then
84+
* this is offset from the top of the element.
85+
* If direction is DOWN then this is offset from the bottom
86+
* of the element. If direction is RIGHT then
87+
* this is offset from the right border of the element.
88+
* If direction is LEFT then this is offset from the
89+
* left border of the element.
90+
* @param duration in milliseconds
91+
* @return an instance of combined {@link TouchAction}
92+
* @throws IllegalCoordinatesException when resulted coordinates are out of the
93+
* element borders or disagree with the given direction.
94+
*/
95+
default TouchAction swipe(MobileElement element, SwipeElementDirection direction, int offsetFromStartBorder,
96+
int offsetFromEndBorder,
97+
int duration) throws IllegalCoordinatesException {
98+
return direction.swipe(this, element, offsetFromStartBorder, offsetFromEndBorder,
99+
duration);
100+
}
101+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121

2222
import org.openqa.selenium.remote.Response;
2323

24+
@Deprecated
25+
/**
26+
* This interface is deprecated and won't be supported anymore.
27+
* Please use {@link HasDeviceTime} and {@link HidesKeyboard} API instead.
28+
*/
2429
public interface DeviceActionShortcuts extends ExecutesMethod {
2530

2631
/**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client;
18+
19+
import static io.appium.java_client.MobileCommand.GET_DEVICE_TIME;
20+
21+
import org.openqa.selenium.remote.Response;
22+
23+
public interface HasDeviceTime extends ExecutesMethod {
24+
/*
25+
Gets device date and time for both iOS(Supports only real device) and Android devices
26+
*/
27+
default String getDeviceTime() {
28+
Response response = execute(GET_DEVICE_TIME);
29+
return response.getValue().toString();
30+
}
31+
}

0 commit comments

Comments
 (0)