24Sometimes you need to press and hold(long press) an element for few seconds to get required result In your android software app. In this case we can use longPress(org.openqa.selenium.WebElement el) method of TouchAction class In appium automation test. Earlier we have used longPress method to generate action chain of DRAG AND DROP, SWIPE ACTION and MULTI TOUCH ACTION. Here I am presenting simple example on exact usage of longPress method of TouchAction class In appium software automation test.
App To Use In This Test
We will use android mobile Dialler app In this example to understand usage of LongPress method. Dialler app will be Installed by default In all android devices so no need to Install It.
Aim To Achieve
On dialler pad, 0 button has two functions. 1. It will dial 0 on normal press. 2. It will dial + on long press. Try It manually first.
In our appium software automation test, We will long press 0 button of dialler pad to dial + as shown In bellow Image. It will type + In dial number textbox.
Create And Run Android Appium LongPress Test
Create new class file DialPad.java under your project's package In eclipse and copy paste bellow given android appium software test script In It.
DialPad.java
package Android;
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 DialPad {
AndroidDriver driver;
@BeforeTest
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "ZX1B32FFXF");
capabilities.setCapability("browserName", "Android");
capabilities.setCapability("platformVersion", "4.4.2");
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 dial() {
//Click on dial pad button to open dialer pad.
driver.findElementById("com.android.dialer:id/dialpad_button").click();
//Create object of TouchAction class.
TouchAction Action = new TouchAction(driver);
//Create action chain using TouchAction class reference to perform long press action on button 0 of dialer pad.
Action.longPress(driver.findElement(By.name("0"))).perform();
//Get the result from dial text box.
String result = driver.findElementById("com.android.dialer:id/digits").getText();
//Compare actual and expected result using testng assertion.
Assert.assertEquals(result, "+", "Actual value is : "+ result+ " did not match with expected value: +");
}
@AfterClass
public void tearDown() {
driver.quit();
}
}
As you can see In above android appium software automation test script,
- First It will launch dialler pad app using given package and activity name.
- Open dialler pad by clicking on dial pad button.
- Action.longPress method Is used to press and hold 0 button on dial pad.
- Next statement will get text from dial text box.
- Used testng assertion Assert.assertEquals to compare actual and expected result. You can learn more about testng and It's assertions on THIS PAGE.
This Is the way of using longpress method of TouchAction class to long press on any element of your android software app.
Hi,
ReplyDeleteNice Article !!
I have a question could you please tell me AppiumDriver is better option or AndroidDriver when initialize the driver..
Please reply!!
Thanks,