How to perform scroll/swipe top to bottom


#21

To do this you must know resource id or cont-desc of scrollable element. You also need to know className of your scrollable element.

If you have cont-desc in scrollable list

try {
    String scrollableList="your con-desc of scrollable List";
    String elementClassName="android.something.something";
    String anyText="any text";

    driver.findElement(MobileBy.AndroidUIAutomator(
                    "new UiScrollable(new UiSelector().description(\"" + scrollableList + "\")).getChildByText("
                            + "new UiSelector().className(\"" + elementClassName + "\"), \"" + anytext + "\")"));
    }catch (Exception e){
            System.out.println("Cannot scroll further");
}

If you have resource-id in scrollable list

try {
    String scrollableList="your con-desc of scrollable List";
    String elementClassName="android.something.something";
    String anyText="any text";

    driver.findElement(MobileBy.AndroidUIAutomator(
                    "new UiScrollable(new UiSelector().resourceId(\"" + scrollableList + "\")).getChildByText("
                            + "new UiSelector().className(\"" + elementClassName + "\"), \"" + anytext + "\")"));
 }catch (Exception e){
            System.out.println("Cannot scroll further");
}

If the screen cannot be scroll further it will throw error which will be catched by catch block.


#22

Hello guyz,

I was also stuck in scroll page issue today in my appium automation. i am using robot framework with appium library and running my automation real device using adb service.

i first tried using the appiumlibrary keywords Scroll · Scroll Down · Scroll Up but was getting error as

" WebDriverException: Message: Unknown mobile command “scroll”. Only shell,startLogsBroadcast,stopLogsBroadcast commands are supported. "

then i tried implementing custom keywords using scroll methods via java & python code also but got the same error as above.

i even tried to use scroll up/down keywords of Androidlibrary of robot framework but it did’nt worked due import error as below:

Importing test library ‘AndroidLibrary’ failed: ImportError: cannot import name GLOBAL_VARIABLES

then finally i came up with 2 ways to scroll element as below:

  1. def scroll_page_to_text(self,text):
    driver = self.get_appium_webdriver_instance()
    driver.implicitly_wait(5000)
    element = driver.find_element_by_android_uiautomator(‘new UiScrollable(new UiSelector().scrollable(true).instance(0)).getChildByText(new UiSelector().className(“android.widget.TextView”), "’
    + text + ‘")’)

    this i found on stackoverflow and worked for me

  2. if for some reasons the above did’nt work then use this alternative way of drag drop element. this is not good way but yes you’ll not be blocked atleast till you find a better way.

        def scroll_page_down(self,source_element_locator,destn_element_locator):
          	driver = self.get_appium_webdriver_instance()
             driver.implicitly_wait(5000)
            source_element=driver.find_element_by_xpath(source_element_locator)
            destn_element=driver.find_element_by_class_name(destn_element_locator)
            driver.drag_and_drop(source_element,destn_element)
    

this will work even if yours elements are not actually draggable/droppable. just make sure to pass locator of lowermost element of current screen as source_element_locator, then any element locator which is on top of screen as destn_element_locator. driver.drag_and_drop method will try to drag source_element to destn_element and it will automatically scroll your screen.


#23

this of my code is scrolling down but how can i make it to scroll up also?

public static boolean scrollToElement (By by) throws Exception {
boolean isFoundTheElement = driver.findElements(by).size() > 0;
while (isFoundTheElement == false) {
swipeVertical(0.8, 0.1, 0.5, 2000);
isFoundTheElement = driver.findElements(by).size() > 0;
}

	  return isFoundTheElement;
	}

	public static void swipeVertical (
	  double startPercentage, double finalPercentage, double anchorPercentage, int duration)
	  throws Exception {
	  org.openqa.selenium.Dimension size = driver.manage().window().getSize();
	  int anchor = (int) (size.width * anchorPercentage);
	  int startPoint = (int) (size.height * startPercentage);
	  int endPoint = (int) (size.height * finalPercentage);
	  getTouchAction().press(PointOption.point(anchor, startPoint))
	  .waitAction(WaitOptions.waitOptions(Duration.ofMillis(duration)))
	  .moveTo(PointOption.point(anchor, endPoint)).release().perform();
	}

	public static TouchAction getTouchAction () {
	  return new TouchAction(driver);
	}

#24

@Sarah_Paul
You just need to change the starting point of Y coordinates
here’s a snippet from my code
case “up”:
try {
new TouchAction<>(driver).press(point(startVerticalX, startVerticalY))
.waitAction(waitOptions(ofSeconds(3))).moveTo(point(startVerticalX, endVerticalY)).release()
.perform();

		} catch (Exception e) {
			log.info(swipeError + e);
		}
		break;
	case "down":
		try {
				new TouchAction<>(driver).press(point(startVerticalX, endVerticalY))
						.waitAction(waitOptions(ofSeconds(3))).moveTo(point(startVerticalX, startVerticalY))
						.release().perform();
			
		} catch (Exception e) {
			log.info(swipeError + e);
		}
		break;

#25

i have used this co-ordinates but its still not working properly : scrollUp(0.6, 0.9, 0.5, 2000);


#26

Try to calculate the coordinates in a different way.
I’m doing this calculation and work great on different devices with different screen sizes.

// calculate coordinates for vertical swipe
	int startVerticalY = (int) (size.height * 0.8);
	int endVerticalY = (int) (size.height * 0.21);
	int startVerticalX = (int) (size.width / 2.1);
	// calculate coordinates for horizontal swipe
	int startHorizontalX = (int) (size.width * 0.9);
	int endHorizontalX = (int) (size.width * 0.1);
	int startHorizontalY = (int) (size.height / 2.1);

#27

In the result of this code i got these values 947-248-342,
648-72-563. So i changed the code to this but it is still not working. Can you fixing it?
public static void scrollUp (
double startPercentage, double finalPercentage, double anchorPercentage, int duration)
throws Exception {
org.openqa.selenium.Dimension size = driver.manage().window().getSize();
int anchor = (int) (size.width * anchorPercentage);
int startPoint = (int)248; //(size.height * startPercentage);
int endPoint = (int)947; //(size.height * finalPercentage);
getTouchAction().press(PointOption.point(anchor, startPoint))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(duration)))
.moveTo(PointOption.point(anchor, endPoint)).release().perform();
}


#28

Try to use this method without modifying it:

public void swipeDown(int howManySwipes) {
	Dimension size = driver.manage().window().getSize();
	// calculate coordinates for vertical swipe
	int startVerticalY = (int) (size.height * 0.8);
	int endVerticalY = (int) (size.height * 0.21);
	int startVerticalX = (int) (size.width / 2.1);
			try {
				for (int i = 1; i <= howManySwipes; i++) {
				new TouchAction<>(driver).press(point(startVerticalX, endVerticalY))
						.waitAction(waitOptions(ofSeconds(2))).moveTo(point(startVerticalX, startVerticalY))
						.release().perform();
			}
		} catch (Exception e) {
			    				//print error or something
		}
		}

Replace the try/catch block with this if you want to swipe Up:

          try {
	      for (int i = 1; i <= howManySwipes; i++) {
					new TouchAction<>(driver).press(point(startVerticalX, startVerticalY))
							.waitAction(waitOptions(ofSeconds(2))).moveTo(point(startVerticalX, endVerticalY)).release()
							.perform();
				}
			} catch (Exception e) {
				//print error or something
			}

#29

I was getting some errors on “point”, “ofSeconds” and “waitOptions” so i have changed the code to this: and im calling it by swipeDown(1);
** scrollUp(1);**. But its not working so can you help?

public static void swipeDown(int howManySwipes) {
org.openqa.selenium.Dimension size = driver.manage().window().getSize();
// calculate coordinates for vertical swipe
int startVerticalY = (int) (size.height * 0.8);
int endVerticalY = (int) (size.height * 0.21);
int startVerticalX = (int) (size.width / 2.1);
try {
for (int i = 1; i <= howManySwipes; i++) {
new TouchAction<>(driver).press(point(startVerticalX, endVerticalY))
.waitAction(waitOptions(ofSeconds(2))).moveTo(point(startVerticalX, startVerticalY))
.release().perform();
}
} catch (Exception e) {
//print error or something
}
}

		public static void scrollUp(int howManySwipes) {
			org.openqa.selenium.Dimension size = driver.manage().window().getSize();
			// calculate coordinates for vertical swipe
			int startVerticalY = (int) (size.height * 0.8);
			int endVerticalY = (int) (size.height * 0.21);
			int startVerticalX = (int) (size.width / 2.1);
			  try {
			      for (int i = 1; i <= howManySwipes; i++) {
							new TouchAction<>(driver).press(point(startVerticalX, startVerticalY))
									.waitAction(waitOptions(ofSeconds(2))).moveTo(point(startVerticalX, endVerticalY)).release()
									.perform();
						}
					} catch (Exception e) {
						//print error or something
					}
		}


		private static WaitOptions waitOptions(Object ofSeconds) {
			// TODO Auto-generated method stub
			return null;
		}


		private static Object ofSeconds(int i) {
			// TODO Auto-generated method stub
			return null;
		}


		private static PointOption point(int startVerticalX, int startVerticalY) {
			// TODO Auto-generated method stub
			return null;
		}


		
		}

Android - Is there a single method to Scroll and validate the required set of elements are available
#30

Ok, in order to help you, first of all, what version of java client and appium do you have?

The errors for “point”, “ofSeconds” and “waitOptions” might be because you need to have latest versions 6.1.0 java and 1.9.1 Appium. Should be very easy solve with proper imports.

Second, what do you mean by not working? You got errors when the swipe starts? Or nothing happens when the swipe is performed?
From developer mode on your device activate the show tap and pointer location to see exactly if anything happens on the screen when swipe is performed


#31

I get no errors at all while running it but nothing happens when the application opens. and secondly Im new to all this so can you tell me how to check the appium and java versions?


#32

appium -v in cmd and java version in pom.xml file.
Just search for this in the forum there are topics already with these issue.
Follow a tutorial first


#33

How did you identify the x and Y co-ordinates?