Flutter集成测试-状态不好,没有元素



在我的集成测试中,我必须继续执行:await tester.pumpAndSettle(const Duration(seconds: [some duration]));以等待小部件在动画延迟后加载到屏幕上,否则我会得到错误:"坏状态:没有元素";。

这是处理这个问题的最佳方式吗?我发现现在我正试图将集成测试添加到我的持续集成平台中,这是非常不稳定的。我不得不在等待时间上增加更多的时间,这感觉很糟糕(比赛条件(,而且由于构建时间更长,还会增加持续集成的成本。以下是代码示例:

Future<void> registerUserPasswordButtonShouldBeDisabled() async {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('''registering a new user with non-unique email address disables 
register password button''', (WidgetTester tester) async {
await app.main();
await tester.pumpAndSettle(const Duration(seconds: 5));
final registerButton = find.byKey(const Key('register'));

有没有更好的方法来处理";坏状态:没有元素";由于延迟的动画导致小部件直到延迟才出现在屏幕上而导致的错误?

我遇到了同样的问题,并为waitFor:编写了一个小的替代品

Future<void> waitFor(Finder finder, [timeoutInSeconds = 10]) {
Completer c = Completer();
Timer poll;
Timer timeout;
poll = Timer.periodic(Duration(milliseconds: 500), (_) {
if (finder.evaluate().isNotEmpty) {
poll.cancel();
timeout.cancel();
c.complete();
}
});
timeout = Timer(Duration(seconds: timeoutInSeconds), () {
poll.cancel();
c.completeError('WaitFor timed out for ${finder.description}');
});
return c.future;
}

您尝试过不使用持续时间参数吗?

await tester.pumpAndSettle();

如果在显示按钮之前有一个动画加载微调器和一些转换,那么pumpAndSettle将等待所有这些动画停止,并且不会比默认的100ms长一毫秒,这不会浪费太多时间。

参考:文件

最新更新