Trying to get multiple elements in flutter driver(QA environment)

I have been successfully using commonfinders to get a single element in flutter driver but when it comes to multiple elements which may have the same type, it always throws up an error. I understand this is by design. I would be grateful if someone could suggest a way to get multiple elements and store them in an array/list, So I can access them through their index. A similar functionality would be, in selenium, where it lets you use findElements(...) for multiple elements in contrast to findElement(...) which lets you search a single element.

Upvotes: 6

Views: 3786

Answers (2)

tomrozb
tomrozb

Reputation: 26251

This is how to get first of elements by its type

find.descendant(
      of: find.byValueKey(parentWidgetKey),
      matching: find.byType('CheckBox'),
      firstMatchOnly: true,
);

If you have multiple checkboxes, just assign a key to their parent, get the parent by key, get the checkbox by type and set the firstMatchOnly flag to true

Upvotes: 5

ilikerobots
ilikerobots

Reputation: 787

The finders in flutter_driver are currently quite limited, in contrast to the finders provided by flutter_test. This is a known issue that will presumably be addressed someday: see https://github.com/flutter/flutter/issues/12810

In the meantime, as the ticket suggests, if you can assign a predictable key to your elements (e.g. my-el-01, my-el-02, my-el-03) then you can write a helper (findMyEl(String prefix, int maxEls)) that will try to find all the elements named per that scheme, and return as a list. :/

Upvotes: 2

Related Questions