【问题标题】:How to scroll a layout view from right to left in Appium?如何在 Appium 中从右到左滚动布局视图?
【发布时间】:2019-09-27 04:29:00
【问题描述】:

想在Appium中从右到左滑动视图,我尝试了坐标,动态也尝试过,但无法滑动视图,每当我滑动视图时,就会出现一个图标,我想单击图标。

我的测试用例已执行,但我无法滑动视图。 我尝试了 TouchAction,使用 co_ordinates。

         Boolean found_result = false;
         String a = "offlin";
         List<AndroidElement> listele = driver.findElementsByClassName("android.widget.LinearLayout");
         System.out.println("swipe = "+listele.size()); 

         int size=0;
         size = size+listele.size();
         AndroidElement slider = listele.get(0);
         Point startButtonPoint = slider.getLocation();
            Dimension startButtonDimenstion = slider.getSize();
         int y = startButtonPoint.getY() + startButtonDimenstion.getHeight()/2;
            int xStart = startButtonPoint.getX() + (int)(startButtonDimenstion.getWidth() * 0.7);
            int xEnd = (int)(startButtonPoint.getX() * 0.3);
         System.out.println("lists ele size"+size);
         for (int i = 0; i < size; i++) {

             String s = listele.get(i).getText();
             if (s.equals(a)) {

                 found_result =true;

                 driver.swipe(xStart, y, xEnd, y, 2000);
                  System.out.println("found : "+size);
                 break;
             }

         }
         if(!found_result){
             System.out.println("swipped cond");
      }

正如我所料,想从右向左滑动视图,在一个视图可见后,想点击视图完成任务。

【问题讨论】:

    标签: eclipse appium


    【解决方案1】:
    1. 你可以考虑执行mobile:swipe commandlike:

      JavascriptExecutor js = (JavascriptExecutor) driver;
      Map<String, Object> params = new HashMap<>();
      params.put("direction", "down");
      params.put("element", ((RemoteWebElement) element).getId());
      js.executeScript("mobile: swipe", params);
      
    2. 如果您的测试仅适用于 Android,您可以考虑执行 mobile:shell command

      Map<String, Object> args = new HashMap<>();
      args.put("command", "input");
      args.put("args", Lists.newArrayList("swipe", "startX","startY","endX","endY"));
      driver.executeScript("mobile: shell", args);
      
    3. 最后但并非最不重要的一点是,您可以使用SeeTest Appium Extension,它提供Swipe command

      seetest.swipe("Right", 10, 500);
      

    【讨论】:

      【解决方案2】:

      使用以下代码在可用视图中执行向右滑动。

      MobileElement element_to_be_swiped=driver.findElement("//xpathtothelinearpath")
      Point elementLoc = elememt_to_be_swiped.getLocation();
      int eleX = elementLoc.getX() + 5;
      int eleY = elementLoc.getY() + 5;
      Dimension elementDimen = elememt_to_be_swiped.getSize();
      int eleH = elementDimen.getHeight();
      int eleW = elementDimen.getWidth();
      Dimension size = driver.manage().window().getSize();
      int x = (int)(eleX + eleW * 0.5D);
      int y = (int)(eleY + eleH * 0.5D);
      
      TouchAction<?> action = new TouchAction(driver);
      action.press(PointOption.point(x, y)).moveTo(PointOption.point(width - 5, y)).release().perform();
      

      【讨论】:

        【解决方案3】:

        下面是我用来滚动和滑动的代码。请注意,有一个主滚动方法被调用,并使用由四个方向滚动/滑动方法中的每一个计算出的适当参数来调用。

        /**
         * 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);
        
        }
        
        /**
         * This method does a swipe left
         * @author Bill Hileman
         */
        public void swipeLeft() {
        
            //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
         */
        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);
        
        }
        
        /**
         * This method does a swipe downwards
         * @author Bill Hileman
         */
        public void scrollUp() {
        
            //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);
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多