I can't make swipe gesture work for me in Appium using Java

折月煮酒 提交于 2019-12-04 11:13:41

//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);
                    }
            }
Hari Nallani

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

}

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!