Widget Testing not working as expected in Flutter

411 Views Asked by At
  testWidgets('Login Widget Testing', (WidgetTester tester) async {
await tester.pumpWidget(makeTestableWidget(Child: loginView));
Finder userNameField = find.byKey(Key('username'));
await tester.enterText(userNameField, 'demo');
Finder passwordField = find.byKey(Key('password'));
await tester.enterText(passwordField, 'welcome');
Finder licenceField = find.byKey(Key('licence'));
await tester.enterText(licenceField, 'demo');

await tester.tap(find.byKey(Key('btnLogin')));
final errorFinder = find.text(
  'Invalid credentials Please check your credentials',
);
print(errorFinder);
expect(errorFinder, findsNothing);

});

onTap api gets call and validate wheather the user exist with above entered credential

  1. if user enters invalid credential expected to get error message 'Invalid credentials Please check your credentials'
  2. if entered valid credential should navigate to another page

Here now even though the credential is invalid it throws zero widgets with text "Invalid credentials Please check your credentials" (ignoring offstage widgets)

expected to have one widget

  1. if user enters invalid credential expected to get error message 'Invalid credentials Please check your credentials'
  2. if entered valid credential should navigate to another page

Here now even though the credential is invalid it throws zero widgets with text "Invalid credentials Please check your credentials" (ignoring offstage widgets)

expected to have one widget

 await tester.pumpWidget(makeTestableWidget(Child: loginView));
    Finder userNameField = find.byKey(Key('username'));
    await tester.enterText(userNameField, 'd'emo);
    Finder passwordField = find.byKey(Key('password'));
    await tester.enterText(passwordField, 'welcome');
    Finder licenceField = find.byKey(Key('licence'));
    await tester.enterText(licenceField, 'demos');

    var foo = find.byKey(Key('btnLogin'));
    await tester.ensureVisible(foo); // <-- here
    await tester.tap(foo);
    await tester.pumpAndSettle();
    final errorFinder = find.text(
      'Invalid credentials Please check your credentials',
    );
    expect(errorFinder, findsOneWidget);

was expecting one widget with following text : 'Invalid credentials Please check your credentials'

Expected: exactly one matching node in the widget tree Actual: _TextFinder:<zero widgets with text "Invalid credentials Please check your credentials" (ignoring offstage widgets)> Which: means none were found but one was expected

//dart code https://drive.google.com/file/d/1wyemZGSP0rrLyMAAo00q4pdJ0nQf0ItX/view?usp=sharing

1

There are 1 best solutions below

2
lsaudon On

I've tried to simplify your form. I didn't use obx. I think your button is not visible. You need to add a await tester.pump(); after await tester.ensureVisible(foo);.

  testWidgets('bad', (tester) async {
    await tester.pumpWidget(const MyApp());
    final userNameField = find.byKey(const Key('username'));
    await tester.enterText(userNameField, 'admin');
    final passwordField = find.byKey(const Key('password'));
    await tester.enterText(passwordField, 'admin');
    final licenceField = find.byKey(const Key('licence'));
    await tester.enterText(licenceField, 'refused');
    final foo = find.byKey(const Key('btnLogin'));
    await tester.ensureVisible(foo);
    await tester.pump(); // Add a pump after ensureVisible
    await tester.tap(foo);
    await tester.pump();
    expect(
      find.text('Invalid credentials Please check your credentials'),
      findsOneWidget,
    );
  });
}