How to do swipe gestures in iOS simualtor using java?

  1. I have tried with below method for swipe gestures.
  2. I need to swipe across photo gallery images both left to right and vice versa.
  3. I have tried so many solutions, but nothing worked at all.
  4. I am searching for reliable solution for swipe gestures for iOS app.
  5. I am using iOS simulator 8.3 version and java as language.
  6. If any one got swipes successfully in simulator means, please provide an solution.
  7. Really, I got struck with this swipe gestures, I don’t how to proceed. Please help me.

// method 1
WebElement el= driver.findElementByXPath(“//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAElement[1]/UIAScrollView[1]/UIAImage[1]”);
String orientation = driver.getOrientation().value();
System.out.println(“o=”+orientation);

     // get the X coordinate of the upper left corner of the element, then add the element's width to get the rightmost X value of the element
     int leftX = el.getLocation().getX();
     int rightX = leftX + el.getSize().getWidth();
     // get the Y coordinate of the upper left corner of the element, then subtract the height to get the lowest Y value of the element
     int upperY = el.getLocation().getY();
     int lowerY = upperY - el.getSize().getHeight();
     int middleY = (upperY - lowerY) / 2;
     if (orientation.equals("portrait")) {
       // Swipe from just inside the left-middle to just inside the right-middle of the element over 500ms
         driver.swipe(leftX + 5, middleY, rightX - 5, middleY, 500);
     }
     else if (orientation.equals("landscape")) {
       // Swipe from just inside the right-middle to just inside the left-middle of the element over 500ms
       driver.swipe(rightX - 5, middleY, leftX + 5, middleY, 500);
     }
     // method 2
   TouchAction act = new TouchAction(driver);
     WebElement To_elem = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAElement[1]/UIAScrollView[1]/UIAImage[2]");
     WebElement from_elem = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAElement[1]/UIAScrollView[1]/UIAImage[3]");
     act.press(from_elem ).moveTo(To_elem).release().perform();
        // method 3
       JavascriptExecutor js = (JavascriptExecutor) driver;
       Dimension size = driver.manage().window().getSize();
       HashMap<String, Integer> swipeObject = new HashMap<String, Integer>();
       swipeObject.put("startX", size.getWidth());
       swipeObject.put("startY", size.getHeight());
       swipeObject.put("endX", size.getWidth()/4);
       swipeObject.put("endY", size.getHeight());
       swipeObject.put("duration", 2);
       js.executeScript("mobile: swipe", swipeObject);
         // method 4
      TouchAction act = new TouchAction(driver);
        act.press(274, 151).moveTo(712, 151).release().perform();
        sleep(9000);
       // method 5
      TouchAction tAction=new TouchAction(driver);
        int startx = driver.findElement(By.className("UIACollectionCell")).getLocation().getX();
        int starty = driver.findElement(By.className("UIACollectionCell")).getLocation().getY();
        int endx = driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIACollectionView[1]/UIACollectionCell[6]")).getLocation().getX();
        int endy = driver.findElement(By.xpath(" //UIAApplication[1]/UIAWindow[1]/UIACollectionView[1]/UIACollectionCell[6]")).getLocation().getY();
        System.out.println(startx + " ::::::: " + starty + " ::::::: " + endx +  " ::::::: " +    endy);
        //First tap on the screen and swipe it right using moveTo function
        tAction.press(startx+20,starty+20).moveTo(endx+20,endy+20).release().perform();
        //Second tap on the screen and swipe it left using moveTo function
        tAction.press(endx+20,endy+20).moveTo(startx+20,starty+20).release().perform();

Hello @Selvi_Ranganathan,

I Ran across the same issue that you as i tried a lot of approaches but none worked for. It works on a real device tough and i don’t understand why…

Regards.

1 Like

AFAIK, swipe gestures have been broken in instruments for almost 2 years:

A couple of workarounds:

  • If it’s a scrollview, use scrolling (driver.scrollTo(element)) or:

      /**
       * Scrolls on the screen in specified direction 
       * @param direction "up" "down" "left" or "right"
       */
      public void scroll(String direction) {
      	TestLogger.logInfo("Scrolling '" + direction + "'");
      	HashMap<String, String> scrollObject = new HashMap<String, String>();
      	scrollObject.put("direction", direction);
      	//scrollObject.put("element", ((RemoteWebElement) element).getId());
      	driverInstance().executeScript("mobile: scroll", scrollObject);
      }
    
  • You can try this hacky python script that actually sends mouse clicks and drags to the OS, of course this wont work if you’re running on a remote appium server: https://github.com/vicwomg/swipeInWindow.py

Why do you think it wont work if we are using a Remote Appium Server.

@vicwomg, @sam_viz and @shankar_garg, After upgraded appium version and simulator version, swipe gestures works perfectly.

which version of appium are you using?