Skip to main content

How to perform Vertical and Horizontal swipe in Appium?


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);

Comments

  1. I'm looking for this solution very ling time, and finally found it.... keep sharing, Thanks :)

    ReplyDelete

Post a Comment

Popular posts from this blog

How to click/tap on the element by using X and Y coordinate in Appium?

How to click/tap on the element by using X and Y coordinate in Appium? [ Up to date:  java-client 6.x.x ] If don't have unique ID or any other locator of any element, then we can click on the element by using x and y coordinate. For that we have to fine X and Y coordinate first. How to find X and Y coordinate from android phone? Go to 'Settings' Go to 'Developer options' (Additional / Advanced settings > Developer options) Enable 'Show taps' and 'Pointer location' (Below the INPUT title) For example, we want to automate Calculator app Open the Calculator app and tap on the element and collect the X and Y coordinate like below image Use above X and Y coordinate int x = 282 ; int y = 808 ; new TouchAction((AndroidDriver) driver ).tap( point (x, y)).perform();

How to find app package and app activity from apk file

There are three ways to find app package and activity form apk, here I'm trying to show the ways Way 1: B y using Appium server Install the old version of Appium on your PC  (Appium 1.2.2) Open the  Appium server > Click on the  Android icon > Check the  Application Path check box> Choose > Select apk file Package and Launch Activity will be populated automatically Use Package as appPackage and Launch Activity as appActivity Way 2: By using APK Info application Install test application(which you want to test) on your device Install 'APK Info' application on your device from Play Store (Play Store link) Open 'APK Info' application > Long press on desired application (in my case 'Calculator') Detailed Information   Collect the appPackage and appActivity from below the Activates title Note: For some application appPackage and appActivity not same for that case you should use uiautomatorv

How to handle native Android keyboard by using Appium?

How to handle native Android keyboard by using Appium?[ Up to date: java-client 6.x.x ] We are going to tap on the enter button For WebDriver ((AndroidDriver) driver ).pressKey( new KeyEvent(AndroidKey. ENTER )); For AndroidDriver driver .pressKey( new KeyEvent(AndroidKey. ENTER ));   For more KeyEvert please follow the link Android KeyEvent