How to count all entries of recyclerview list in android app?

In the app I am automating using appium+node.js, the screen with Offers has recycler view for offer list. Each cell is an image without any text on it.

I am getting the total count of offers through API call and want to assert that count by counting the total list entries.

Currently I am only able to get count of elements displayed on the screen(i.e 3 in this case).
How can I count each entry of this section and match that count with the total count of api result?

you can’t. you can see only what is visible. otherwise you can scroll and count.

While scrolling the list How would I know when to stop as there are no last element to be identified specifically.
How to scroll till the end of the list.

Does the index for each item in list get changed??

No, the index remains same for each item. Only the index of FrameLayout varies from 0 to 2 i.e the total items displayed on the screen at a time.

So from there, you can get the total count by counting a number of times
you got index as 2. Scroll till the end of the screen and count the
occurrences of 2.

But how will I know I have reached the end of the screen?
Presently I am using below code to scroll 3 elements at a time:

exports.swipeNew = function (driver) {
var action = new wd.TouchAction(driver);
return action
.press({x: 17, y:1500 })
.wait(1000)
.moveTo({x: 17, y: 254})
 .release()
.perform();
};

you just need to remember content of first or last visible element and compare it after scroll.

In this scenario, there is no unique content to differentiate each element. They are just images.
Is there any unique attribute I can get by using element.getAttribute() ?

as workaround just ask DEV to set element count into some attribute.

The DEV has done the required changes and has added a unique id to the content-desc.
Now I am having issues in scrolling/swiping the list till the condition is satisfied.

Can you please look into the code i have posted and tell me what am I doing wrong:-

@shashikumarraja i am not nice js coder. for example do not get why in your while first action is RETURN.
mine code in java consist like (code has gramma errors. written from memory as is.):

int loopBreaker = 0;
String newText = null;
String oldText = null;
oldText = someElementList.get(0).getAttribute("content-descr");
int elementCounter = 0;
do {
    //do swipe
    newText = someElementList.get(0).getAttribute("content-descr");
   if (newText.equals(oldText)) {
       loopBraker++;
   } else {
      loopBraker = 0;
      oldText = newText;
   }
} while (newText.equals(oldText) && loopBraker > 1); // we do try 2 times swipe when we reach end for sure

// swipe end. now read last value of 'content-descr' of last element
elementCounter = Integer.parse(someElementList.get(someElementList.size()-1).getAttribute("content-descr"))

@Aleksei Thanks for your help. I was able to figure it out. The problem was indeed of RETURN.
Instead of while loop I chose the recursive implementation as while loop is not recommended for async function calls in javascript.

let swipeAndSaveElement = async function (driver, offersDisplayedInApp, offersCountFromApi) {
let els = await driver.elementsById(offersBanner);
if (els.length > 0) {
var repeatedIdCount = 0
for (let el of els) {
  let offerId = await el.getAttribute('contentDescription');
  if (offersDisplayedInApp.indexOf(offerId) === -1) {
    offersDisplayedInApp.push(offerId);
  } else {
    repeatedIdCount++;
    console.log('offerId already exists in the array'.green);
  }
}
if (offersDisplayedInApp.length <= offersCountFromApi && repeatedIdCount < 3) {
  await swipeNew(driver);
  await swipeAndSaveElement(driver, offersDisplayedInApp, offersCountFromApi);
}
else if (offersDisplayedInApp.length === offersCountFromApi) {
  console.log('OffersId count matched'.green);
  return true;
  } else {
  throw "offersId count didn't match with the offersCountFromApi";
  return false;
}
} else {
console.log('offerIds not found'.red);
return false;
}
}

Hope it can help every1, you just have to find the last element in list!

public static int index=0;
ArrayList<String> generalArray = new ArrayList<String>();
String a = "last element in the list"; 
while (!found_result) {
            List<WebElement> ele = driver.findElementsByXPath("locator of your list");
            int size = 0;
            size = size + ele.size();
            for (int i = 0; i < size; i++) {
                String s = ele.get(i).getText();
                if (s.equals(a)) {
                    found_result = true;
                    System.out.print("index = " + (index+1));
                    break;
                } else {
                    if (!generalArray.contains(s)) {
                        index++;
                        generalArray.add(s);
                      }
                  }
              }
             if (!found_result) {
                  scrollEvent(); //this's my function, scroll to find invisible elements 
                  Thread.sleep(1000);
            }
        }
1 Like