ClassCastException on using Appium TouchActions

孤者浪人 提交于 2019-12-11 15:16:45

问题


Im trying to perform tap action using Appium TouchActions class, but it throws exceptions. Please provide your solution.

import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.interactions.touch.TouchActions;
===========================================================
WebDriver driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities());
        Thread.sleep(5000);
        String title = driver.findElement(By.id("app_title")).getText();
        System.out.println("TITLE: " + title);
        WebElement ele = driver.findElement(By.id("bt1"));
        TouchActions touch = new TouchActions(driver);
        touch.singleTap(ele);
        touch.perform();

Output and Exception:

Aug 02, 2018 6:51:11 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
TITLE: TestApp
java.lang.ClassCastException: io.appium.java_client.android.AndroidDriver cannot be cast to org.openqa.selenium.interactions.HasTouchScreen
at org.openqa.selenium.interactions.touch.TouchActions.<init>(TouchActions.java:38)
at scratchpad.MobileAutomation.main(MobileAutomation.java:23)

Process finished with exit code 0

Version Used: selenium-java: 3.11.0 java-client(io.appium): 5.0.4

Let me know, if you want any other information. Also give me suggestion best way to perform Mobile operation such as swipe(all direction), tap, double tap, long press, etc. Thanks in advance.


回答1:


Use Appium TouchAction instead of Selenium TouchActions. It is also handful to put the below code into parent of your test class:

import io.appium.java_client.TouchAction;

public AndroidDriver<MobileElement> driver = new TouchAction(driver);

public void tap(MobileElement element) {

  getTouchAction()
    .tap(
      new TapOptions().withElement(
        ElementOption.element(
          element)))
    .perform();
}

Call the method ():

tap(myMobileElement);


来源:https://stackoverflow.com/questions/51655230/classcastexception-on-using-appium-touchactions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!