From Appium 2.5 onwards, the legacy touch/tap actions have been deprecated. If you are upgrading your Appium then you need to migrate these methods to new W3C standard gestures. In this blog, we would discuss how to do that for below three methods.
Tap By coordinates:
Legacy method:
public void tapByCoordinates(int x, int y) {
new TouchAction(driver).tap(point(x, y)).perform();
}
New W3C method:
public void tapByCoordinates(int x, int y) {
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
Sequence tap = new Sequence(finger, 1);
tap.addAction(finger.createPointerMove(Duration.ofMillis(0), PointerInput.Origin.viewport(), x, y));
tap.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
tap.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
getSession().getAppiumDriver().perform(Arrays.asList(tap));
}
Vertical Swipe:
Legacy method:
public void verticalSwipeAction(int width, int start, int end) {
new TouchAction(driver)
.press(point(width, start))
.waitAction(waitOptions(ofMillis(1000)))
.moveTo(point(width, end))
.release()
.perform();
}
New W3C method:
public void verticalSwipeAction(int width, int start, int end, int waitMillis) {
PointerInput finger1 = new PointerInput(PointerInput.Kind.TOUCH, "finger1");
Sequence sequence = new Sequence(finger1, 1)
.addAction(finger1.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), width, start))
.addAction(finger1.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
.addAction(new Pause(finger1, Duration.ofMillis(waitMillis)))
.addAction(finger1.createPointerMove(Duration.ofMillis(waitMillis), PointerInput.Origin.viewport(), width, end))
.addAction(new Pause(finger1, Duration.ofMillis(waitMillis)))
.addAction(finger1.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
getSession().getAppiumDriver().perform(Collections.singletonList(sequence));
}
Swipe across screen:
Legacy method:
protected void swipeAction(int startx, int starty, int endx, int endy, int duration) {
new TouchAction(driver).press(PointOption.point(startx, starty))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(duration)))
.moveTo(PointOption.point(endx, endy))
.release()
.perform();
}
New W3C method:
protected void swipeTouchAction(int startx, int starty, int endx, int endy, int duration) {
PointerInput finger1 = new PointerInput(PointerInput.Kind.TOUCH, "finger1");
Sequence sequence = new Sequence(finger1, 1)
.addAction(finger1.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), startx, starty))
.addAction(finger1.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
.addAction(new Pause(finger1, Duration.ofMillis(duration)))
.addAction(finger1.createPointerMove(Duration.ofMillis(100), PointerInput.Origin.viewport(), endx, endy))
.addAction(finger1.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
getSession().getAppiumDriver().perform(Collections.singletonList(sequence));
}
- TS360 Engineering Team