【问题标题】:Horizontal scroll in appiumappium中的水平滚动
【发布时间】:2018-11-06 19:32:40
【问题描述】:

如何在 appium 中水平滚动 就像我们做的垂直滚动一样 driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView("")");

有这样的吗???

【问题讨论】:

    标签: selenium automation appium appium-android


    【解决方案1】:

    您可以将 UiSelector 设置为可滚动并设置滚动方向

    driver().findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true)."
                    + "resourceId(\"<id of scrollable control>\"))"
                    + ".setAsHorizontalList().scrollIntoView(new UiSelector().textContains(\"<text to search for>\"))"); 
    

    链接提供了有关UiScrollable的详细信息

    【讨论】:

    • 这有助于我在水平滚动视图中找出元素。但是当我使用 click() 时它会点击另一个元素。
    【解决方案2】:

    您可以使用这种方式,这将在水平方向工作,并且通过更改滚动开始和滚动结束,您也可以使用相同的代码进行垂直滚动。

    public void scrollHorizontal(double value1,double value2) {
    
    Dimension dimension =driver.manage().window().getSize();
    Double scrollheight = dimension.getHeight()*value1;
    int scrollStart = scrollheight.intValue();
    
    Double scrollheightEnd = dimension.getHeight()*value2;
    
    int scrollEnd = scrollheightEnd.intValue();
    
    new TouchAction((PerformsTouchActions)driver)
    .press(PointOption.point(scrollStart,0))
    .waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2)))
    .moveTo(PointOption.point(scrollEnd , 0))
    .release()
    .perform(); }
    

    【讨论】:

      【解决方案3】:

      使用以下方法进行水平滚动:

      public static void swipeHorizontal(AppiumDriver driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
              Dimension size = driver.manage().window().getSize();
              int anchor = (int) (size.height * anchorPercentage);
              int startPoint = (int) (size.width * startPercentage);
              int endPoint = (int) (size.width * finalPercentage);
              new TouchAction(driver).press(startPoint, anchor).waitAction(Duration.ofMillis(duration)).moveTo(endPoint, anchor).release().perform();
          }
      

      通过以下方式调用上述方法:

      swipeHorizontal((AppiumDriver) driver,0.9,0.01,0.5,2000);
      

      【讨论】:

        【解决方案4】:

        这适用于所有方向:

        枚举:

        public enum DIRECTION {
            DOWN, UP, LEFT, RIGHT;
        }
        

        实际代码:

        public static void swipe(MobileDriver driver, DIRECTION direction, long duration) {
            Dimension size = driver.manage().window().getSize();
        
            int startX = 0;
            int endX = 0;
            int startY = 0;
            int endY = 0;
        
            switch (direction) {
                case RIGHT:
                    startY = (int) (size.height / 2);
                    startX = (int) (size.width * 0.90);
                    endX = (int) (size.width * 0.05);
                    new TouchAction(driver)
                            .press(startX, startY)
                            .waitAction(Duration.ofMillis(duration))
                            .moveTo(endX, startY)
                            .release()
                            .perform();
                    break;
        
                case LEFT:
                    startY = (int) (size.height / 2);
                    startX = (int) (size.width * 0.05);
                    endX = (int) (size.width * 0.90);
                    new TouchAction(driver)
                            .press(startX, startY)
                            .waitAction(Duration.ofMillis(duration))
                            .moveTo(endX, startY)
                            .release()
                            .perform();
        
                    break;
        
                case UP:
                    endY = (int) (size.height * 0.70);
                    startY = (int) (size.height * 0.30);
                    startX = (size.width / 2);
                    new TouchAction(driver)
                            .press(startX, startY)
                            .waitAction(Duration.ofMillis(duration))
                            .moveTo(endX, startY)
                            .release()
                            .perform();
                    break;
        
        
                case DOWN:
                    startY = (int) (size.height * 0.70);
                    endY = (int) (size.height * 0.30);
                    startX = (size.width / 2);
                    new TouchAction(driver)
                            .press(startX, startY)
                            .waitAction(Duration.ofMillis(duration))
                            .moveTo(startX, endY)
                            .release()
                            .perform();
        
                    break;
        
            }
        }
        

        用法:

        swipe(driver, Util.DIRECTION.RIGHT);
        

        希望这会有所帮助,

        【讨论】:

          猜你喜欢
          • 2019-04-28
          • 1970-01-01
          • 2020-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多