Appium 1.6.3, How can I scroll to particular element in java

Hi,
When I use the below statement, it always search from the top of the screen to search for the matching text.

UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches("" + selector + “”).instance(0))"));

Consider a scenario where there two elements Element1 and Element2 displaying invisible before scrolling and Element1 is having matching text of ‘permission’ and Element2 is having matching text of ‘Submit’.

When I use the command for Element1 as below, it searches from the top of the screen for the text ‘permission’ and if finds the match, it stops there.
UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches("" + “permission”+ “”).instance(0))"));

And now, when I use the command for Element2 as with matching text ‘Submit’, it again scrolls to the top to find the matching for the text ‘Submit’.
UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches("" + “Submit”+ “”).instance(0))"));

When Element1 is already found, shouldn’t the search for ‘Element2’ start from the position after ‘Element1’?
If the command using textMatches is made to work like always searching from the top, is there any alternative best approach to scroll to any particular element?

I already tried the below method also but did not find it effective way:

org.openqa.selenium.Dimension size1 = driver.manage().window().getSize();
int x1 = size1.getWidth() / 2;
int starty1 = (int) (size1.getHeight() * 0.70);
int endy1 = (int) (size1.getHeight() * 0.10);
driver.swipe(x1, starty1, x1, endy1, 3000);

Please let me know your responses.

Hi Every one is any one having knowledge over react native application automation , if yes then please help me

I am using following method which scroll until element present and then return element. Hope this will help.
public AndroidElement ScrollToElement(String by, String using, int miliseconds) {
AndroidElement element = null;
int numberOfTimes = 10;
Dimension size = driver.manage().window().getSize();
int anchor = (int)(size.width / 2);
//Swipe up to scroll down
int startPoint = (int)(size.height - 10);
int endPoint = 10;

for (int i = 0; i < numberOfTimes; i++) {
try {
new TouchAction(driver)
.longPress(point(anchor, startPoint)) //.press(point(anchor, startPoint)) if used press need proper waiting time
//.waitAction(waitOptions(ofMillis(miliseconds)))
.moveTo(point(anchor, endPoint)).release().perform();
element = (AndroidElement) driver.findElement(by, using);
i = numberOfTimes;
} catch (NoSuchElementException ex) {
System.out.println(String.format(“Element not available. Scrolling (%s) times…”, i + 1));
}
}
return element;
}

In your test use as follows:
Example:
String usingWebView = “//android.widget.TextView[@text=‘Spinner’]”;
String by = “xpath”;
MobileActions mbAct = new MobileActions(driver); //This is just a class which has above function code.
AndroidElement webViewElement = mbAct.ScrollToElement(by, usingWebView, 250);
webViewElement.click();

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);
		}

}

Perhaps this can help on iOS:

public void scrollToMobileElement(String elementName, String direction) {

    final int maximumScrolls = 5

    for (int i = 0; i < maximumScrolls; i++) {
        try {
            if (findElementsByPredicateString("label CONTAINS \"" + elementName + "\"").size() > 0)
            // PredicateString & label is the locator strategy that I used. It can be changed to others as needed for your app.
                break;
        } catch (Exception e) {
            e.printStackTrace()
        }
        scroll(direction)
    }
}

private void scroll(String direction) {
    final HashMap<String, String> scrollObject = new HashMap<String, String>()
    scrollObject.put("direction", direction)
    driver.executeScript("mobile:scroll", scrollObject);
}

public List<MobileElement> findElementsByPredicateString(String predicateString) {
    return driver.findElements(MobileBy.iOSNsPredicateString(predicateString))
}