Home » appium-tutorial » TouchAction : Tap And longPress in Appium

Newly Updated Posts

TouchAction : Tap And longPress in Appium

Previous Topic, In this tutorial we will learn touch Action commands like “TouchAction : Tap And longpress in Appium

How to perform TouchAction : Tap And longpress in Appium?

In Appium client library, Touch Action is a class which extends Object class and implements PerformsAction Interface.

TouchAction : Tap And longPress in Appium

Touch action class has multiple events to perfrom actions on mobile devices like tap, release, press, longpress,cancel, moveto,wait etc.

In this tutorial we will be performing Tap, Press and long press on mobile application through appium client library, let start now.

1 ) Tap: In mobile application Tap method works same as click method in UI. We provide the co ordinates of x and y as a parameter to point on the element where the tap for an element is to be performed.

We call PointOption.point() method and pass in tap method to perform tap operation. Find below the code.

//defining the locator 
AndroidElement e =driver.findElementByXPath(" ");

//Finding the x and y coordinates
int x=e.getLocation().x;
int y=e.getLocation().y;

//Creating the object of Touch Action
TouchAction  action =new TouchAction(adriver);

//Calling the tap method and passing the x and y co ordinates 
action.tap(PointOption.point(x, y)).perform();

So on the above code we have firstly created or defined the locator of the mobile element , after that we extracted the x and y co ordinates of the element.

Finally we have created the object of Touch Action and called the tap method providing the x and y co-ordinates using PointOption.point(x,y) method followed by performed method.

How to perfrom Press and LongPress in Appium?

2 ) Press: Press action in mobile application is, pressing of element to a certain time period or wait, so that the element could perform an event. For ex sometimes in an application we come up with scenario where pressing on any element displays mutiple listing or changes the UI of an application or an event gets triggered.

Syntex of Press without wait is below:

//defining the locator 
AndroidElement e =driver.findElementByXPath(" ");

//Finding the x and y coordinates
int x=e.getLocation().x;
int y=e.getLocation().y;

//Creating the object of Touch Action
TouchAction  action =new TouchAction(adriver);

//Calling the press method and passing the x and y co ordinates 
action.press(PointOption.point(x, y)).perform();

Syntex of Press with wait command and release the press is below:

action.press(PointOption.point(x, y)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2))).release().perform();

In the above code press with wait command, we firstly press an element in mobile application and then provide a wait of 2 seconds using WaitOptions.waitOptions() method and after 2 second release the press from the element.

3 ) LongPress: Longpress method is just simply pressing the UI element for a longer time span so that the specific element can trigger an even and perform action.

The longPress works same as press method with wait for certain seconds, the difference is that the longpress is a customised method where the wait is not implicitly defined.

Syntex for the LongPress method is below:

//defining the locator 
AndroidElement e =driver.findElementByXPath(" ");

//Finding the x and y coordinates
int x=e.getLocation().x;
int y=e.getLocation().y;

//Creating the object of Touch Action
TouchAction  action =new TouchAction(adriver);

//Calling the longPress method and passing the x and y co ordinates
action.longPress(PointOption.point(x, y)).perform();

So using the above code, we are good to perform longpress on mobile application to any element with the help of x and y co-ordinates. The code in the eclipse will look like as below screenshot.

TouchAction: Tap And longpress in Appium

Conclusion:

So we have come with the final conclusion that, we can perform Touch Action on the mobile android device with events like tap, press, long press with the above mentioned code and syntex using Touch Action class.


Leave a comment

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