How to automate swipe gestures for iOS mobile app using iOS simulator in java?

  1. I need to automate swipe gesture for both side(Right To Left and Left to Right) in iOS mobile app.
  2. I am using appium version 1.4.8 , iOS simulator 6 and platform version as 8.3.
  3. I am writing test cases using java language.
  4. I have tried with below coding for swipe gestures. But, the swipe action doesn’t happens for me.

public void swipeLeftToRight(AppiumDriver driver) {
Dimension size = driver.manage().window().getSize();
int endx = (int) (size.width * 0.8);
int startx = (int) (size.width * 0.20);
int starty = size.height / 2;
driver.swipe(startx, starty, endx, starty, 1000);
}

public void swipeRightToLeft(AppiumDriver driver) {
    Dimension size = driver.manage().window().getSize();
    int startx = (int) (size.width * 0.8);
    int endx = (int) (size.width * 0.10);
    int starty = size.height / 2;
    driver.swipe(startx, starty, endx, starty, 1000);
}

Are you getting any error while swiping. Try to increase swipe duration to 3000 /4000 and then check.

You can also debug by printing coordinates passed to swipe method.

Thanks for your reply, I didn’t get any error message at all. But, the swipe action doesn’t happens for me. I have printed the size and x coordinates value and given a try with different values, but swipe action doesn’t occurs. I don’t exactly, how to grab the coordinates and pass it in swipe().

I got the swipe working with c# using ITouchAction. Hope this can help you with your problem.

    protected void SwipeLeft(AppiumWebElement row)
    {
        int xShift = Convert.ToInt32(row.Size.Width * 0.20);
        int xStart = (row.Size.Width) - xShift;
        int xEnd = xShift;

        Swipe(row, xStart, xEnd);
    }

    private void Swipe(AppiumWebElement row, int xStart, int xEnd)
    {
        ITouchAction action = new TouchAction(driver)
            .Press(row, xStart, (row.Size.Height / 2))
            .Wait(1000)
            .MoveTo(row, xEnd, (row.Size.Height / 2))
            .Release();

        action.Perform();
        Thread.Sleep(2000);
    }
2 Likes

Thanks Alan :slight_smile: I need this

@alangithubtrader I need to get partial swipe left so that button gets shown on right side of the element.

can you please clarify how to achieve it, with that code I could do swipe action and goto next screen; similar to tap action on element.

Thanks,
Vikram

@VikramVI Your problem is probably due to the xshift being too small, if you set it to a larger value that may help with what you’re trying to achieve.

Have you tried the latest stable version of Appium? 1.5.3 works well for me and my swipes are working there. Version 1.4.8 seems kinda old and may have had some issues.

Hi, can someone please give me example of code, how to swipe ios element to the left? please, I’m struggling with this for days. :frowning:

public void scrollIos(String move){
    JavascriptExecutor js = (JavascriptExecutor) driver;
    HashMap<String, String> scrollObject = new HashMap<String, String>();
    scrollObject.put("direction", move);
    js.executeScript("mobile: scroll", scrollObject);
}

pass “right” or “left” as move string

1 Like

Thank you very much! You saved me, it works! :slight_smile:

Hi,
I am trying to swipe an element & perform some action which need waiting duration.
I am working iOS native app.
Here is my code:
public void swipeLeft(By locator){
RemoteWebElement iosElement=(RemoteWebElement) wd.findElement(locator);
JavascriptExecutor js = (JavascriptExecutor) wd;
HashMap swipeObject = new HashMap();
swipeObject.put(“direction”, “left”);
swipeObject.put(“duration”, “5000”);
swipeObject.put(“element”, iosElement.getId());
js.executeScript(“mobile: swipe”, swipeObject);
}
Here, element is getting swipe but don’t wait to perform any action on it.
Has anyone came across such issue?

Thanks

Did you solve this issue?

As I know, xcuitest-driver provides “velocity”: 250 for speed up/down the swipe action.
However, I don’t see the affect.
Can you share your way?

It’s working as expected