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);
}
//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);
}
}
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.
来源:https://stackoverflow.com/questions/32387357/i-cant-make-swipe-gesture-work-for-me-in-appium-using-java