Muhammad Hasan Khan
Muhammad Hasan Khan

Reputation: 33

Is there a way I can find an element in a FlatList using Detox E2E testing

So while testing my application, I am using a data set. Since the data set has many entries, I am using FlatList to display my list. After creating the list, I want to be able to verify and tap on certain list items. Below, I have attached the code which I am trying to use and it does not work for me. If anyone has a better idea to do this, please let me know.

The approach I think should be taken. I can't think of anything else to make the FlatList scroll and find the item I am looking for.

let isNotFound = true;
while (isNotFound) {
   try {
       await waitFor(element(by.id((edit_details.index).toString()))).toBeVisible().withTimeout(2000)
       isNotFound = false;
   } catch (e) {
       await element(by.id('credit_history_list')).swipe('up', 'slow', 0.2)
   }
}
await element(by.id((edit_details.index).toString())).tap();

It does help scroll the list and it keeps scrolling while it actually finds the entry I am looking for. But the .tap() function does not work. Instead, detox moves on with the next line of code.

Is there a better way to do this?

Thanks in advance!

Upvotes: 2

Views: 2616

Answers (1)

d4vidi
d4vidi

Reputation: 2537

This sounds spot-on with Detox' whileElement() API.

I think that what you're looking for is this:

const itemId = (edit_details.index).toString();
const listId = 'credit_history_list';
await waitFor(element(by.id(itemId))).toBeVisible()
        .whileElement(by.id(listId))
        .scroll(100, 'down');

Upvotes: 3

Related Questions