I was also stuck in scroll page issue today in my appium automation. i am using robot framework with appium library and running my automation real device using adb service.
i first tried using the appiumlibrary keywords Scroll ¡ Scroll Down ¡ Scroll Up but was getting error as
" WebDriverException: Message: Unknown mobile command âscrollâ. Only shell,startLogsBroadcast,stopLogsBroadcast commands are supported. "
then i tried implementing custom keywords using scroll methods via java & python code also but got the same error as above.
i even tried to use scroll up/down keywords of Androidlibrary of robot framework but it didânt worked due import error as below:
Importing test library âAndroidLibraryâ failed: ImportError: cannot import name GLOBAL_VARIABLES
then finally i came up with 2 ways to scroll element as below:
def scroll_page_to_text(self,text):
driver = self.get_appium_webdriver_instance()
driver.implicitly_wait(5000)
element = driver.find_element_by_android_uiautomator(ânew UiScrollable(new UiSelector().scrollable(true).instance(0)).getChildByText(new UiSelector().className(âandroid.widget.TextViewâ), "â
+ text + â")â)
this i found on stackoverflow and worked for me
if for some reasons the above didânt work then use this alternative way of drag drop element. this is not good way but yes youâll not be blocked atleast till you find a better way.
this will work even if yours elements are not actually draggable/droppable. just make sure to pass locator of lowermost element of current screen as source_element_locator, then any element locator which is on top of screen as destn_element_locator. driver.drag_and_drop method will try to drag source_element to destn_element and it will automatically scroll your screen.
@Sarah_Paul
You just need to change the starting point of Y coordinates
hereâs a snippet from my code
case âupâ:
try {
new TouchAction<>(driver).press(point(startVerticalX, startVerticalY))
.waitAction(waitOptions(ofSeconds(3))).moveTo(point(startVerticalX, endVerticalY)).release()
.perform();
In the result of this code i got these values 947-248-342,
648-72-563. So i changed the code to this but it is still not working. Can you fixing it?
public static void scrollUp (
double startPercentage, double finalPercentage, double anchorPercentage, int duration)
throws Exception {
org.openqa.selenium.Dimension size = driver.manage().window().getSize();
int anchor = (int) (size.width * anchorPercentage);
int startPoint = (int)248; //(size.height * startPercentage);
int endPoint = (int)947; //(size.height * finalPercentage);
getTouchAction().press(PointOption.point(anchor, startPoint))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(duration)))
.moveTo(PointOption.point(anchor, endPoint)).release().perform();
}
I was getting some errors on âpointâ, âofSecondsâ and âwaitOptionsâ so i have changed the code to this: and im calling it by swipeDown(1);
** scrollUp(1);**. But its not working so can you help?
public static void swipeDown(int howManySwipes) {
org.openqa.selenium.Dimension size = driver.manage().window().getSize();
// calculate coordinates for vertical swipe
int startVerticalY = (int) (size.height * 0.8);
int endVerticalY = (int) (size.height * 0.21);
int startVerticalX = (int) (size.width / 2.1);
try {
for (int i = 1; i <= howManySwipes; i++) {
new TouchAction<>(driver).press(point(startVerticalX, endVerticalY))
.waitAction(waitOptions(ofSeconds(2))).moveTo(point(startVerticalX, startVerticalY))
.release().perform();
}
} catch (Exception e) {
//print error or something
}
}
public static void scrollUp(int howManySwipes) {
org.openqa.selenium.Dimension size = driver.manage().window().getSize();
// calculate coordinates for vertical swipe
int startVerticalY = (int) (size.height * 0.8);
int endVerticalY = (int) (size.height * 0.21);
int startVerticalX = (int) (size.width / 2.1);
try {
for (int i = 1; i <= howManySwipes; i++) {
new TouchAction<>(driver).press(point(startVerticalX, startVerticalY))
.waitAction(waitOptions(ofSeconds(2))).moveTo(point(startVerticalX, endVerticalY)).release()
.perform();
}
} catch (Exception e) {
//print error or something
}
}
private static WaitOptions waitOptions(Object ofSeconds) {
// TODO Auto-generated method stub
return null;
}
private static Object ofSeconds(int i) {
// TODO Auto-generated method stub
return null;
}
private static PointOption point(int startVerticalX, int startVerticalY) {
// TODO Auto-generated method stub
return null;
}
}
Ok, in order to help you, first of all, what version of java client and appium do you have?
The errors for âpointâ, âofSecondsâ and âwaitOptionsâ might be because you need to have latest versions 6.1.0 java and 1.9.1 Appium. Should be very easy solve with proper imports.
Second, what do you mean by not working? You got errors when the swipe starts? Or nothing happens when the swipe is performed?
From developer mode on your device activate the show tap and pointer location to see exactly if anything happens on the screen when swipe is performed
I get no errors at all while running it but nothing happens when the application opens. and secondly Im new to all this so can you tell me how to check the appium and java versions?
appium -v in cmd and java version in pom.xml file.
Just search for this in the forum there are topics already with these issue.
Follow a tutorial first
how ,can u please explain but for new appium version there is no ready made method like swipe,scroll, so can you tell how to scroll until find that element.