how to perform vertical swipe in android for appiu

2020-04-20 08:14发布

I am using Appium 1.7 and Android 8 on real device. But I am stuck with swipe up. tried with different combinations. Could you please provide an easy code for swipe functionality?

Tried:

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();
}

But no luck..!!

标签: appium
6条回答
来,给爷笑一个
2楼-- · 2020-04-20 08:55

According to this question: Appium - Java, How to automate swipe in android, the use of coordinates is Deprecated. I posted an answer there with the code that works for me in this case

查看更多
不美不萌又怎样
3楼-- · 2020-04-20 08:57
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();
}

you have to wait after pressing. This will work Can use this to swipe in any direction from point a to point b

查看更多
戒情不戒烟
4楼-- · 2020-04-20 09:04

You can perform scroll / swipe up action with below code:

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

You can tweak the code to perform left, down and right swipe. Let me know if this works for you.

查看更多
手持菜刀,她持情操
5楼-- · 2020-04-20 09:08

i have used the below code to Swipe / Scroll and it is working perfectly
Code to Swipe UP

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;               
    }

Code to Swipe DOWN

  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;                   
    }

Code for carousel images swipe

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;                   
    }

Thanks!!!

查看更多
看我几分像从前
6楼-- · 2020-04-20 09:17

Use this latest code this is working for 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();
}
查看更多
太酷不给撩
7楼-- · 2020-04-20 09:19

This gives me the solution:

TouchAction action = new TouchAction(driver); 
action.longPress(bottomElement).moveTo(topElement).release().perform();
查看更多
登录 后发表回答