Appium : Perform LongPress Using TouchAction

Use of Long press in Appium :

In Android Apps ,sometimes we have to press and hold the elements to get the expected elements.So in this case we can use longPress() method of Touch Action class.
By using TouchAction Class, also we can do DragAnddrop,swipe ,multiTouchAction etc..

EX:
While composing message in mobile through message app if i want to include @ in message text , Then i have to poress and hold the letter 'S' then release .So that i will get '@'.
Same while dialing any number in dial pad if want to include '+' icon then i have to longpress '0'.
So we can automate this kind of scenarios in mobile apps only when we will use longPress() method of TouchAction class.

Sample Program of DialPad:


package AppiumAndroidTest;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class DialPadTest {
 public static Android driver;

 @BeforeTest
 public void setUp() throws Exception {
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName", "AnyAndroiddevice");
  capabilities.setCapability("browserName", "Android");
  capabilities.setCapability("platformVersion", "6.0");
  capabilities.setCapability("platformName", "Android");
  capabilities.setCapability("appPackage", "com.android.dialer");
  capabilities.setCapability("appActivity","com.android.dialer.DialtactsActivity");
  driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
 }

 @Test
 public void dialnumber() {
  driver.findElementById("com.android.dialer:id/dialpad_button").click();
  //object of TouchAction class.
  TouchAction Act = new TouchAction(driver);
  //perform long press action on button 0 of dialer pad.
  Act.longPress(driver.findElement(By.name("0"))).perform();
  String result = driver.findElementById("com.android.dialer:id/digits").getText();
  Assert.assertEquals(result, "+", "Actual value is : "+ result+ " did not match with expected value: +");
 }

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

No comments:

Post a Comment

Copyright © 2017 QALEARNINGGUIDE.COM || ALL RIGHTS RESERVED