【问题标题】:How to swipe Horizontally in appium Android如何在appium Android中水平滑动
【发布时间】:2021-02-26 15:58:47
【问题描述】:

我需要从左向右滑动一个元素。如何实现。

Element screenshot

我试过了:

new TouchAction(driver).press(214, 1219).moveTo(854,1199).release().perform();

但没有运气。谁能帮我从左向右滑动这个按钮?

【问题讨论】:

  • 什么不起作用?根本不用刷?滑动还不够?
  • @MikeCollins 它没有滑动按钮。也没有抛出任何错误。

标签: java android appium


【解决方案1】:

硬编码 x 和 y 位置不是最好的主意,但如果您想水平滑动,y 点应该相同。在您的示例中,它们相差 20 像素。但是,它可能不会影响结果。

这是我的滑动/滚动方法:

/**
 * 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))
               .waitAction(WaitOptions.waitOptions(ofSeconds(1)))
               .moveTo(PointOption.point(endx, endy))
               .release()
               .perform();

}

现在我有其他方法使用屏幕坐标来确定 x 和 y 应该是什么,然后调用滚动方法,如下所示:

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

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

    //Starting x location set to 5% of the width (near left)
    int startx = (int) (size.width * 0.05);
    //Ending x location set to 95% of the width (near right)
    int endx = (int) (size.width * 0.95);
    //y position set to mid-screen vertically
    int starty = size.height / 2;

    scroll(startx, starty, endx, starty);

}

【讨论】:

  • @SubanDhyako 我希望您在评论中添加您的行作为建议。我的代码中没有那行,没有它也可以正常工作。我并不是说这不是一个有效的补充,但我还没有用那个改变来测试它,答案就是我的。
猜你喜欢
  • 2018-11-03
  • 2016-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-06
  • 1970-01-01
  • 2018-05-18
  • 2014-01-12
相关资源
最近更新 更多