Tech Blog

Mobile Automation

Automate Android Gestures using Appium.

By Abhishek MK2 min read
← Back to blog

A gesture in Android is basically a hand/finger movement that results in some action on the device.

Examples: 

1. Tapping on the login button for Gmail or Facebook should provide account access.

2. Long pressing on the voice chat icon should initiate a voice message in WhatsApp.

We use Android gestures in Appium by using the TouchAction class.

TouchAction class in Appium:

Appium has designed a class called TouchAction. By using the methods present in TouchAction class, we can perform mobile gestures such as Tap, Long Press, Scroll, Swipe, Drag and Drop, etc.

Types of Gestures

  1. [Gesture: Tap]

On tapping an object or an element on the screen, an action similar to a finger tap will be performed..

Syntax:

TouchAction action = new TouchAction(driver);

WebElement webelement= driver.findElementByXPath(“xpath of an webelement“);

action.tap(tapOptions().withElement(element(webelement))).perform();

Note:

1. The tap method will expect tapOptions for locating an element and the action would be performed on the webelement. We need to add the below packages manually.

Packages: 

import static io.appium.java_client.touch.TapOptions.tapOptions;

import static io.appium.java_client.touch.offset.ElementOption.element;

  1. [Gesture: LongPress]

Long Press is an action performed on web elements by tapping and holding for specific time periods.

Syntax:

TouchAction action = new TouchAction(driver);

WebElement webelement= driver.findElementByXPath(“xpath of an webelement“);

action.longPress(longPressOptions().withElement(element(webelement)).withDuration(ofSeconds(2))).release().perform();

Note:

  1. The longPress method will expect longPressOptions for locating an element and the action would be performed on the webelement. We need to add the below packages manually.

Packages: 

  • import static io.appium.java_client.touch.LongPressOptions.longPressOptions;
  • import static io.appium.java_client.touch.offset.ElementOption.element;
  • import static java.time.Duration.ofSeconds;
  1. [Gesture: Swipe]

Swipe action will be performed using the long press method by long pressing on the first web element for a specified time and moving to the second web element.

Syntax: 

TouchAction action = new TouchAction(driver);
WebElement firstWebElement = driver.findElementByXPath("xpath of an first webelement");
WebElement secondWebElement= driver.findElementByXPath("xpath of an second webelement");
action.longPress(longPressOptions().withElement(element(firstWebElement)).withDuration(ofSeconds(2))).moveTo(element(secondWebElement)).release().perform();

Note: 

  • We can add long press packages manually to perform swipe actions. (Refer to the longPress gesture).
  1. [Gesture: Scroll]

We can use Android APIs to Scroll. In Android APIs, we have some methodology for scrolling. To execute the scroll gesture, we need to bring those APIs from Android to Appium. 

Syntax:

Method 1:

AndroidDriver<AndroidElement> driver = capabilities();
driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches(\""+ text +"\").instance(0))")).isDisplayed();

Method 2:

AndroidDriver<AndroidElement> driver = capabilities();
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\"text\"))");

Note: 

  1. The text refers to the element name (UI) where scroll action needs to be performed.

Keep reading

You may be interested in

Browse all articles →
Using SCEP Certificates with Okta and SureMDM for Zero Trust Security
August 5, 20257 min read

Using SCEP Certificates with Okta and SureMDM for Zero Trust Security

For businesses embracing zero-trust principles, secure certificate-based authentication isn’t just nice to have—it’s a necessity. With Okta as your Identity Provider (IdP) and SureMDM as your unified endpoint management solution, you can now seamlessly distribute and manage device certificates using Simple Certificate Enrollment Protocol (SCEP). This blog will guide you through: Why Use SCEP with […]

By Sunny KumarRead More →
How to Use mc CLI for Bucket Management and Credential Setup
June 12, 20252 min read

How to Use mc CLI for Bucket Management and Credential Setup

MinIO client The MinIO Client, commonly known as mc, is a command-line tool used to interact with MinIO, a high-performance object storage system. The mc client allows users to manage buckets and objects on the MinIO storage service.

By Malkeet SinghRead More →
How to Setup Passwordless SSH Login for ADE
March 18, 20253 min read

How to Setup Passwordless SSH Login for ADE

To set up a passwordless SSH login in Linux all you need to do is to generate a public authentication key and append it to the remote hosts ~/.ssh/authorized_keys file. The following steps will describe the process for configuring passwordless SSH login: 1. Check for existing SSH key pair Before generating a new SSH key […]

By Mohan Kumar KMRead More →