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

如何在android for appium中执行垂直滑动,appium,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 fing

我在真实设备上使用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();
}

但是没有运气

根据这个问题:,不推荐使用坐标。我在那里贴了一个答案,上面的代码在这种情况下对我有效

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的任何方向上滑动

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

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

您可以调整代码以执行向左、向下和向右滑动。让我知道这是否适用于您。

这为我提供了解决方案:

TouchAction action = new TouchAction(driver); 
action.longPress(bottomElement).moveTo(topElement).release().perform();

我使用了下面的代码来滑动/滚动,它工作得很好
向上滑动的代码

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 swipeFromUpToBottom()
{
试一试{
JavascriptExecutor js=(JavascriptExecutor)驱动程序;
HashMap scrollObject=新建HashMap();
滚动对象。放置(“方向”,“向上”);
js.executeScript(“移动:滚动”,滚动对象);
System.out.println(“刷卡成功完成”);
}
捕获(例外e)
{
System.out.println(“刷卡未成功”);
}   
返回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 swipeFromBottomToUp()
{       
试一试{
JavascriptExecutor js=(JavascriptExecutor)驱动程序;
HashMap scrollObject=新HashMap);
滚动对象。放置(“方向”,“向下”);
js.executeScript(“移动:滚动”,滚动对象);
System.out.println(“向下滑动已成功完成”);
}
捕获(例外e)
{
System.out.println(“向下滑动未成功”);
}   
返回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;                   
    }
public boolean swipeImages()
{       
试一试{
WebElement pageIndicator=driver.findElement(页面指示器);
String pageString=pageIndicator.getAttribute(“值”);
int length=pageString.length();
String count_String=pageString.substring(长度-2,长度).trim();
int count=Integer.parseInt(count\u字符串);
System.out.println(“可刷卡的图像数:“+count”);
对于(int i=0;i使用此最新代码,此代码适用于Appium Java Client 7.2.0
//方法-1
公共无效无效SwipeVertical(AndroidDriver驱动程序,双起始百分比,双结束百分比){
维度大小=driver.manage().window().getSize();
int-width=(int)(size.getWidth()/2);
int startPoint=(int)(size.getHeight()*startPercentage);
int端点=(int)(size.getHeight()*endPercentage);
新建TouchAction(驱动程序)。按(PointOption.point(width,startPoint)).waitAction().moveTo(PointOption.point(width,endPoint)).release().perform();
}
//方法2
公共作废作废SWIPERTICAL2(AndroidDriver driver、双倍起始百分比、双倍结束百分比){
维度大小=driver.manage().window().getSize();
int-width=(int)(size.getWidth()/2);
int startPoint=(int)(size.getHeight()*startPercentage);
int端点=(int)(size.getHeight()*endPercentage);
新建触摸操作(驱动程序)。按(PointOption.point(width,startPoint)).moveTo(PointOption.point(width,endPoint)).release().perform();
}
//方法-3
public void voidSwipevertical3(AndroidDriver驱动程序、double startPercentage、double endPercentage)引发异常{
维度大小=driver.manage().window().getSize();
int-width=(int)(size.getWidth()/2);
int startPoint=(int)(size.getHeight()*startPercentage);
int端点=(int)(size.getHeight()*endPercentage);
TouchAction动作=新的TouchAction(驱动程序);
action.longPress(PointOption.point(width,startPoint)).moveTo(PointOption.point(width,endPoint)).release().perform();
}

这会给出一个负值。“int endY=(int)(startY*-1*0.75);”。因此,坐标不可用异常抛出。如果您使用的是java客户端5.0或更高版本,那么这将很好地工作,因为我在我的框架中使用它,并且这段代码也很好。基本上-ve endY将指定从屏幕中心移动到屏幕上边框的应用程序。我使用java 1.8,它给出了-ve值。您能确认吗您使用的是我发布的完整代码,或者您对其进行了修改。如果您进行了修改,您可以发布您的代码版本吗?我尝试了使用您的整个代码。driver.swip(size.width/2,size.height-1,size.width/2200,2000);此driver.swipe对我不起作用。它显示“将强制转换添加到”driver“也将滚动到”也不起作用。你能给我一些建议吗。我们需要在这里直接传递坐标值吗?int-fromX、int-fromY、int-toX、int-toYTap()是错误的。但需要等待是正确的。正确的做法是按()然后等待()这个答案是错误的。C#驱动程序中既没有ActionOptions也没有PointOptions。我想这是一个只存在于Java beta版中的特性。
//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();
}