Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform swipe action in Selendroid?

  1. I have tried with below codings for swiping.
  2. While running the test case, the swipe action doesn't occurs and I am also not getting any error message.
  3. How can I swipe on both side from left to right and vice-versa.

There are two methods which are as follows:-

Method 1(using TouchActions):-

    1. //Swipe Right to Left side of the Media Viewer First Page
                    WebElement firstPages = driver.findElement(By.id("media-list"));
                    TouchActions flick = new TouchActions(driver).flick(firstPages,-100,0,0);
                    flick.perform();

    2. //perform swipe gesture
                   TouchActions swipe = new TouchActions(driver).flick(0, -20);
                   swipe.perform();

Method 2 (using javascript):-

public static void swipe(WebDriver driver) {

            JavascriptExecutor js = (JavascriptExecutor) driver;
            HashMap<String, Double> swipeObject = new java.util.HashMap<String, Double>();
            swipeObject.put("startX", 0.95);
            swipeObject.put("startY", 0.5);
            swipeObject.put("endX", 0.05);
            swipeObject.put("endY", 0.5);
            swipeObject.put("duration", 1.8);
            js.executeScript("mobile: swipe", swipeObject);
        }
like image 635
Galet Avatar asked Mar 23 '15 12:03

Galet


1 Answers

Try following implementation which inlcudes standard FlickAction.SPEED_NORMAL argument and also action builder for flick:

import org.openqa.selenium.interactions.touch.FlickAction;

private Action getBuilder(WebDriver driver) {
    return new Action(driver);
}

WebElement toFlick = driver().findElement(By.id("media-list"));
    Action flick = getBuilder(driver()).flick(toFlick, -500, 0, FlickAction.SPEED_NORMAL).build();
    flick.perform();

Swiping from left to right and vice verse can be performed by varying X-asis coordinates:

  • Swipe to the left:

Action flick = getBuilder(driver()).flick(toFlick, -500, 0, FlickAction.SPEED_NORMAL).build();

  • Swipe to the right:

Action flick = getBuilder(driver()).flick(toFlick, 500, 0, FlickAction.SPEED_NORMAL).build();

  • Swipe to the top:

Action flick = getBuilder(driver()).flick(toFlick, 0, 500, FlickAction.SPEED_NORMAL).build();

  • Swipe to the bottom:

Action flick = getBuilder(driver()).flick(toFlick, 0, -500, FlickAction.SPEED_NORMAL).build();

like image 158
Viktor Malyi Avatar answered Oct 18 '22 11:10

Viktor Malyi