How to perform swipe action in Selendroid?
Asked Answered
L

3

11
  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);
        }
Lumbering answered 23/3, 2015 at 12:56 Comment(0)
L
2

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

Luster answered 20/4, 2015 at 9:39 Comment(5)
What is getBuilder method here ?Lumbering
Sorry, I've forgotten to add code for getBuilder. Check latest version of the answerLuster
I have tried your answer. But it did't perform swipe or any action on that particular code.Lumbering
Try to add explicit wait (1 sec.) before flick.perform(). It'll give you an idea whether you have to wait for the presence of element.Luster
Checked the documentation, public class org.openqa.selenium.interactions.touch.FlickAction extends org.openqa.selenium.interactions.internal.TouchAction implements org.openqa.selenium.interactions.Action. How can we instantiate an "Action" instance (as depicted in the code above) as its an Interface, not a concrete class. Am I missing something here.Gilead
P
0

A common reason for actions just completely failing to work when functionally testing JavaScript heavy web sites is that the actions are taken before the site has finished initialising. The simplest way to test this is the case is to add a short sleep before performing the action. Say 2 seconds.

If this solves the problem then you know you have a race condition between the page initialising and your test code running.

At that point you can rewrite your code to wait for the action to be possible.

Polypetalous answered 20/4, 2015 at 9:22 Comment(0)
O
0

I know this is an old question, but I encountered a number of similar problems with this.

As others have pointed out, it's generally a timing thing. So I found that if I waited for an element to be present (i.e. the driver has returned control and the page has rendered), and then did a brief sleep both before and after the flick action, it works, certainly on every page/app I've tested it with. So our code looks a bit like this:

WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("some-element-id)));
flickIt(driver, element -100, 0);

private void flickIt(WebDriver driver, WebElement el, int x, int y) {
    SyntheticsUtility.sleep(500); // put 0.5s sleeps around it to make sure, need to be stable
    TouchActions touch = new TouchActions(driver);
    touch.flick(el, x, y, FlickAction.SPEED_NORMAL);
    touch.perform();
    SyntheticsUtility.sleep(500);
}
Odalisque answered 28/7, 2016 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.