Wednesday, August 3, 2016

Workaround or solution for deprecated scroll and scrollTo methods in Appium



In java-client v4.0.0 - https://github.com/appium/java-client/releases/tag/4.0.0

scrollTo() and scrollToExact() became deprecated. They are going to be removed in the next release.

So I have found solution for scroll method in Android. Following is the working solution:-


Method to scroll:-


HashMap scroll = getScrollStartandEndElements(driver);
By by = By.xpath("//android.widget.TextView[@text="test"]");
for (int i = 0; i < 10; i++) {
    driver.swipe(0, scroll.get("start"), 0, scroll.get("end"), 1000);
    if (isElementPresent(driver, by).size() > 0) {
        waitElementToBeClickableByLocator(driver, by).click();
        elementFoundAndClick = true;
        break;
    }
}
Method to get x,y co-ordinates:-

public static HashMap getScrollStartandEndElements(AppiumDriver driver) {
    Dimension dimensions = driver.manage().window().getSize();
    Double screenHeightStart = dimensions.getHeight() * 0.5;
    HashMap scrollHash = new HashMap<>();
    Integer scrollStart = screenHeightStart.intValue();
    scrollHash.put("start", scrollStart);
    Double screenHeightEnd = dimensions.getHeight() * 0.2;
    Integer scrollEnd = screenHeightEnd.intValue();
    scrollHash.put("end", scrollEnd);

    return scrollHash;

}
Method for element to wait:-
public static WebElement waitElementToBeClickableByLocator(
        WebDriver driver, By by) {
    WebDriverWait wait = new WebDriverWait(driver, TEN_SECONDS);

    return wait.until(ExpectedConditions.elementToBeClickable(by));
}
Method for check element is present:-
public static boolean isElementPresent(AppiumDriver driver, By by) {
    boolean size = driver.findElements(by).size() > 0;
    return size;
}

1 comment: