uiautomator - cannot get ListView to scroll as I validate text in each list item. it just fails when I hit the last item on screen
Asked Answered
R

2

7

I am trying to validate text in each list item in my ListView and scroll down one by one so I can validate each one... but for some reason I cant get it to continue scrolling through the list validating each element. Has anyone had any luck automating this kind of action.Here is the closest I can come to. Please let me know if this isn't enough info:

     UiScrollable scrollableList = new UiScrollable(new UiSelector()
        .resourceId("com.example.app:id/listView")
                .className(android.widget.ListView.class.getName())
                .index(3));

for ( i = 0; i < 100; i++ ) {
    UiObject listItem = scrollableList.getChildByText(new UiSelector()
        .className("android.widget.RelativeLayout")
        .instance(i), "Text Title");

    UiObject textBody = listItem.getFromParent(new UiSelector()
        .resourceId("com.example.app:id/textBody"));

    System.out.println("Validating: " + textBody.getText());

There are only 5 listItems visible on the screen at the time, so it fails when it tries to get to number 6, which it cant because:

a. it wont try to scroll forward to get the next one in the list
b. even if it did scroll forward to get the next one in the list, the instance value will decrease to 5, instead of increasing to 6.

I have tried it without the .instance() method, but in that case it just loops over the first item in the list over and over without progressing to item 2,3,4 ect.

If anyone has come across this issue, I would love feedback.

Rinker answered 3/2, 2015 at 6:13 Comment(0)
C
0

In my case, I just want to get all values from that page. It is the limitation of Android that you can only work with the objects that are currently visible on the screen. Check out this link for Ui Automator with limitations with Mobile Automation - http://www.automationtestinghub.com/more-about-ui-automator-viewer/ Also, I have tried this example also -http://www.qaautomated.com/2016/02/how-to-scroll-in-appium.html. I have also tried this solution but it was not useful for me but you can try this -http://www.automationtestinghub.com/uiselector-android/. I have used below lines of code which solves my problem.

// to print value of visible objects on screen.
List<MobileElement> allTitleElements = ((AndroidDriver<MobileElement>)driver).findElementsByAndroidUIAutomator("new UiSelector().resourceId(\"com.experient.swap:id/editText\")");
System.out.println("Element Count - " + allTitleElements.size());

for(MobileElement element : allTitleElements)
 {
    System.out.println("Text - " + element.getText());
 }
// to click on last element of screen(country in my case) to scroll down page to last object
MobileElement abc = (MobileElement) driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView("
                  +"new UiSelector().text(\"Country\"));"));
          abc.click();
Claudication answered 4/5, 2018 at 10:17 Comment(0)
D
-2

I Have implemented the same functionality in python not by using java.I am writing the logic below,see if it helps you.

while(i<100):
   self.d.press('down')
   print self.d(resourceID='android:id/text1').text

This loop scrolls down upto 100 elements and return each element in dropdown.

Dinger answered 17/6, 2015 at 6:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.