1

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;

  • 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 7 '18 at 14:47
  • Doesn't work I tried both the suggestions – WiredTheories Aug 7 '18 at 14:57
  • 1
    Are you using Maven? If yes, could you provide your dependencies? – Alexey R. Aug 7 '18 at 14:57
  • I have updated the question with the details – WiredTheories Aug 7 '18 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 – WiredTheories Aug 8 '18 at 10:01
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;

  • You should mark your own answer as the "accepted answer" – Bill Hileman Aug 8 '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 9 '18 at 8:55
  • @BillHileman person can not accept it's own answer. Never. – Eliyahu Jul 2 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 2 at 14:15
  • OK, but I'm sure this does not give points – Eliyahu Jul 2 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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