How to end the scroll action using appium driver in jav?

  1. I am writing automation test case for iOS app using appium and java.I need to do scroll action, I have tried with below code for scroll action.
  2. I need to scroll till the particular web element and then i need to assert that particular element.
  3. Below code scroll till the end of the page, after it reached to the end of the page, it tries to scroll down, so it throws error message.
  4. I don’t know how to end the scroll action, if particular web element is present means, I need to end the scroll action.

code:-

while(AppiumHelper.isElementPresent(driver,By.name(“Comment”)) == false){
System.out.println(“1…”);
WebElement element = driver
.findElementByName(“save_button”);
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, String> scrollObjects = new HashMap<String, String>();
scrollObjects.put(“direction”, “down”);
scrollObjects.put(“element”,
((RemoteWebElement) element).getId());

          js.executeScript("mobile: scroll", scrollObjects);
        }

iOS

public static void scrollToElement(IOSDriver driver,String elementName) {
String targetCell = “//UIATableCell[UIAStaticText[@name=”"+elementName+""]]";
WebElement cellWithText = driver.findElement(By.xpath(targetCell));
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put(“element”, ((RemoteWebElement)cellWithText).getId());
driver.executeScript(“mobile: scrollTo”,scrollObject);
}

P.S . How to do scroll action till the particular element is present or not?

@Sargis_Azaryan

When appium jav-client gives lot of methods to scroll

scrollTo()
scrollToExact()
driver.swipe() - now it includes direction also [with loop]
TouchActions - [with loop]

Then is it really required to write such complex code for scroll. ? This is just information I want ??

One more question : What is terminating condition of this method… because if lets say due to some data issue/bug elementName we are scrolling to is missed then will method work or it will throw some exception. ???

public static void scrollToElement(IOSDriver driver,String elementName) {
String targetCell = “//UIATableCell[UIAStaticText[@name=”"+elementName+""]]";
WebElement cellWithText = driver.findElement(By.xpath(targetCell));
HashMap scrollObject = new HashMap();
scrollObject.put(“element”, ((RemoteWebElement)cellWithText).getId());
driver.executeScript(“mobile: scrollTo”,scrollObject);
}