Appium 6.1.0 TouchAction 与 TouchAction

Posted

技术标签:

【中文标题】Appium 6.1.0 TouchAction 与 TouchAction【英文标题】:Appium 6.1.0 TouchActions vs TouchAction 【发布时间】:2019-04-09 22:46:22 【问题描述】:

我正在寻找使用最新(此时)Appium Java 客户端 6.1.0 来创建 Tap/Swipe/Drag 等事件的“正确”或“最新”方式。 我在 Appium 网站上看到了不同的文档(Tap using TouchActions、Touch using TouchAction),但没有参考我应该使用哪个(哪些会被弃用?)。

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

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

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

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

感谢您的宝贵时间

【问题讨论】:

【参考方案1】:

您想使用 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);


【讨论】:

感谢您的快速回答。您是否可以解释为什么这是要使用的正确操作?实现并不难,appium 设计使用正确的对象是真正的问题。 坦率地说,在我自己看来,这是正确的操作,因为它是我能找到的唯一可行的选项。我也注意到了 Appium 的在线文档,并尝试了“替代”方法,包括使用 javascript。它们都不起作用我相信(但我可能错了)Selenium 的 TouchActions 类更多地用于触摸屏等触摸设备,而不一定是移动设备。它还以完全不同的方式实现 - Appium 使用 REST 服务进行通信,而 Selenium 使用浏览器仿真。 Appium中有滚动动作:TouchActions action = new TouchActions(driver); action.scroll(element, 10, 100); action.perform();Appium official usage 我似乎记得在早期的项目中使用过它,但是当我尝试将它与较新版本的 Appium/Selenium 一起使用时,它根本不起作用。在我找到这个解决方案之前,这是一项令人沮丧的研究。我可能是错的,由于各种更新,替代方法可能会再次起作用,我只是没有费心检查,因为我现在已经工作了。【参考方案2】:

使用TouchAction 类的最新方法是使用AndroidTouchAction 类而不是TouchAction 类,因为它现在是通用的。这就是为什么您会在最后一个答案中看到@SuppressWarnings("rawtypes")

这是在 6.1.0 中点击元素的方式

对于安卓:

AndroidTouchAction touch = new AndroidTouchAction (driver);
touch.tap (TapOptions.tapOptions ()
    .withElement (ElementOption.element (e)))
  .perform ();

对于 iOS:

IOSTouchAction touch = new IOSTouchAction (driver);
touch.tap (TapOptions.tapOptions ()
    .withElement (ElementOption.element (e)))
  .perform ();

【讨论】:

这听起来比以前更准确,谢谢

以上是关于Appium 6.1.0 TouchAction 与 TouchAction的主要内容,如果未能解决你的问题,请参考以下文章

Appium解锁九宫格(TouchAction)

appium---TouchAction

关于appium中的TouchAction的用法

appium+python自动化33-解锁九宫格(TouchAction)

appium--连续滑动

Appium 元素定位 控件定位 uiautomatorviewer TouchAction Toast