[英]Flutter integration test - bad state, no element
In my integration tests I have to keep doing: await tester.pumpAndSettle(const Duration(seconds: [some duration]));
在我的集成测试中,我必须继续做:
await tester.pumpAndSettle(const Duration(seconds: [some duration]));
to wait for the widgets to load to the screen after animation delays, otherwise I get the error: "bad state: no element".在 animation 延迟后等待小部件加载到屏幕上,否则我会收到错误消息:“坏 state:没有元素”。
Is this the best way to handle this issue?这是处理这个问题的最好方法吗? I find it is very flaky now that I am trying to add integration tests to my continuous integration platform.
我发现现在我正在尝试将集成测试添加到我的持续集成平台中,这非常不稳定。 I have had to add more time to the duration to wait which feels quite yuck (race condition) and will also increase the costs of continuous integration due to build times taking longer.
我不得不增加更多的等待时间,这感觉很糟糕(竞争条件),并且由于构建时间更长,也会增加持续集成的成本。 Here is an example of the code:
以下是代码示例:
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'));
Is there a better way to handle the "bad state: no element" error due to delayed animations causing widgets to not appear on the screen until some delay?有没有更好的方法来处理“糟糕的 state:无元素”错误,这是由于动画延迟导致小部件直到一些延迟才出现在屏幕上?
Have you tried without the duration parameter?您是否尝试过不使用持续时间参数?
await tester.pumpAndSettle();
If you have an animating loading spinner and some transitions before your button is shown, then pumpAndSettle
will wait till all those animations have stopped and not a millisecond longer than the default 100ms which isn't a big waste of time.如果您在显示按钮之前有一个动画加载微调器和一些过渡,那么
pumpAndSettle
将等待所有这些动画停止并且不会比默认的 100 毫秒长一毫秒,这不会浪费时间。
I had the same problem and wrote a small replacement for waitFor
:我遇到了同样的问题,并为
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.