【问题标题】:I want to know a genric method for scrolling in appium java我想知道在appium java中滚动的通用方法
【发布时间】:2019-03-13 02:48:32
【问题描述】:

我想滚动浏览此应用程序的主页以及我需要通用滚动方法的相关其他应用程序

【问题讨论】:

  • 到目前为止你所做的尝试
  • Appium 标签有几十个(至少)关于这个主题的问题。您是否尝试过先在这里搜索答案?我知道我在不久的将来至少发布了一个答案。
  • 我已经尝试并测试了我找到的所有解决方案,但没有一个有效。请发布您认为适用的代码

标签: java eclipse selenium generics appium


【解决方案1】:

我确定我过去曾发布过此内容并且不想重新发布,但我似乎找不到它,所以我现在在这里发布。

下面是一组允许您向上、向下、向左和向右滚动的方法,前面是由前面四个方向滚动方法中的每一个调用的摘要(通用)滚动方法。

/**
 * 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
 */
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
 * @throws Exception
 */
public void scrollDown() throws Exception {

    //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);

}

/**
 * This method does a swipe left
 * @author Bill Hileman
 * @throws Exception
 */
public void swipeLeft() throws Exception {

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

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

    scroll(startx, starty, endx, starty);

}

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

    //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);

}

/**
 * This method does a swipe downwards
 * @author Bill Hileman
 * @throws Exception
 */
public void scrollUp() throws Exception {

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

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

    scroll(startx, starty, startx, endy);

}

【讨论】:

  • 如果你只想滚动,你应该调用scrollDown,然后调用scroll用正确的参数从屏幕高度的80%滚动到屏幕高度的20%。查看代码示例,如果您想更改百分比,请随意这样做,但我发现我硬编码的那些就足够了。
  • 我应该将所有这些方法放在一个单独的类中,然后在 main 中调用它还是让它们成为单独的方法?
  • 我应该在 int startx、int starty、int endx、int endy 中给出什么值?
  • 这些方法应该与您的驱动程序在同一个类中,在我的情况下称为“驱动程序”,我有一个名为 AppiumUtils 的类,其中包含我所有的 appium 辅助函数。我不确定为什么您对这个答案的原始评论以某种方式移至 cmets 列表的底部,但关于值,请参阅我的第一条评论。
  • 每当我调用方法、滚动、scrollDown 等时,它都会要求我将方法更改为静态并抛出声明。运行测试用例后,我收到如下错误:线程“主”org.openqa.selenium.WebDriverException 中的异常:处理命令时发生未知的服务器端错误。原始错误:坐标 [x=720.0, y=1184.0] 在元素 rect 之外:[0,0][720,1184](警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:0 毫秒此错误持续不断
【解决方案2】:

以下是appium中使用java实现水平和垂直滚动的通用方法。

public static MobileElement horizontalScroll(String selector) {
MobileElement element = (MobileElement) driver.findElement(MobileBy
        .AndroidUIAutomator("new UiScrollable(new UiSelector()).setAsHorizontalList()
        .scrollIntoView(new UiSelector().textContains(\""+ selector + "\"));"));
        return element;
    }

public static MobileElement verticalScroll(String selector) {
MobileElement element = (MobileElement) driver.findElement(MobileBy
        .AndroidUIAutomator("new UiScrollable(new UiSelector())
        .scrollIntoView(new UiSelector().textContains(\""+ selector + "\"));"));
        return element;
    }

【讨论】:

    猜你喜欢
    • 2019-03-14
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多