Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
719 views
in Technique[技术] by (71.8m points)

Flutter integration test - bad state, no element

In my integration tests I have to keep doing: 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".

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Reference: the docs


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...