Appium - Scroll to Element



There are situations where you will need to scroll to a particular element using Appium
Below functions verified working on IOS Mobile Apps

Below code snippet will scroll to an element using the text of that element

/**
* Scroll to a given element by value
* @param value Element value
*/
def static void ScrollToElementByValue(String value) {
var scrollObject = new HashMap();
scrollObject.put("predicateString", "value == '" + value + "'");
scrollObject.put("direction", "down");
driver.executeScript("mobile: scroll", scrollObject);
}


Below code snippet will scroll to an element by its ID

/**
* Scroll to a given element by id
* @param elementID Element id
*/
def static void ScrollToElementById(String elementID) {
var scrollObject = new HashMap<String, String>();
scrollObject.put("element", elementID);
scrollObject.put("toVisible", "not an empty string");
driver.executeScript("mobile:scroll", scrollObject);

}


Below code snippet will scroll to an element by element

/**
* Scroll to a given element by element
* @param element WebElement
*/
def static void ScrollToElementByElement(WebElement element) {
var scrollObject = new HashMap();
scrollObject.put("predicateString", "value == '" + element + "'");
scrollObject.put("direction", "down");
driver.executeScript("mobile:scroll", scrollObject);
}

Powered by Blogger.