Android gesture: There is a couple of basic gestures, like swipe, fling or pinch, which are commonly used in Android applications
public void swipeUp() {
Dimension size = driver.manage().window().getSize();
int startX = size.getWidth() / 2;
int startY = (int) (size.getHeight() * 0.8);
int endY = (int) (size.getHeight() * 0.2);
performSwipe(startX, startY, startX, endY);
}
public void swipeDown() {
Dimension size = driver.manage().window().getSize();
int startX = size.getWidth() / 2;
int startY = (int) (size.getHeight() * 0.2);
int endY = (int) (size.getHeight() * 0.8);
performSwipe(startX, startY, startX, endY);
}
public void swipeLeft() {
Dimension size = driver.manage().window().getSize();
int startX = (int) (size.getWidth() * 0.8);
int endX = (int) (size.getWidth() * 0.2);
int y = size.getHeight() / 2;
performSwipe(startX, y, endX, y);
}
public void swipeRight() {
Dimension size = driver.manage().window().getSize();
int startX = (int) (size.getWidth() * 0.2);
int endX = (int) (size.getWidth() * 0.8);
int y = size.getHeight() / 2;
performSwipe(startX, y, endX, y);
}
public void longPress(WebElement element) {
Point center = getCenter(element);
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
Sequence longPressSeq = new Sequence(finger, 0);
longPressSeq.addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), center.getX(), center.getY()));
longPressSeq.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
longPressSeq.addAction(new Pause(finger, Duration.ofSeconds(2)));
longPressSeq.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
driver.perform(Collections.singletonList(longPressSeq));
}
public void doubleTap(WebElement element) {
Point center = getCenter(element);
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
Sequence doubleTapSeq = new Sequence(finger, 0);
doubleTapSeq.addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), center.getX(), center.getY()));
doubleTapSeq.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
doubleTapSeq.addAction(new Pause(finger, Duration.ofMillis(50)));
doubleTapSeq.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
doubleTapSeq.addAction(new Pause(finger, Duration.ofMillis(100)));
doubleTapSeq.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
doubleTapSeq.addAction(new Pause(finger, Duration.ofMillis(50)));
doubleTapSeq.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
driver.perform(Collections.singletonList(doubleTapSeq));
}
private void performSwipe(int startX, int startY, int endX, int endY) {
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
Sequence swipe = new Sequence(finger, 0);
swipe.addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), startX, startY));
swipe.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
swipe.addAction(finger.createPointerMove(Duration.ofMillis(600), PointerInput.Origin.viewport(), endX, endY));
swipe.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
driver.perform(Collections.singletonList(swipe));
}
private Point getCenter(WebElement element) {
Point loc = element.getLocation();
Dimension size = element.getSize();
return new Point(loc.getX() + size.getWidth() / 2, loc.getY() + size.getHeight() / 2);
}