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;

WiredTheories
  • 281
  • 3
  • 9
  • 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() – Bill Hileman Aug 07 '18 at 14:47
  • Doesn't work I tried both the suggestions – WiredTheories Aug 07 '18 at 14:57
  • 1
    Are you using Maven? If yes, could you provide your dependencies? – Alexey R. Aug 07 '18 at 14:57
  • I have updated the question with the details – WiredTheories Aug 07 '18 at 15:00
  • We need to know your versions as well, for Appium, Selenium at least. – Bill Hileman Aug 07 '18 at 15:00
  • It is also worth providing a stacktrace since we need to know where exactly it was thrown (Class, method, line) – Alexey R. Aug 07 '18 at 15:06
  • @AlexeyR. I have added the version and the trace – WiredTheories Aug 08 '18 at 07:34
  • What you have added is not a trace. It is just an exception message. Trace means showing the call stack for the place where exception occurred (see https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors). Also you shouw only AppiumDriver version. But what is the version of your AndroidDriver? – Alexey R. Aug 08 '18 at 09:24
  • 1
    Thanks @AlexeyR. for pointing that out, I managed to fix it using a different approach, I will post here. Pleas check – WiredTheories Aug 08 '18 at 10:01
  • As per Appium 1.10.0 and io.appium.java_client - 7.0, the original issue still exists and the provided solution still works. – Djambazoff Jan 03 '19 at 01:55

2 Answers2

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;

WiredTheories
  • 281
  • 3
  • 9
  • You should mark your own answer as the "accepted answer" – Bill Hileman Aug 08 '18 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 – WiredTheories Aug 09 '18 at 08:55
  • @BillHileman person can not accept it's own answer. Never. – Prophet Jul 02 '19 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. – Bill Hileman Jul 02 '19 at 14:15
  • OK, but I'm sure this does not give points – Prophet Jul 02 '19 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()

Narendra Chandratre
  • 2,776
  • 7
  • 29
  • 60