This site is in read only mode. Please continue to browse, but replying, likes, and other actions are disabled for now.
1 / 16
Feb 2017

How to long press on android record button for a particular time in appium test, by java. I have tried 2 ways but both are not working at all, those are:

Way 1:
By pressRecBtn = By.id(“recorderButton”);
int x = 353; // x coordinate of device screen, get it after enabling the Show touch and Pointer location from developer option
int y = 980; // same as x
int timeInMs = 4000;
Action.longPress(driver.findElement(pressRecBtn)).longPress(x, y, timeInMs).perform();

Way2:
By pressRecBtn = By.id(“recorderButton”);
int timeInMs = 4000;

Action.longPress(driver.findElement(pressRecBtn)).waitAction(timeInMs).perform();

for this way its press on rec button but for a default time(>=1000 MS).

  • created

    Feb '17
  • last reply

    Nov '18
  • 15

    replies

  • 3.6k

    views

  • 5

    users

  • 4

    likes

  • 3

    links


TouchAction touchAction=new TouchAction(driver);
touchAction.longPress(element,duration).release().perform();
6 months later

Hi Telmo!
with appium latest jar it shows longpress is deprecated when i give time limit(duration) so how can i perform it i need to use duration

with out duration it didn’t shows the deprecated error

public TouchAction longPress(WebElement el, int duration) is deprecated and was replaced by public TouchAction longPress(WebElement el, Duration duration)

so:

new TouchAction(driver).longPress(element, Duration.ofMillis(duration)).release().perform()

with the above code it sows error for The method ofMillis(int) is undefined for the type Duration can you provide the correct one please.

Just explore… import java.time.Duration and build your duration with milliseconds, seconds, whatever you prefer.

ohh Thank you Telmo! i took >>import org.openqa.selenium.support.ui.Duration;
now its working fine

6 months later

Hi @Telmo_Cardoso,
I’m using
Java client: 5.0.4
Selenium Java: 3.7.1

I have also imported: import java.time.Duration

Its suggesting me for importing those:

After importing both suggestions: new TouchAction((PerformsTouchActions) driver).longPress((WebElement) recBtn, Duration.ofMillis(10000)).release().perform();

but its not working, showing the error: java.lang.ClassCastException: org.openqa.selenium.By$ById cannot be cast to org.openqa.selenium.WebElement

Could you please tell me what’s wrong with me?

You driver is instance of what class?
You element recBtn is instance of what class?

your recBtn seems to be instance of org.openqa.selenium.By and should be WebElement or MobileElement for instance.

Thanks @Telmo_Cardoso
Problem solved, its working fine now, here is the update for Java Client: 5.0.4

WebElement recBtn = driver.findElement(MobileBy.id("img_button"));
new TouchAction((MobileDriver) driver).press(recBtn).waitAction(Duration.ofMillis(10000)).release().perform();

Not for higher :slight_smile: For Java client 6.0.0-BetaX those methods are deprecated and should use in that case:

new TouchAction(driver).longPress(longPressOptions().withElement(element(recBtn)).withDuration(Duration.ofMillis(10000))).release().perform();
2 months later

new TouchAction(mUser.mDriver)
.longPress(new LongPressOptions()
.withElement(ElementOption.element(mWebElement))
.withDuration(Duration.ofMillis(duration)))
.release()
.perform();

5 months later

new TouchAction(driver).longPress(longPressOptions().withElement(element(camera)).withDuration(java.time.Duration.ofMillis(60000))).release().perform();

Above code worked for me. please try