如何在Appium中从左向右滑动?

如何在Appium中从左向右滑动?,appium,appium-android,Appium,Appium Android,由于不推荐使用swipe(),我无法从左向右滑动屏幕。我的应用程序中有4个横幅,我想滑动以查看所有横幅。假设您创建了driver的AndroidDriver实例,您可以向左滑动: // Get location of element you want to swipe WebElement banner = driver.findElement(<your_locator>); Point bannerPoint = banner.getLocation();

由于不推荐使用swipe(),我无法从左向右滑动屏幕。我的应用程序中有4个横幅,我想滑动以查看所有横幅。

假设您创建了
driver
AndroidDriver
实例,您可以向左滑动:

    // Get location of element you want to swipe
    WebElement banner = driver.findElement(<your_locator>);
    Point bannerPoint = banner.getLocation();
    // Get size of device screen
    Dimension screenSize = driver.manage().window().getSize();
    // Get start and end coordinates for horizontal swipe
    int startX = Math.toIntExact(Math.round(screenSize.getWidth() * 0.8));
    int endX = 0;

    TouchAction action = new TouchAction(driver);
    action
            .press(PointOption.point(startX, bannerPoint.getY()))
            .waitAction(WaitOptions.waitOptions(Duration.ofMillis(500)))
            .moveTo(PointOption.point(endX, bannerPoint.getY()))
            .release();
    driver.performTouchAction(action);
//获取要滑动的元素的位置
WebElement banner=driver.findElement();
Point bannerPoint=banner.getLocation();
//获取设备屏幕的大小
维度screenSize=driver.manage().window().getSize();
//获取水平滑动的开始和结束坐标
int startX=Math.toIntExact(Math.round(screenSize.getWidth()*0.8));
int-endX=0;
TouchAction动作=新的TouchAction(驱动程序);
行动
.press(PointOption.point(startX,bannerPoint.getY()))
.waitAction(WaitOptions.WaitOptions(持续时间500毫秒))
.moveTo(PointOption.point(endX,bannerPoint.getY()))
.release();
驱动程序。执行动作(动作);

使用最新的Appium 1.8.x服务器这适用于所有方向:

枚举:

实际代码:

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,DIRECTION.RIGHT);
希望这会有帮助,

这应该会起作用

 Dimension size = driver.manage().window().getSize();
           System.out.println(size.height+"height");
           System.out.println(size.width+"width");
            System.out.println(size);
            int startPoint = (int) (size.width * 0.99);
            int endPoint = (int) (size.width * 0.15);
            int ScreenPlace =(int) (size.height*0.40);          
            int y=(int)size.height*20;

           TouchAction ts = new TouchAction(driver);
           //for(int i=0;i<=3;i++) {
           ts.press(PointOption.point(startPoint,ScreenPlace ))
          .waitAction(WaitOptions.waitOptions(Duration.ofMillis(1000)))
          .moveTo(PointOption.point(endPoint,ScreenPlace )).release().perform();
Dimension size=driver.manage().window().getSize();
系统输出打印项次(尺寸高度+高度);
System.out.println(大小.宽度+宽度);
系统输出打印项次(尺寸);
int起始点=(int)(size.width*0.99);
int端点=(int)(size.width*0.15);
int屏幕位置=(int)(大小、高度*0.40);
整数y=(整数)大小。高度*20;
TouchAction ts=新的TouchAction(驾驶员);
//对于(int i=0;i这是针对iOS Mobile的:
//我正在尝试从右向左滑动图像列表
//首先,我获取父元素(表/单元格)id
//然后使用谓词字符串搜索是否存在元素,然后尝试单击
List ele=getMobileElement(ListBtQueicKlink).findelelements(By.xpath(“.//XguiElementTypeButton”);

对于(inti=1;i请尝试下面的方法。它适用于Appium 1.16.0版本。 我创建了这个方法,根据屏幕上的特定元素位置向左或向右滑动 元素X:需要在其上执行滑动触摸操作的元素的X坐标。 元素Y:元素的Y坐标。 方向:左/右

    //method to left and right swipe on the screen based on coordinates
public void swipeAction(int Xcoordinate, int Ycoordinate, String direction) {
    //get device width and height
    Dimension dimension = driver.manage().window().getSize();
    int deviceHeight = dimension.getHeight();
    int deviceWidth = dimension.getWidth();
    System.out.println("Height x Width of device is: " + deviceHeight + " x " + deviceWidth);

    switch (direction) {
        case "Left":
            System.out.println("Swipe Right to Left");
            //define starting and ending X and Y coordinates
            int startX=deviceWidth - Xcoordinate;
            int startY=Ycoordinate; // (int) (height * 0.2);
            int endX=Xcoordinate;
            int endY=Ycoordinate;
            //perform swipe from right to left
            new TouchAction((AppiumDriver) driver).longPress(PointOption.point(startX, startY)).moveTo(PointOption.point(endX, endY)).release().perform();
            break;

        case "Right":
            System.out.println("Swipe Left to Right");
            //define starting X and Y coordinates
            startX=Xcoordinate;
            startY=Ycoordinate;
            endX=deviceWidth - Xcoordinate;
            endY=Ycoordinate;
            //perform swipe from left to right
            new TouchAction((AppiumDriver) driver).longPress(PointOption.point(startX, startY)).moveTo(PointOption.point(endX, endY)).release().perform();
            break;
    }
}
要获取元素X,Y坐标,请尝试以下方法

int elementX= driver.findElement(elementLocator).getLocation().getX();
int elementY= driver.findElement(elementLocator).getLocation().getY();

使用touchactionsHaven尚未对其进行测试,但看起来上下都不太正确
int elementX= driver.findElement(elementLocator).getLocation().getX();
int elementY= driver.findElement(elementLocator).getLocation().getY();