如何在android中为appium执行垂直滑动

时间:2017-12-12 11:55:41

标签: appium

我在真实设备上使用Appium 1.7和Android 8。但是我被卡住了。试过不同的组合。您能否提供一个简单的滑动功能代码?

尝试:

private void scrollDown() {
    //if pressX was zero it didn't work for me
    int pressX = driver.manage().window().getSize().width / 2;
    // 4/5 of the screen as the bottom finger-press point
    int bottomY = driver.manage().window().getSize().height * 4/5;
    // just non zero point, as it didn't scroll to zero normally
    int topY = driver.manage().window().getSize().height / 8;
    //scroll with TouchAction by itself
    scroll(pressX, bottomY, pressX, topY);
}

/*
 * don't forget that it's "natural scroll" where 
 * fromY is the point where you press the and toY where you release it
 */
private void scroll(int fromX, int fromY, int toX, int toY) {
    TouchAction touchAction = new TouchAction(driver);
    touchAction.longPress(fromX, fromY).moveTo(toX, toY).release().perform();
}

但没有运气.. !!

6 个答案:

答案 0 :(得分:1)

private void scroll(int fromX, int fromY, int toX, int toY) {
   TouchAction touchAction = new TouchAction(driver);
   touchAction.tap(fromX, fromY).waitAction(1000).moveTo(toX, 
   toY).release().perform();
}

按下后你必须等待。 这会奏效 可以使用它在从a点到b点的任何方向上滑动

答案 1 :(得分:0)

根据这个问题:Appium - Java, How to automate swipe in android,坐标的使用已弃用。我在那里发布了一个答案,其代码对我来说很有用

答案 2 :(得分:0)

您可以使用以下代码执行滚动/向上滑动操作:

Dimension size = this.driver.manage ()
    .window ()
    .getSize ();
int startX = size.getWidth () / 2;
int startY = size.getHeight () / 2;
int endX = 0;
int endY = (int) (startY * -1 * 0.75);
TouchAction action = new TouchAction (this.driver);
action.press (startX, startY)
    .moveTo (endX, endY)
    .release ()
    .perform ();

您可以调整代码以执行向左,向下和向右滑动。如果这对您有用,请告诉我。

答案 3 :(得分:0)

这给了我解决方案:

TouchAction动作=新的TouchAction(驱动程序); action.longPress(bottomElement).moveTo(topElement).release()执行();

答案 4 :(得分:0)

我已使用下面的代码进行滑动/滚动,并且运行良好
向上滑动代码

public boolean swipeFromUpToBottom() 
    {

     try {
          JavascriptExecutor js = (JavascriptExecutor) driver;
          HashMap<String, String> scrollObject = new HashMap<String, String>();
          scrollObject.put("direction", "up");
          js.executeScript("mobile: scroll", scrollObject);
          System.out.println("Swipe up was Successfully done.");
        }
           catch (Exception e) 
            {
                System.out.println("swipe up was not successfull");
            }   
        return false;               
    }

向下滑动代码

  public boolean swipeFromBottomToUp() 
    {       
      try  {
              JavascriptExecutor js = (JavascriptExecutor) driver;
              HashMap<String, String> scrollObject = new HashMap<String,String>);
              scrollObject.put("direction", "down");
              js.executeScript("mobile: scroll", scrollObject);
              System.out.println("Swipe down was Successfully done");
        }
           catch (Exception e) 
            {
                System.out.println("swipe down was not successfull");
            }   
        return false;                   
    }

滑动轮播图片的代码

public boolean swipeImages() 
    {       
      try   {
                  WebElement pageIndicator = driver.findElement(page_indicator);
                  String pageString= pageIndicator.getAttribute("value");
                  int length = pageString.length();
                  String count_string= pageString.substring(length-2, length).trim();
                  int count = Integer.parseInt(count_string);
                  System.out.println("Number of Image available to Swipe: "+count);
                  for (int i=0; i<=count; i++){          
                          JavascriptExecutor js = (JavascriptExecutor) driver;
                          HashMap<String, String> scrollObject = new HashMap<String, String>();
                          scrollObject.put("direction", "right");
                          js.executeScript("mobile: scroll", scrollObject);       
           }
           System.out.println("Swipe Successfully");
        }
           catch (Exception e) 
            {
                System.out.println("Image swipe was not successfull");
            }   
        return false;                   
    }

谢谢!

答案 5 :(得分:-1)

使用此最新代码,此代码可用于Appium Java Client 7.2.0

//Method - 1 
public void voidSwipevertical(AndroidDriver<MobileElement> driver, double startPercentage, double endPercentage){
    Dimension size=driver.manage().window().getSize();
    int width=(int)(size.getWidth()/2);
    int startPoint=(int)(size.getHeight()*startPercentage);

    int endPoint=(int)(size.getHeight()*endPercentage);
    new TouchAction(driver).press(PointOption.point(width, startPoint)).waitAction().moveTo(PointOption.point(width, endPoint)).release().perform();

}

//Method - 2 
public void voidSwipevertical2(AndroidDriver<WebElement> driver, double startPercentage, double endPercentage){
    Dimension size=driver.manage().window().getSize();
    int width=(int)(size.getWidth()/2);
    int startPoint=(int)(size.getHeight()*startPercentage);

    int endPoint=(int)(size.getHeight()*endPercentage);
    new TouchAction(driver).press(PointOption.point(width, startPoint)).moveTo(PointOption.point(width, endPoint)).release().perform();
}

//Method - 3
public void voidSwipevertical3(AndroidDriver<WebElement> driver, double startPercentage, double endPercentage) throws Exception{
    Dimension size=driver.manage().window().getSize();
    int width=(int)(size.getWidth()/2);
    int startPoint=(int)(size.getHeight()*startPercentage);

    int endPoint=(int)(size.getHeight()*endPercentage);

    TouchAction action = new TouchAction(driver); 
    action.longPress(PointOption.point(width, startPoint)).moveTo(PointOption.point(width, endPoint)).release().perform();
}