简体   繁体   中英

How to swipe Horizontally in appium Android

I need to swipe a element from left to right. How to achieve it.

Element screenshot

I tried:

new TouchAction(driver).press(214, 1219).moveTo(854,1199).release().perform();

But no luck. Can any one help me to swipe this button from left to right?

Hard-coding the x and y locations is not the best idea, but if you want to swipe horizontally, the y points should be the same. They're 20 pixels off in your example. It probably does not affect the outcome, however.

Here's my method to swipe/scroll:

/**
 * 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))
               .waitAction(WaitOptions.waitOptions(ofSeconds(1)))
               .moveTo(PointOption.point(endx, endy))
               .release()
               .perform();

}

Now I then have other methods that use screen coordinates to determine what the x's and y's should be, and then call the scroll method, like this:

/**
 * 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);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM