Left or Right swipe not working in appium for android app

I’m trying to swipe right to left from the first content(there is no content at left hand side) So far I’m using below, but it’s not working.

public void swipeRightToLeft() {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        HashMap<String, Double> swipeObject = new HashMap<String, Double>();
        swipeObject.put("startX", 0.9);
        swipeObject.put("startY", 0.5);
        swipeObject.put("endX", 0.01);
        swipeObject.put("endY", 0.5);
        swipeObject.put("duration", 3.0);
        js.executeScript("mobile: swipe", swipeObject);
}

public void swipeLeftToRight() {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        HashMap<String, Double> swipeObject = new HashMap<String, Double>();
        swipeObject.put("startX", 0.01);
        swipeObject.put("startY", 0.5);
        swipeObject.put("endX", 0.9);
        swipeObject.put("endY", 0.6);
        swipeObject.put("duration", 3.0);
        js.executeScript("mobile: swipe", swipeObject);
}

Error log: 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)

you need switch to:
new TouchAction(this).press(startx, starty).waitAction(duration).moveTo(endx, endy).release().perform();

some other example may look here - https://github.com/appium/java-client/blob/master/src/test/java/io/appium/java_client/android/AndroidTouchTest.java

1 Like

what would be the value of startx, starty, endx and endy ?

Those coordinates depend on the size of your android. If you want to continue to use percentage you can use the following method:

public static void swipeHorizontal(AppiumDriver driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
    Dimension size = driver.manage().window().getSize();
    int anchor = (int) (size.height * anchorPercentage);
    int startPoint = (int) (size.width * startPercentage);
    int endPoint = (int) (size.width * finalPercentage);
    new TouchAction(driver).press(startPoint, anchor).waitAction(duration).moveTo(endPoint, anchor).release().perform();

    //In documentation they mention moveTo coordinates are relative to initial ones, but thats not happening. When it does we need to use the function below
    //new TouchAction(driver).press(startPoint, anchor).waitAction(duration).moveTo(endPoint-startPoint,0).release().perform();
}

And calling it with the value from your topic would be:

swipeHorizontal(driver,0.9,0.01,0.5,3000);

Notice the comments about absolute/relative values

3 Likes

and i suggest to get coordinates directly from element you trying to scroll. not the whole screen.

Thanks a lot @Telmo_Cardoso, It’s working cool :slight_smile:

Thanks @Aleksei for your reply.

@Telmo_Cardoso could you please tell me about Page Scroll up and Page Scroll Down by same format, it would be more helpful for me. Thanks in advance.

As you said, it is the same format… You just exchange height for width

public static void swipeVertical(AppiumDriver driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
    Dimension size = driver.manage().window().getSize();
    int anchor = (int) (size.width * anchorPercentage);
    int startPoint = (int) (size.height * startPercentage);
    int endPoint = (int) (size.height * finalPercentage);
    new TouchAction(driver).press(anchor, startPoint).waitAction(duration).moveTo(anchor, endPoint).release().perform();

    //In documentation they mention moveTo coordinates are relative to initial ones, but thats not happening. When it does we need to use the function below
    //new TouchAction(driver).press(anchor, startPoint).waitAction(duration).moveTo(0,endPoint-startPoint).release().perform();
}

swipeVertical(driver,0.9,0.1,0.5,3000);
2 Likes

Working fine, Thanks :slight_smile:

@Telmo_Cardoso, One more question please, How I can verify toast message ? I’m using java-client 4.1.2

Never tried. I think it works only with automator2. But check this test testToastMSGIsDisplayed() in

@Telmo_Cardoso, Could you please tell me how to hide the android keyboard.

I have tried this one: driver.hideKeyboard() but it’s not working.

Currently I’m using: driver.navigate().back(); but I think it’s not the proper way.

could you please tell me the better way i’m using java-client 4.1.2

I’m waiting for your ans please @Telmo_Cardoso

New “issues” should be open in new topics. What happens when you run driver.hideKeyboard() ? Post the logs in a new topic. Thanks

1 Like

Thanks a lot, ok I will create a new issue.

@Telmo_Cardoso in my case, I need to swipe a row to left to view hidden button on right side of it.

I tried your solution but instead of press event, I see tap event happening on row.

I’m not sure what may be going wrong, even I tried longPress instead of press but no luck yet.

Can you please refer to below method and review if it has some issue ?

WebElement rowToSlide = driver.findElement(By.xpath("//XCUIElementTypeApplication[1]//XCUIElementTypeTable[1]/XCUIElementTypeCell[1]"));
Dimension size = rowToSlide.getSize();

            	TouchAction swipe = new TouchAction(driver).press(rowToSlide, size.width / 2 + 50 , size.height / 2).waitAction(1000)
            	            .moveTo(rowToSlide, 100, size.height / 2).release().perform();

@Telmo_Cardoso, I have created a new issue, could you please ans my question:

Hi @Telmo_Cardoso, i tried this code for Swipe Up, After running, the TestNG test result indicates passed but on the actual device is not swiped. Please Help me. Thanks

1 Like

Looping @Al_Imran for this… How did you fixed yours? Thanks