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