3

I need to long press a button for a few secs and release, I have the following code,

WebElement recordButton = driver.findElement(By.id("xxxxxx/record_button"));
        TouchActions action = new TouchActions(driver);
        action.longPress(recordButton);
        action.release();
        action.perform();

I get the below errors

java.lang.ClassCastException: io.appium.java_client.android.AndroidDriver cannot be cast to org.openqa.selenium.interactions.HasTouchScreen

What I'm I doing wrong.

Trace:

java.lang.ClassCastException: io.appium.java_client.android.AndroidDriver cannot be cast to org.openqa.selenium.interactions.HasTouchScreen

Appium v1.8.1

Dependencies used:

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.internal.TouchAction;
import org.openqa.selenium.interactions.touch.TouchActions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.net.URL;

10
  • Try declaring your WebDriver as AndroidDriver<AndroidElement> and use AndroidElement instead of WebElement. Only a suggestions, not positive it will work. Also, doesn't longPress have a duration parameter? You might also need to do a .build() before the .perform() Aug 7, 2018 at 14:47
  • Doesn't work I tried both the suggestions Aug 7, 2018 at 14:57
  • 1
    Are you using Maven? If yes, could you provide your dependencies?
    – Alexey R.
    Aug 7, 2018 at 14:57
  • I have updated the question with the details Aug 7, 2018 at 15:00
  • 1
    Thanks @AlexeyR. for pointing that out, I managed to fix it using a different approach, I will post here. Pleas check Aug 8, 2018 at 10:01

2 Answers 2

2

The below worked for me,

TouchAction action = new TouchAction(driver).longPress(longPressOptions().withElement(element(recordButton)).withDuration(Duration.ofMillis(10000))).release().perform();
        Thread.sleep(5000);

Used Dependencies,

import static io.appium.java_client.touch.LongPressOptions.longPressOptions; import static io.appium.java_client.touch.offset.ElementOption.element; import io.appium.java_client.TouchAction;

5
  • You should mark your own answer as the "accepted answer" Aug 8, 2018 at 13:31
  • 1
    You can't mark your answers directly atleast in my case, I will do it once the needed time elapses which is now in 5 hrs Aug 9, 2018 at 8:55
  • @BillHileman person can not accept it's own answer. Never.
    – Prophet
    Jul 2, 2019 at 13:26
  • @Eliyahu I believe the green checkmark on this answer, where the original poster answered and his own question and has now accepted his own answer proves that you can indeed accept your own answer. I've done it to my own questions when I've later discovered a solution so that it does not still show up as unresolved. Jul 2, 2019 at 14:15
  • OK, but I'm sure this does not give points
    – Prophet
    Jul 2, 2019 at 15:49
0

Working solution with: Python 3.x, Appium 1.22.3-4, Mac M1

  • Long press on iOS can be achieve using snippet

TouchAction(driver).long_press(element).release().perform()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.