Long Press Gesture – Appium


You already know, how to launch a web browser using Appium and launching a Native App using Appium. Now lets talk about user interactions. Just like in Selenium – Actions class, here we have TouchAction class to perform Mobile gestures like long press, drag and drop, swipe, and zoom etc. In Android by pressing an element for much long we will get other information about a feature. Let’s say, to uninstall an app we generally longpress on the app icon till we see a delete icon on the screen. So these kinda operations can be handled by ‘TouchAction‘ class

Let’s take a look at the syntax. Here i’m not using any app for demonstration. You may use any app. You need to change package and activity name, locators of the desired element.

package com.training;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Longpress {
AndroidDriver driver;
@BeforeTest
public void Setup() throws MalformedURLException{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, “4.4.2”);
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, “Android”);
caps.setCapability(MobileCapabilityType.DEVICE_NAME, “YourDevice”);
caps.setCapability(MobileCapabilityType.APP_PACKAGE,
“YourAPPPackage”);
caps.setCapability(MobileCapabilityType.APP_ACTIVITY,”App Activity”);
driver = new AndroidDriver (new URL(“http://127.0.0.1:4723/wd/hub”),caps);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void sample(){
WebElement someElement=driver.findElementByAccessibilityId(“Some ID”);
someElement.click();
TouchAction myaction=new TouchAction(driver);
//longpress webelement
myaction.longPress(driver.findElement(By.id(“some ID”))).perform();
}
@AfterClass
public void tearDown(){
driver.closeApp();
}

}

Here we are using TouchAction class, which has inbuilt method ‘longPress’. You can also specify certain duration to hold the element. For that you need to set syntax as ‘myaction.longPress(someElement, 15)’. The above syntax is used to long press an element for 15 seconds. You may dig into it for more inbuilt methods related to long press. If you’re not familiar with Desired capabilities then click here.

Advertisements

One thought on “Long Press Gesture – Appium

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s