Search code examples
javaandroidscrollappiumswipe

Replacement for scrollTo and scrollToExact in java client v4.0


I am writing Mobile App automation test cases for Android/iOS using java and appium.

I have upgraded my appium version from 3.1.0 to 4.0.0. Now I am not able use scrollTo() and scrollToExact()

Java client ReadMe readme says the following:- scrollTo() and scrollToExact() became deprecated. They are going to be removed in the next release.

Anyother method avaialble other than swipe method and like

((MobileElement)element).swipe(SwipeDirection.UP,100);

Does anyone know any possible methods for replacing scrollTo and scrollToExact?


Solution

  • This method is deprecated because it is not consistent and it is going to be removed. It is workaround actually.
    Recommended to use instead:

    AppiumDriver.swipe(int, int, int, int, int)
    MobileElement.swipe(SwipeElementDirection, int)   
    MobileElement.swipe(SwipeElementDirection, int, int, int) 
    

    or search for elements using

    MobileBy.ByAndroidUIAutomator
    

    Specified by:

    scrollTo in interface ScrollsTo<org.openqa.selenium.WebElement>
    

    Parameters:
    text - description or text of an element scroll to

    Returns:
    an element that matches

    Now to user swipe

    public abstract void swipe(int startx, int starty, int endx, int endy, int duration)
    

    Description copied from interface:
    TouchShortcuts
    Convenience method for swiping across the screen.

    Parameters:
    startx - starting x coordinate.
    starty - starting y coordinate.
    endx - ending x coordinate.
    endy - ending y coordinate.
    duration - amount of time in milliseconds for the entire swipe action to take

    Example:

     @Test
     public void swipingVertical() throws InterruptedException {
      //Get the size of screen.
      size = driver.manage().window().getSize();
      System.out.println(size);
    
      //Find swipe start and end point from screen's with and height.
      //Find starty point which is at bottom side of screen.
      int starty = (int) (size.height * 0.80);
      //Find endy point which is at top side of screen.
      int endy = (int) (size.height * 0.20);
      //Find horizontal point where you wants to swipe. It is in middle of screen width.
      int startx = size.width / 2;
      System.out.println("starty = " + starty + " ,endy = " + endy + " , startx = " + startx);
    
      //Swipe from Bottom to Top.
      driver.swipe(startx, starty, startx, endy, 3000);
      Thread.sleep(2000);
      //Swipe from Top to Bottom.
      driver.swipe(startx, endy, startx, starty, 3000);
      Thread.sleep(2000);
     }
    

    This will definitely work for the new version of Java Client 4.0 to execute your Appium Script

    Regards:
    Gaurav Lad