How to swipe horizontally in Android app using Appium

Below example illustrates how to swipe the app screen in Appium. Below code can be used to swipe horizontally on Whatsapp Android app is given below.

package nativeapptests;

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

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

public class SwipingTestInWhatsapp
{
    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);

            //swiping from left to right
            driver.swipe(1,  size.height/2, size.width-1,  size.height/2, 2000);
            Thread.sleep(2000);

            //swiping from right to left
            driver.swipe(size.width-1,  size.height/2, 1,  size.height/2, 2000);
            Thread.sleep(2000);

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


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

You may also like...