Code to swipe>>save elements on the screen to list>>scroll>>(Repeat till end of list)

I am trying to count the total elements of a recycler view in android appium using wd.js.

For this I am using the logic:
1.For the id get all elements displayed on the screen>>get each elements unique content description and save it to an array.
2.Swipe/scroll the screen >>save the element’s unique attribute to the array>>while total length of the array is not equal to the total count

I have written below code for this:

it("Scroll and save offerid to offersPresent List", function () {
      while(offersPresent.length !== offersCount) {
         return  offersPOM.swipeNew(driver, function (err, res) { }).then(function () {
               driver.elementsById('someId').then(function (els) {
                   els.forEach(function (el) {
                       el.getAttribute('contentDescription').then(function (offerId) {
                          //Here I check if the offerId is already there in the array or not
                           if (offersPresent.indexOf(offerId) === -1) {
                               offersPresent.push(offerId);
                           }
                           console.log('offersPresent'.green, offersPresent);
                       })
                   })

               })
           })
       }
   });

The swipNew function is:

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

The problem is no matter the condition, this loop only runs once.
How to make this loop run till the while condition is satisfied?

What do you initialize offersCount to? What is the initial size of offersPresent?

I take the value of offers count from API call…Mostly 13.
Offers present is initially of size 3

Thanks and Regards
Shashi Kumar Raja

I don’t believe this is an appium problem, just a logical flaw in your code. You log the value of offersePresent at the end of the loop, and I suspect it prints out a number other than 13, so either your comparison is wrong (and you can print out offersCount to verify it is or not), or your program control/flow is incorrect. Sorry I can’t offer more help than this

@willosser I was able to figure it out. It was a callback implementation issue.Its now working.

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";
}
} else {
console.log('offerIds not found'.red);
return false;
}
}
1 Like