I want to know a genric method for scrolling in appium java

I know there has been many responses, here is what I use,

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

  return isFoundTheElement;
}

public void swipeVertical (
    double startPercentage, double finalPercentage, double anchorPercentage, int duration)
        throws Exception {
  Dimension size = appiumServer.getDriver().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 TouchAction getTouchAction () {
  return new TouchAction(appiumServer.getDriver());
}

I was continously getting an error on "getDrive. So, i have changed the code a bit. But can you tell me how to call it in the main method?
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);
	}

It is just some methods. Add it to a class (existing or new class). From any method (main() is just a method, in a class), you instantiate the class, then call the method ()

myClass.scrollToElement (by);

its continuously giving me error on (by) when I call it in the main method saying ā€œby cannot be resolved to a variableā€

image

by is a variable, of type By
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/By.html

This is your locator object.

I have already imported import org.openqa.selenium.By; but still its giving me error
image

Really?

Import isnā€™t making a variable.

By by = By.xpath("//someXpath");
scrollToElement(by);

thank you a gazillion times it has worked :slight_smile: but i have some other queries also should I also ask them?

Can you also tell the scroll up method please?

I will leave a scroll up as a exercise for you. Basically, your are just swamping the startPoint and endPoint.

Sure, i will do the scrollUp myself and ask you questions. But can you tell me a generic method method for clicking on menu and then scrolling and clicking on each element of the menu?

all menus are different. But, you would use driver.findElements(by) to get the list. Then, iterate through the list, calling click (or better yet, make a tap(MobileElement) method since this is Appium). However, once you change page, the elements found with driver.findElements(by) are no longer going to be relevant. Will need to get it again, and keep track of your placement.

Iā€™m a beginner right now so can you code that for me? Its hard for me to quickly develop logicā€™s right now. I have made a code also but its not clicking on all menus

I suggest taking a course on the language your using (such as Java)

If you watch www.udemy.com (sign up is free, courses cost), they often have Sales where you could get a complete course for under $12 USD. Find courses, favorite them. Pay for it when your comfortable with the price. I am not part of Udemy in any way, I just take many classes there (work and leisure related). There are many other online training web sites out there. Look around.

Coding Appium isnā€™t just putting in commands, it is just another library in the language your using. You need to understand that language. The more complex the application under test, the better you need to be at programming. In test, we have to determine that the application is correct - that can result in some complex testing code.

Learn programming.

-David Genrich

2 Likes

Ā© Two coffee cups to this gentleman

Thank you so much Sir, I will for sure look into that :slight_smile:

I have tried making this scroll up method but its not working fine, hereā€™s the code
public static boolean scrollToElement (By by) throws Exception {
boolean isFoundTheElement = driver.findElements(by).size() > 0;
while (isFoundTheElement == false) {
scrollDown(0.8, 0.1, 0.5, 2000);
scrollUp(0.1,0.8,0.5, 2000);
//scroll(0.1,0.9,0.5,3000); //scrolldown
//scroll(0.9,0.1,0.5,3000); //scrollup
isFoundTheElement = driver.findElements(by).size() > 0;
}

		  return isFoundTheElement;
		}

		public static void scrollDown (
		  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 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) (size.height * finalPercentage);
				  int endPoint = (int) (size.height * startPercentage);
				  getTouchAction().press(PointOption.point(endPoint, anchor))
				  .waitAction(WaitOptions.waitOptions(Duration.ofMillis(duration)))
				  .moveTo(PointOption.point(anchor,startPoint)).release().perform();
				}