4

I can't made Swipe action work. I did browsed through the web for a few days and found many similar questions but there is no working answer. Also I have tried TouchAction class, doesn't work as well.

I have Appium Version 1.4.13 (Draco) and using Java, TestNG framework.

btw I made scrolling work using (you can use the same logic as a pull to refresh) here is code sample.

public void scrollUp() {
    JavascriptExecutor js = (JavascriptExecutor) getDriver();
    HashMap<String, String> scrollObject = new HashMap<String,String();
    scrollObject.put("direction", "up");
    scrollObject.put("element", listElements.get(1).getText());
    js.executeScript("mobile: scroll", scrollObject);
}
pasine
  • 11,311
  • 10
  • 49
  • 81
Sashko
  • 191
  • 3
  • 10

5 Answers5

0

//Method names explain themselves. I hope it works.

            public static void swipeFromRightToLeftMultipleTimes(int howManySwipes) {
                    JavascriptExecutor js = (JavascriptExecutor) iosDriver;
                    for(int counter=0; counter < howManySwipes; counter++){
                        HashMap<String, Integer> swipeObject = new HashMap<String, Integer>();
                        swipeObject.put("startX", getScreenWidth());
                        swipeObject.put("startY", getScreenHeight());
                        swipeObject.put("endX", getScreenWidth()/4);
                        swipeObject.put("endY", getScreenHeight());
                        swipeObject.put("duration", 2);
                        js.executeScript("mobile: swipe", swipeObject);
                    }
            }

            public static void swipeFromLeftToRightMultipleTimes(int howManySwipes) {
                    JavascriptExecutor js = (JavascriptExecutor) iosDriver;
                    for(int counter=0; counter < howManySwipes; counter++){
                        HashMap<String, Integer> swipeObject = new HashMap<String, Integer>();
                        swipeObject.put("startX", getScreenWidth()/4);
                        swipeObject.put("startY", getScreenHeight());
                        swipeObject.put("endX", getScreenWidth());
                        swipeObject.put("endY", getScreenHeight());
                        swipeObject.put("duration", 2);
                        js.executeScript("mobile: swipe", swipeObject);
                    }
            }
Igor Vishnevskiy
  • 1,031
  • 2
  • 9
  • 13
  • Thanks you for the reply. Still doesn't work for me "org.openqa.selenium.WebDriverException: Not yet implemented. Please help us: http://appium.io/get-involved.html (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 4 milliseconds" – Sashko Sep 04 '15 at 02:32
0

Is this swipe issue in iOS or Android? Further, is it in real devices or simulators. I couldn't find a solution for iOS simulator for Xcode 7.0.1 with iOS 8.x / 9.0. Please let me know if it works for you.

However, Following is the solution perfectly work in Android real device as well in Android Simulator.

When calling swipeElement method provide element as MobileElement. use like this.. this.swipeElement(driver, MobileElement, 200, 3000); If the scroll length is positive then it scrolls to down, negative then scrolls up.

public void swipeElement(AndroidDriver driver, WebElement element, int scrollLength, int duration){ 

driver.context("NATIVE_APP");
int bottomY = element.getLocation().getY()+scrollLength;

((AppiumDriver)driver).swipe(element.getLocation().getX(), element.getLocation().getY(), element.getLocation().getX(), bottomY, duration); 

}
maxshuty
  • 9,708
  • 13
  • 64
  • 77
0

This solution works in iOS simulator with Appium 1.4.13; Java-Client 3.2.0; Xcode 7.0.1; iOS 8.x / 9.0;

Provide 'elementName' where scroll happens to the element.

JavascriptExecutor js = (JavascriptExecutor) iosDriv;
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put("element", ((RemoteWebElement) iosDriv.findElement(By.name("elementName"))).getId());
js.executeScript("mobile: scroll", scrollObject);

  • I no need to scroll gesture, I'm trying to swipe cell/row from the list of elements on the page (it is left to right swipe if to be more specific), your answer will scroll down only, but thanks anyway for your reply! – Sashko Nov 02 '15 at 05:52
  • Even for scroll this implementation will fail because scrollObject.put("direction", "up"); is missing. – Sashko Nov 13 '15 at 19:57
0
driver.context("NATIVE_APP"); 
        Dimension size = driver.manage().window().getSize(); 
        int startx = (int) (size.width * 0.8); 
        int endx = (int) (size.width * 0.20); 
        int starty = size.height / 2; 
        driver.swipe(startx, starty, endx, starty, 1000);

Use above code. Change startx to starty if you wish to swipe in opposite direction.

Emna Ayadi
  • 2,430
  • 8
  • 37
  • 77
-1

driver.swipe(startingXCoordinate,StartingYCoordinate,EndXCoordinate,EndYCoordinate,timeForSwipe);

Ex: driver.swipe(100,200,450,200,2000);

here, you are swipe from 100th X coordinate to 450th X coordinate.that is why Y coordinates are same for starting and ending points(200 and 200). The last parameter 2 indicates, swipe action will performs 2 seconds. Actually we have to mention 2000 milliseconds for 2 seconds.