Scroll

One of the most commonly used gestures on mobiles is scroll. Earlier, there were two methods available for scrolling: scrollTo(String text) and ScrollToExact(String text). However, recently, both functions have been deprecated. To solve this, we can use swipe to perform the scroll functions and pass in parameters that are based on the relative height and width of the screen.

To scroll down, use the following code snippet, where we have fixed the x component and moved the y component:

public void scrollDown() {
int height = driver.manage().window().getSize().getHeight();
androidDriver.swipe(5, height * 2 / 3, 5, height / 3, 1000);
}

To scroll up, use this code snippet where we have fixed the x component and moved the y component:

public void scrollUp() {
int
height = driver.manage().window().getSize().getHeight();
androidDriver.swipe(5, height / 3, 5, height * 2 / 3, 1000);
}

Once we have the preceding implementation, we can implement scroll down to an element using the following code. Change the System.out to the appropriate assertion.

public void scrollDownTo(By locatorOfElement) {
int i = 0;
while (i < 12) {
if (driver.findElements(locatorOfElement).size() > 0)
return;
scrollDown();
i++;
}
System.out.println("Couldn't find element: " + locatorOfElement.toString());
}

We can even implement a scroll down to text on similar implementations; refer to this code snippet:

public void scrollDownTo(String text) {
By locatorOfElement = By.xpath("//*[@text="" + text + ""]");
androidDriver.hideKeyboard();
int i = 0;
while (i < 12) {
if (androidDriver.findElements(locatorOfElement).size() > 0)
return;
scrollDown();
i++;
}
System.out.println("Couldn't find text : " + locatorOfElement.toString());
}

The preceding code can be used for reference and for developing your own customized scroll method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.198.59