简体   繁体   English

Flutter - 如何在集成测试中获取小部件计数

[英]Flutter - how to get widget count on integration test

how can i get the widget count我怎样才能得到小部件计数

final SerializableFinder finder = find.byType('MyWidget');
await driver.tap(finder);

but MyWidget may be more than one,但 MyWidget 可能不止一个,
that will throw exception那会抛出异常

If you want to find one or more widgets matching yours, use findsWidgets : 如果要查找一个或多个与您匹配的小部件,请使用findsWidgets

final SerializableFinder finder = find.byType('MyWidget');
expect(finder, findsWidgets);

If you're looking for an exact number of widgets, you can count how many matching nodes are in the widget tree using findsNWidgets() (let's say 4): 如果您要查找小部件的确切数量 ,则可以使用findsNWidgets() (假设4)来计算小部件树中有多少个匹配节点:

final SerializableFinder finder = find.byType('MyWidget');
expect(finder, findsNWidgets(4));

You will need to use some attribute that allows you to find the widget you need.您将需要使用一些属性来让您找到所需的小部件。 For example, add a Key to each of these widgets, so it's easier to find the one you need:例如,为每个小部件添加一个Key ,以便更轻松地找到您需要的小部件:

...MyWidget(key: Key('myWidget1'))...

So, find it by key:所以,通过键找到它:

find.byKey(Key('myWidget1'))

If you want to find the widget count, you can use this:如果要查找小部件计数,可以使用以下命令:

final finder = find.byType(MyWidget, skipOffstage: false); // Without quotes!
final count = tester.widgetList<MyWidget>(finder).length;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM