0
  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

  • Add some `pumpAndSettle()` before ensureVisible and maybe `await tester.testTextInput.receiveAction(TextInputAction.done)` to close the keyboard – lsaudon Jun 14 '23 at 06:51
  • @lsaudon getting this : ══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════ The following TestFailure was thrown running a test: Expected: exactly one matching node in the widget tree Actual: _TextFinder: Which: means none were found but one was expected – frank jebaraj Jun 14 '23 at 09:18
  • If your btnLogin is always visible you can use skipOffstage: false or scroll – lsaudon Jun 14 '23 at 09:43
  • 1
    Is it possible to share the form code? – lsaudon Jun 14 '23 at 09:43
  • @lsaudon https://drive.google.com/file/d/1wyemZGSP0rrLyMAAo00q4pdJ0nQf0ItX/view?usp=sharing – frank jebaraj Jun 14 '23 at 09:59

1 Answers1

0

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,
    );
  });
}
lsaudon
  • 1,263
  • 11
  • 24
  • it is working if the text 'Invalid credentials Please check your credentials' is hardcoded i was passing loginCtrl.errormsg.value, to make it dynamic show error based on the response any solution to do so... – frank jebaraj Jun 15 '23 at 04:07
  • I don't think we have the information to answer your problem. Where does the error message come from? If it's coming from a back-end or storage, you probably need to mock it. – lsaudon Jun 15 '23 at 07:05