1

If I run this code snippet:

await tester.enterText(
        find.byKey(Key('titleControlKey')), 'Sample Account 1');
await tester.enterText(find.byKey(Key('usernameControlKey')), 'username1');

or this one:

await tester.enterText(
        find.byKey(Key('titleControlKey')), 'Sample Account 1');
await tester.enterText(find.byKey(Key('passwordControlKey')), 'password1');

It both works.

But if I run the three:

await tester.enterText(
        find.byKey(Key('titleControlKey')), 'Sample Account 1');
await tester.enterText(find.byKey(Key('usernameControlKey')), 'username1');
await tester.enterText(find.byKey(Key('passwordControlKey')), 'password1');

The test will fail and produce this error:

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following StateError was thrown running a test:
Bad state: No element

When the exception was thrown, this was the stack:
#0      Iterable.single (dart:core/iterable.dart:654:25)
#1      WidgetController.state (package:flutter_test/src/controller.dart:156:42)
#2      WidgetTester.showKeyboard.<anonymous closure> (package:flutter_test/src/widget_tester.dart:1042:42)
#3      WidgetTester.showKeyboard.<anonymous closure> (package:flutter_test/src/widget_tester.dart:1041:39)
#6      TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:71:41)
#7      WidgetTester.showKeyboard (package:flutter_test/src/widget_tester.dart:1041:27)
#8      WidgetTester.enterText.<anonymous closure> (package:flutter_test/src/widget_tester.dart:1077:13)
#9      WidgetTester.enterText.<anonymous closure> (package:flutter_test/src/widget_tester.dart:1076:39)
#12     TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:71:41)
#13     WidgetTester.enterText (package:flutter_test/src/widget_tester.dart:1076:27)
#14     AddAccountScreenRobot.enterAccountDetails (file:///C:/Users/Noyruto/sources/repos/account_saver/integration_test/robots/add_account_screen_robot.dart:25:18)
<asynchronous suspension>
<asynchronous suspension>
(elided 5 frames from dart:async and package:stack_trace)
noyruto88
  • 767
  • 2
  • 18
  • 40

2 Answers2

0

There must have some settling time between two tester.enterText to work properly. Add these according to your need.

        await tester.pump();
        await tester.pumpAndSettle();
        await tester.pump(Duration(seconds: 3));

Example

await tester.enterText(
    find.byKey(Key('titleControlKey')), 'Sample Account 1');
await tester.pumpAndSettle();
await tester.enterText(find.byKey(Key('usernameControlKey')),'username1');
await tester.pumpAndSettle();
await tester.enterText(find.byKey(Key('passwordControlKey')), 'password1');
await tester.pump(Duration(seconds: 3));
Fahmida
  • 1,050
  • 8
  • 19
0

I just solved my own problem. Because the TextFields are inside ListView widget and when the screen key shows up, other TextField widget is not visible. So what I did was scroll down the form using tester.fling() to make the password TextField visible.

await tester.enterText(
        find.byKey(Key('titleControlKey')), 'Sample Account 1');
await tester.pumpAndSettle();
await tester.enterText(find.byKey(Key('usernameControlKey')), 'username1');
await tester.pumpAndSettle();
await tester.fling(listFinder, const Offset(0, -500), 10000);
await tester.pumpAndSettle();
await tester.enterText(find.byKey(Key('passwordControlKey')), 'password1');
await tester.pump(Duration(seconds: 2));
noyruto88
  • 767
  • 2
  • 18
  • 40