Touch and Tap action on Mobile Automation with Appium

Khemlall Mangal
2 min readJun 6, 2022

Sample of Tap Option in Appium

When ever you use touch option you need to end it with perform. If you have a test case where you tap on a field and it will expand the list and on that list you want to click on that expanded list.

Difference between click and Tap. We can use a gesture called Tap . We tap with a finger and check if that single tap is working. With Appium we are tapping instead of moving mouse and click. If you want to use mobile gesture you need to use touchpoints. ‘

To point based on Coordinate, you use point Option and if you want to click on any element based upon it locator like XPATH then you can use TapOptions.

a) Instantiate the Touch action.

TouchAction t =new TouchAction(driver);

b) Find the expand List

WebElement expandList= driver.findElementByXPath("//android.widget.TextView[@text='Expandable Lists']");

c) Tap on the list to expand

t.tap(tapOptions().withElement(element(expandList))).perform();

d) find the text and click

driver.findElementByXPath("//android.widget.TextView[@text='1. Custom Adapter']").click();

e) Find the element, hold down for 2 second and then ensure item appears.

WebElement pn= driver.findElementByXPath("//android.widget.TextView[@text='People Names']");

t.longPress(longPressOptions().withElement(element(pn)).withDuration(ofSeconds(2))).release().perform();
//Thread.sleep(2000);
System.out.println(driver.findElementById("android:id/title").isDisplayed());

Entire code sample method. This is using ApiDemos.apk provided by appium.

import java.net.MalformedURLException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebElement;import io.appium.java_client.TouchAction;
import static io.appium.java_client.touch.TapOptions.tapOptions;
import static io.appium.java_client.touch.LongPressOptions.longPressOptions;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import static java.time.Duration.ofSeconds;
import static io.appium.java_client.touch.offset.ElementOption.element;
public class gestures extends base {public static void main(String[] args) throws MalformedURLException {
// TODO Auto-generated method stub
AndroidDriver<AndroidElement> driver=capabilities();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElementByXPath("//android.widget.TextView[@text='Views']").click();
//Tap
TouchAction t =new TouchAction(driver);
WebElement expandList= driver.findElementByXPath("//android.widget.TextView[@text='Expandable Lists']");
t.tap(tapOptions().withElement(element(expandList))).perform();
driver.findElementByXPath("//android.widget.TextView[@text='1. Custom Adapter']").click();
WebElement pn= driver.findElementByXPath("//android.widget.TextView[@text='People Names']");

t.longPress(longPressOptions().withElement(element(pn)).withDuration(ofSeconds(2))).release().perform();
//Thread.sleep(2000);
System.out.println(driver.findElementById("android:id/title").isDisplayed());
}}

--

--

Khemlall Mangal

I am a passionate coder, QA Engineer, and someone who enjoys the outdoors.