How to Perform Swipe Touch Using Appium


Swipe Touch
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.appium.java_client.MobileDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.touch.offset.ElementOption;

public class Example18SwipeTouchAction {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

        DesiredCapabilities dc = new DesiredCapabilities();

        dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");

        dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android"); // Android Emulator

        dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");

        dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.1");

        dc.setCapability(MobileCapabilityType.APP, "C:\\apkfiles\\SwipeListView.apk");

        URL url = new URL("http://127.0.0.1:4723/wd/hub");

        AndroidDriver driver = new AndroidDriver(url, dc);

        // click on ok button from the popup
        WebDriverWait wait = new WebDriverWait(driver, 300);
        wait.until(ExpectedConditions.elementToBeClickable(By.id("android:id/button1"))).click();

        // Get the size of screen.
        Dimension size = driver.manage().window().getSize();
        System.out.println(size);

        // Find swipe x points from screen's with and height.
        // Find x1 point which is at right side of screen.
        int x1 = (int) (size.width * 0.20);
        // Find x2 point which is at left side of screen.
        int x2 = (int) (size.width * 0.80);

        // Create object of TouchAction class.
        TouchAction action = new TouchAction(driver);

        // Find element to swipe from right to left.
        WebElement ele1 = (WebElement) driver.findElementsById("com.fortysevendeg.android.swipelistview:id/front")
                .get(3);
        // Create swipe action chain and perform horizontal(right to left) swipe.
        // Here swipe to point x1 Is at left side of screen. So It will swipe element
        // from right to left.
        action.longPress(ElementOption.element(ele1)).moveTo(ElementOption.element(ele1, x1, 580)).release().perform();

        Thread.sleep(5000);

        // Find element to swipe from left to right.
        WebElement ele2 = (WebElement) driver.findElementsById("com.fortysevendeg.android.swipelistview:id/back")
                .get(4);
        // Create swipe action chain and perform horizontal(left to right) swipe.
        // Here swipe to point x2 Is at right side of screen. So It will swipe element
        // from left to right.
        action.longPress(ElementOption.element(ele2)).moveTo(ElementOption.element(ele2, x2, 580)).release().perform();

        Thread.sleep(9500);

        driver.quit();
    }

}

Followers