首页 > 解决方案 > Appium 6.1.0 TouchAction 与 TouchAction

问题描述

我正在寻找使用最新(此时)Appium Java-client 6.1.0 来创建 Tap/Swipe/Drag 等事件的“正确”或“最新”方式。我在 Appium 网站上看到了不同的文档(使用 TouchActions 进行点击,使用 TouchAction进行触摸),并且没有参考我应该使用哪个(以及哪些将被弃用?)。

new TouchAction(driver)
    .tap(tapOptions()
    .withElement(element(myElement)))
    .perform();

new TouchActions(driver)
    .singleTap(myElement)
    .perform();

看起来 TouchActions 是 Selenium 项目的一部分,而 TouchAction 是 Appium 的一部分,但这并不意味着 Appium 是正确的方法。

ps 我目前正在使用适用于 Android/iOS 的 Chrome/Safari 浏览器进行测试,但这并不意味着我不需要本机应用程序支持代码。

感谢您的时间

标签: appiumappium-iosappium-android

解决方案


您想使用 TouchAction (Appium)。

下面是我的一段代码。第一个是以坐标为参数的通用滚动函数。您通常不会直接调用该函数,它是由其他函数调用的,例如我在它下面包含的 scrollDown 函数,它计算坐标并调用通用滚动函数。

希望这可以帮助。

/**
 * This method scrolls based upon the passed parameters
 * @author Bill Hileman
 * @param int startx - the starting x position
 * @param int starty - the starting y position
 * @param int endx - the ending x position
 * @param int endy - the ending y position
 */
@SuppressWarnings("rawtypes")
public void scroll(int startx, int starty, int endx, int endy) {

    TouchAction touchAction = new TouchAction(driver);

    touchAction.longPress(PointOption.point(startx, starty))
               .moveTo(PointOption.point(endx, endy))
               .release()
               .perform();

}

/**
 * This method does a swipe upwards
 * @author Bill Hileman
 */
public void scrollDown() {

    //The viewing size of the device
    Dimension size = driver.manage().window().getSize();

    //Starting y location set to 80% of the height (near bottom)
    int starty = (int) (size.height * 0.80);
    //Ending y location set to 20% of the height (near top)
    int endy = (int) (size.height * 0.20);
    //x position set to mid-screen horizontally
    int startx = (int) size.width / 2;

    scroll(startx, starty, startx, endy);

}

推荐阅读