Wednesday, August 28, 2019

Long Press action in Appium | Mobile Automation




In this Post let us learn how to do long press on android mobile using Appium.

Long press action is a mobile gesture that we use most of the time in mobile automation. for example when we need to type "+" in dailer , we press and hold "0", then it will change to "+", these kind of actions we can achieve using Long Press

Syntax:
TouchActions actions=new TouchAction(driver);
 actions.longPress(new LongPressOptions().withElement(ElementOption.element(Element)).withDuration(Duration.ofMillis(duration))).release().perform();




Let’s try and create a long press example using the TouchAction class. Here, i will take an example of a dialer pad on an Android real device. In this section, we will long press the number 0 and it will be converted into +. 





In below program, i have used long press action to achieve the above example :



package practise;


import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.LongPressOptions;
import io.appium.java_client.touch.offset.ElementOption;

public class LongPressExample {

AppiumDriver<MobileElement> driver;
 
 @BeforeClass
 public void setUp() throws MalformedURLException
 {
 DesiredCapabilities cap=new DesiredCapabilities();
     cap.setCapability("deviceName", "emulator-5554");
     cap.setCapability("udid", "emulator-5554");
     cap.setCapability("appActivity", "com.android.dialer");
     cap.setCapability("appPackage", "com.android.dialer.DialtactsActivity");
     cap.setCapability("platformName", "Android");
     cap.setCapability("platformVersion", "9.0");
     driver=new AppiumDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), cap);
     driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
 }
 
 @Test
 public void testExample(){
 
 MobileElement dialPad=driver.findElementByAccessibilityId("dial pad");
 dialPad.click();
 
 MobileElement dailPad=driver.findElement(By.name("0"));
 
  new TouchAction(driver)
 .longPress(new LongPressOptions()
 .withElement(ElementOption.element(dailPad))
 .withDuration(Duration.ofMillis(5)))
 .release()
 .perform();
  
  
 }

  @AfterClass
 public void tearDown(){
 driver.quit();
 }
}








No comments:

Post a Comment

If any suggestions or issue, please provide