There are situations, where we have to long press on key buttons of screen to trigger an event, to hold the element or type a letter,
LongPress can be done on a co-ordinate or on an element, we will see how to achieve this.
Example –
Let’s try with the android default Phone/Dialer app.
To display the dial “+”, we need long press the number “0” on the keypad.
For java-client 8.x with appium server 2.x
package com.test.apiTest; import io.appium.java_client.AppiumBy; import io.appium.java_client.AppiumDriver; import io.appium.java_client.android.options.UiAutomator2Options; import io.appium.java_client.ios.options.XCUITestOptions; import org.openqa.selenium.Point; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.PointerInput; import org.openqa.selenium.interactions.Sequence; import java.net.MalformedURLException; import java.time.Duration; import java.util.Collections; public class LongPress { static DesiredCapabilities cap; static AppiumDriver driver; @BeforeTest static void setup() throws MalformedURLException{ UiAutomator2Options options = new UiAutomator2Options() .setDeviceName("emulator-5554") .setPlatformVersion("12.0") .setAppPackage("com.android.dialer") .setAppActivity(".main.impl.MainActivity"); driver = new AndroidDriver(new URL("http://127.0.0.1:4723/"), options); } @Test static void TestLongPress() throws InterruptedException{ driver.findElement(AppiumBy.accessibilityId("key pad")).click(); WebElement zero = driver.findElement(AppiumBy.accessibilityId("0")); longPress(zero); longPress(zero); zero.click(); zero.click(); zero.click(); Thread.sleep(2000); WebElement back = driver.findElement(AppiumBy.accessibilityId("backspace")); back.click(); Thread.sleep(2000); longPress(back); Thread.sleep(3000); driver.quit(); } static void longPress(WebElement el){ Point location = el.getLocation(); PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger"); Sequence longpress = new Sequence(finger, 1); longpress.addAction(finger.createPointerMove(Duration.ofMillis(0),PointerInput.Origin.viewport(), location.x, location.y)); longpress.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg())); longpress.addAction(finger.createPointerMove(Duration.ofMillis(1000),PointerInput.Origin.viewport(), location.x, location.y)); longpress.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg())); ((AppiumDriver)driver).perform(Collections.singletonList(longpress)); } }
For java-client 7.x or 6.x –
In Appium, we have one method LongPress() which is under class TouchAction
Refer the API doc link [java-client version >= 6.1.0 as of now]
public T longPress(LongPressOptions longPressOptions)
public T longPress(PointOption longPressOptions)
@Test static void TestLongPress() throws InterruptedException{ Thread.sleep(2000); WebElement Add = driver.findElement(By.id("fab")); Add.click(); Thread.sleep(500); WebElement Zero = driver.findElement(By.xpath("//android.widget.TextView[@text='0']")); new TouchAction(driver) .longPress(LongPressOptions.longPressOptions().withElement((ElementOption.element(Zero)))) .release().perform(); //verify if the + sign displayed Assert.assertEquals(driver.findElement(By.id("digits")).getText(), "+"); }
Try to long press on “zero” button to print +