Touch and swipe in Android using Appium

Reading Time: 2 minutes

In previous blogs we saw how we can use the Appium to launch any type of app without any APK. In this blog we will see how we can scroll through android elements using Appium.

It is very common where we need to scroll through a list of elements in a menu or on any application. Using Appium we can only select the elements which are visible on the screen. So, in order to access the elements which are in not visible on the screen we need to scroll to them.

For testing we will be using a test application with a simple interface.

We need to click on the last element as the which is Views. As the element is visible on the screen we do not require any scrolling to get their. For this we will be using the AccessibilityId and performing a click action in it.

Clicking on simple elements

driver.findElementByAccessibilityId("Views").click();

We will be directed to the next page where for accessing the last element we will need to perform the scroll operation.

To scroll to the list item Spinner we need to scroll to it.

Scrolling through elements

MobileElement listitem = (MobileElement) driver.findElement(
                MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(" +
                        "new UiSelector().description(\"Spinner\"));"));

MobileElement : We use it for OS-specific features such as findByUIAutomator for android. We are storing the elements in the listitem so that we can perform actions of it.

Mobileby.AndroidUIAutomator : We use for identifying the mobile elements in android devices.

UiScrollable : We use it to privately when performing swipe searches to decide if an element has become visible or not.

UiSelector().scrollIntoView : It performs a scroll forward action to move through the scrollable layout element until a visible item that matches the selector is found.

UiSelector().description : We use it to search through the elements so that we can know when to stop scrolling.

System.out.println(listitem.getLocation());
        listitem.click();

listitem.getLocation() : We use it to print the location of the element for which we are looking for.

listitem.click() : To click on the element which has the text defined in the Description.

import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

public class swipe_action {
    AndroidDriver driver;
    @Test
    public void testAppium() throws MalformedURLException, InterruptedException {

        DesiredCapabilities capability = new DesiredCapabilities();
        capability.setCapability("deviceName", "Pixel_API_28");
        capability.setCapability("platformVersion", "9");
        capability.setCapability("platformName", "Android");

        File file = new File("/home/ankur/Desktop/KnolxDemo/apk/ApiDemos-debug (1).apk");
        capability.setCapability("app", file.getAbsolutePath());

        driver = new AndroidDriver(new URL("http://127.0.1.1:4723/wd/hub"), capability);
        driver.findElementByAccessibilityId("Views").click();

        MobileElement listitem = (MobileElement) driver.findElement(
                MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(" +
                        "new UiSelector().description(\"Spinner\"));"));

        System.out.println(listitem.getLocation());
        listitem.click();
        Thread.sleep(10000);

    }
    @AfterTest
    public void afterTest() {
        driver.quit();
    }
}

So, this is a small introduction of how to perform swipe action on android devices. In the next blogs we will see how we can perform different type of actions on mobile devices.

Written by 

Ankur is a QA Consultant having experience of more than 1.5 years. He is familiar with the core concepts of manual and automation, postman and Newman are his expertise. He is always eager to learn new and advance concepts in order to expand his horizon and apply them in project development with his existing knowledge. His hobbies include watching web series and getting to know about all the latest gadgets that come in the market.