Automate Android Gestures using Appium.


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.

Leave A Comment

Your email address will not be published. Required fields are marked *