Handling Important Android Mobile Gestures using Appium and Java

Amit Kumar Santra
2 min readMay 23, 2021

--

TouchAction : TouchAction objects contain a chain of events for all the appium client libraries. The available events from the touch action are: * press * release * moveTo * tap * wait * longPress * cancel * perform* etc.

Reference

Tap on Element using Locator

import static io.appium.java_client.touch.TapOptions.tapOptions;
import static io.appium.java_client.touch.offset.ElementOption.element;
TouchAction touchAction = new TouchAction(driver);
AndroidElement expdList = driver.findElementByAndroidUIAutomator("text(\"Expandable Lists\")");
touchAction.tap(tapOptions().withElement(element(expdList))).perform();

Tap on particular coordinates of an Element

import static io.appium.java_client.touch.TapOptions.tapOptions;
import static io.appium.java_client.touch.offset.ElementOption.element;
TouchAction touchAction = new TouchAction(driver);
AndroidElement clock = driver.findElementByClassName("android.widget.RadialTimePickerView$RadialPickerTouchHelper");
touchAction.tap(tapOptions().withElement(element(clock)).withPosition(new PointOption<>().withCoordinates(922,1266))).perform(); // x, y coordinates

Double Tap

import static io.appium.java_client.touch.TapOptions.tapOptions;
import static io.appium.java_client.touch.offset.ElementOption.element;
AndroidElement nextCount= driver.findElementByAndroidUIAutomator("text(\"NEXT\")");
TouchAction touchAction = new TouchAction(driver);
touchAction.tap(tapOptions().withElement(element(nextCount)).withTapsCount(2)).perform();

Long press on a Element using Locator

import static java.time.Duration.ofSeconds;
import static io.appium.java_client.touch.LongPressOptions.longPressOptions;
TouchAction touchAction = new TouchAction(driver);
AndroidElement peopleNames = driver.findElementByAndroidUIAutomator("text(\"People Names\")");
touchAction.longPress(longPressOptions().withElement(element(peopleNames)).withDuration(ofSeconds(2))).release().perform(); // 2 second

Swipe Gesture

Hold the object and long press for 1–2sec then release the object to another element

import static java.time.Duration.ofSeconds;
import static io.appium.java_client.touch.LongPressOptions.longPressOptions;
TouchAction touchAction = new TouchAction(driver);
AndroidElement clock12 = driver.findElementByXPath("//*[@content-desc ='12']");
AndroidElement clock6 = driver.findElementByXPath("//*[@content-desc ='6']");
touchAction.longPress(longPressOptions().withElement(element(clock12)).withDuration(ofSeconds(2))).moveTo(element(clock6)).release().perform();

Scrolling down until a particular text is visible

String scrollElement = "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"MyText\").instance(0))"; 
// My Text: scroll till Mytext is visible
driver.findElementByAndroidUIAutomator(scrollElement);

Drag and Drop

Long press Source Object then Move to Destination Object then release the object.

import static io.appium.java_client.touch.offset.ElementOption.element;
import static io.appium.java_client.touch.LongPressOptions.longPressOptions;
TouchAction touchAction = new TouchAction(driver);
AndroidElement sourceEle = driver.findElementById("drag_dot_1");
AndroidElement destinationEle = driver.findElementById("drag_dot_2");
touchAction.longPress(longPressOptions().withElement(element(sourceEle))).moveTo(element(destinationEle)).release().perform();

--

--

Amit Kumar Santra
Amit Kumar Santra

Written by Amit Kumar Santra

SDET 2 @Whatfix | DAP | Test Automation | Front-End Automation | C# | Java | Selenium | Appium | WinAppDriver | SQL | DS & Algo | Azure DevOps CI/CD