How to long press on android record button(during chat) for a particular time duration in appium by java

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).

TouchAction touchAction=new TouchAction(driver);
touchAction.longPress(element,duration).release().perform();
2 Likes

@Telmo_Cardoso. Thanks a lot for you reply, it’s working fine.

@Telmo_Cardoso could you please ans my below question

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()
1 Like

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.

1 Like

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

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();
1 Like

Thanks @Telmo_Cardoso
Edited

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

2 Likes

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

Above code worked for me. please try

TouchAction t = new TouchAction(driver);

WebElement first = driver.findElementById("com.nra.productmarketingmaker:id/btnControlRight");

        t.longPress(LongPressOptions.longPressOptions().withElement(element(first)).withDuration(Duration.ofSeconds(10))).release().perform();

I don’t know why it’s still tap for moment and release. any idea?

This code is working fine