How to swipe vertically in Android app in Appium

Below example illustrates how to swipe vertically in Android app.

package nativeapptests;

import io.appium.java_client.android.AndroidDriver;
import org.junit.Test;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.URL;
import java.util.concurrent.TimeUnit;

public class SwipingTestVerticallyInWhatsapp
{
    private AndroidDriver driver;

    @Test
    public void loadingSinglePageTest() throws Exception{

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("platformVersion", "4.4.4");
        capabilities.setCapability("deviceName", "sagarcell");
        capabilities.setCapability("browser_Name", "Android");
        // capabilities.setCapability("app", "chrome");
        capabilities.setCapability("appPackage", "com.whatsapp");
        capabilities.setCapability("appActivity","com.whatsapp.Main");
        //capabilities.setCapability("browserName", "Chrome");

        try {
            driver = new AndroidDriver(new URL("https://127.0.0.1:4723/wd/hub"), capabilities) {
            };

           driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
           Thread.sleep(5000);


            Dimension size = driver.manage().window().getSize();
            System.out.println("App window dimensions -> " + size);
            driver.swipe(size.width/2,  size.height-1, size.width/2,200  , 2000);

            //swiping from top to bottom
            Thread.sleep(2000);

            //swiping from bottom to top
             driver.swipe(size.width/2,  200, size.width/2,  size.height-1, 2000);

            Thread.sleep(2000);


        }catch(Exception ex) {
            System.out.println(ex.toString());


        }finally {
             //quit the browser
            driver.quit();
        }
        
    }
}

You may also like...