How to perform Vertical and Horizontal swipe in Appium?[Up to date: java-client 6.x.x]
In Android application Automation, sometimes we need to scroll up/down and swipe left/right, in this tutorial I'm trying to show how to perform them.
1. For vertical swiping use below method
public static void swipeVertical(AppiumDriver driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
Dimension size = driver.manage().window().getSize();
int anchor = (int) (size.width * anchorPercentage);
int startPoint = (int) (size.height * startPercentage);
int endPoint = (int) (size.height * finalPercentage);
new TouchAction(driver).press(PointOption.point(anchor, startPoint)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(duration))).moveTo(PointOption.point(anchor, endPoint)).release().perform();
}
and call the above method like below
Scroll up: swipeVertical((AppiumDriver)driver,0.9,0.1,0.5,3000);
Scroll down: swipeVertical((AppiumDriver)driver,0.1,0.9,0.5,3000);
2. For horizontal swiping use below method public static void swipeHorizontal(AppiumDriver driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception { Dimension size = driver.manage().window().getSize(); int anchor = (int) (size.height * anchorPercentage); int startPoint = (int) (size.width * startPercentage); int endPoint = (int) (size.width * finalPercentage); new TouchAction(driver).press(PointOption.point(startPoint, anchor)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(duration))).moveTo(PointOption.point(endPoint, anchor)).release().perform(); }
and call above method like below
Right to left: swipeHorizontal((AppiumDriver) driver,0.9,0.01,0.5,3000);
Left to right: swipeHorizontal((AppiumDriver) driver,0.01,0.9,0.5,3000);
I'm looking for this solution very ling time, and finally found it.... keep sharing, Thanks :)
ReplyDelete