I am unable to scroll down to the bottom of any page in an Android app using Appium. Multiple approaches were attempted, including those found on Stack Overflow. (It seems this challenge is common.) However, all attempts failed.
Environment:
- Appium version: 1.6.2
- Appium client: Java (java-client-6.1.0.jar)
- Android versions: 5.1, 6.0.1, 7.1.1
- Java version: jre1.8.0_171
- Selenium version: selenium-java-3.13.0
- App type: Cordova (hybrid); the app is built with Cordova but when running
System.out.println(driver.getContext());
, the result is NATIVE_APP
Please share any alternatives or refinements that can solve this problem. Any helpful suggestions are greatly appreciated!
The following summarizes the various approaches that were attempted:
- Approach: scrollIntoView() (various implementations)
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(resourceId(\"send-to-email-app\"));").click();
- Source: Udemy course
- Error: Does not scroll and NO error
Approach: Try catch > scrollIntoView()
try {
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\"Send\"))");
}catch(Exception e) {
System.out.println("We got an error scrolling!");
}
- Source: https://stackoverflow.com/questions/39658626/how-to-scroll-top-to-botton-using-android-driver
- Error: Partially works: the page scrolls down but not far enough
Approach: Swipe
driver.swipe(0, Startpoint,0,scrollEnd,1000);
- Source: https://stackoverflow.com/questions/39130591/swipe-or-scroll-to-bottom-in-appium-android
- Error: Swipe is deprecated
Approach: touchAction.longPress + moveTo
...touchAction.longPress(fromX, fromY).moveTo(toX, toY).release().perform();
- Source: https://stackoverflow.com/questions/44282417/how-to-scroll-with-appium-1-7-1-using-touchaction
- Error: "driver cannot be resolved"
Approach: touchAction.longPress + moveTo
TouchAction touchAction = new TouchAction(driver);
touchAction.longPress(10, 10).moveTo(x, y).release().perform();
- Source: https://stackoverflow.com/questions/44282417/how-to-scroll-with-appium-1-7-1-using-touchaction
- Error: "The method longPress(LongPressOptions) in the type TouchAction is not applicable for the arguments (int, int)"
Approach: touch.longPress + moveTo
...TouchAction touch = new TouchAction(driver);
touch.longPress(co_ordinates[0], co_ordinates[1]).moveTo(dinates[0], dinates[1]).release().perform();
- Source: https://stackoverflow.com/questions/50304699/how-to-scroll-to-specific-element-inlatest-version-of-appium-using-java
- Error: "takeaway cannot be resolved"
Approach: moveTo + using coordinates
actions.press(startX, startY).waitAction(Duration.ofSeconds(2)).moveTo(endX, endY).release().perform();
- Source: http://www.automationtestinghub.com/appium-scroll-examples/
- Error: The 'press' action is deprecated!
Approach: Using Press Approach to swipe + moveTo
TouchAction().press(el0).moveTo(el1).release();
- Source: https://stackoverflow.com/questions/49004686/scroll-text-area-with-terms-and-conditions-on-hybrid-app-with-appium
- Error: "The method press(AndroidElement) is undefined for the type Object"
Approach: touchAction 'tap' using coordinates
TouchAction touchAction = new TouchAction(driver);
touchAction.tap(PointOption.point(x, y)).perform();
- Source: https://discuss.appium.io/t/scrolling-to-element-in-android-app-using-java/17208
- Error: None; it partially works: the page scrolls down but not far enough!
Approach: UiScrollable > Specify Parent scrollable frame and Child element
MobileElement doeButton = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiScrollable(new UiSelector().text(\"Android scrollbar test\")).getChildByText("
+ "new UiSelector().className(\"android.widget.TextView\"), \"Doe\")"));
- Source: https://stackoverflow.com/a/51003840/9214050
- Error: "Could not parse UiSelector argument: problem using reflection to call this method"
Approach: UiScrollable > Specify Parent scrollable frame and 'scrollIntoView' child element
MobileElement sendEmailButton = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiScrollable(new UiSelector().resourceId(\"content\")).scrollIntoView("
+ "new UiSelector().resourceId(\"add-task-button\"))"));
sendEmailButton.click();
- Source: https://stackoverflow.com/a/51003840/9214050
- Error: Does not scroll and NO error
Approach: Changed “scrollable” attribute for page elements from “false” to “true” using CSS styles
- Source: http://burnignorance.com/html-tips/making-a-smooth-scroll-view-for-android-ios-in-html/
- Error: none, but app won't scroll to bottom of page.
Approach: Touch actions: “mobile: swipe”, “mobile: scroll” (http://appium.io/docs/en/writing-running-appium/touch-actions/)
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap params = new HashMap();
params.put("direction", "up");
js.executeScript("mobile: swipe", params);
- Source: https://stackoverflow.com/questions/32387357/i-cant-make-swipe-gesture-work-for-me-in-appium-using-java#
- Error: "Unknown mobile command "scroll". Only shell,startLogsBroadcast,stopLogsBroadcast commands are supported."
Approach: HashMap + scrollObject.put
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap scrollObject = new HashMap();
scrollObject.put("direction", "down");
js.executeScript("mobile: scroll", scrollObject);
- Source: http://appium.io/docs/en/writing-running-appium/touch-actions/
- Error: "Unknown mobile command "scroll". Only shell,startLogsBroadcast,stopLogsBroadcast commands are supported."
Approach: Adjusted context of app from "native" to "webview" (since it's a Cordova app)
appCapabilities.setCapability("autoWebview", "true");
- Source: https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/caps.md
- Error: "Exception in thread "main" org.openqa.selenium.WebDriverException: It is impossible to create a new session because 'createSession' which takes HttpClient, InputStream and long was not found or it is not accessible."
- Error: "org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: No Chromedriver found that can automate Chrome '58.0.3029'. See https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/web/chromedriver.md for more details. (WARNING: The server did not provide any stacktrace information)"
How do you swipe on Android using Appium?
Press by using x,y coordinates, and duration (in seconds) Horizontal swipe by using the start and end percentage of the screen and an anchor for the height. Vertical swipe by using the start and end percentage of the screen and an anchor for the width. Swipe one element to another element.
As a variation on the options proposed above, here is another way of implementing TouchAction
in combination with press
and moveTo
methods that worked for me. This results in a downward screen swipe. You might need to simply adjust the coordinates to whatever works best on your client:
new TouchAction(driver).press(PointOption.point(550, 640)).waitAction().moveTo(PointOption.point(550, 60)).release().perform();
For additional info, you might check out these references:
- Appium site: http://appium.io/docs/en/writing-running-appium/touch-actions/
- Tutorial: https://www.youtube.com/watch?v=9rHe0r6YsVg