Skip to content

Instantly share code, notes, and snippets.

@asolntsev
Last active July 12, 2023 21:26
Show Gist options
  • Save asolntsev/4693ce0768081880920557b78e6dce15 to your computer and use it in GitHub Desktop.
Save asolntsev/4693ce0768081880920557b78e6dce15 to your computer and use it in GitHub Desktop.
How to scroll in Appium
// Will be added to https://github.com/selenide/selenide-appium soon
// helper class
data class ScrollBy(val uiSelector: String) {
companion object {
fun toId(id: String) = ScrollBy("new UiSelector().resourceIdMatches(\".*id/$id\")")
fun toText(text: String) = ScrollBy("new UiSelector().text(\"${text}\")")
}
}
// helper method in BaseTest
protected fun scroll(selector: ScrollBy): WebElement {
val container = "new UiSelector().scrollable(true)"
val script = "new UiScrollable($container).scrollIntoView(${selector.uiSelector})"
return driver().findElement(MobileBy.AndroidUIAutomator(script))
}
// usage
scroll(toId("account_balance"))
scroll(toText("Hello world")).click()
@asolntsev
Copy link
Author

I don't know. Maybe not.
But if we add method $.scrollTo() to Selenide, it should work both in Android and iOS, right?
We cannot add a method that works only in Android...

@Anton-Ilyin-AEG
Copy link

Sure: just return nothing in method for iOS ><
BTW, for RecycleView component, this code should be little bit modified. So I created two methods:
scroll() and scrollRv().
For RV we need to use:

var container = "new UiSelector().scrollable(true).className(\"androidx.recyclerview.widget.RecyclerView\")";
var script = "new UiScrollable(%s).setAsVerticalList().scrollIntoView(%s)".formatted(container, locator.uiSelector());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment