Testing in Flutter

Sep 19 2023 · Dart 2.19.3, Flutter 3.7.6, Android Studio 2021.3.1, Visual Studio Code 1.7.4

Part 3: Write Widget Tests

11. Test Different Login Scenarios

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 10. Find & Pump Widget Next episode: 12. Mock Quotes Data & Override Riverpod Dependencies

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the last episode, we checked if all the widgets are present on the login screen. Now test lets test the login screen with different scenarios. The first scenario is when the user enters invalid credentials.

await tester.enterText(emailTextField, 'abcd');
await tester.enterText(passwordTextField, '1234');

await tester.tap(loginButton);

await tester.pumpAndSettle();
var emailErrorText = find.text(kEmailErrorText);
var passwordErrorText = find.text(kPasswordErrorText);

expect(emailErrorText, findsOneWidget);
expect(passwordErrorText, findsOneWidget);
await tester.enterText(emailTextField, 'abcd@mail.com');
await tester.enterText(passwordTextField, 'abcd1234');

await tester.tap(loginButton);
await tester.pumpAndSettle();
emailErrorText = find.text(kEmailErrorText);
passwordErrorText = find.text(kPasswordErrorText);

expect(emailErrorText, findsNothing);
expect(passwordErrorText, findsNothing);
await tester.enterText(emailTextField, 'abcd@mail.com');
await tester.enterText(passwordTextField, 'abcd1234');

await tester.tap(loginButton);
await tester.pumpAndSettle();
expect(find.byKey(loginCircularProgressKey), findsOneWidget);


  await tester.enterText(emailTextField, 'abcd@mail.com');
  await tester.enterText(passwordTextField, 'abcd1234');
  await doGolden('Login-Page', 'Valid Credentials', testCaseScreenInfo);

  await tester.tap(loginButton);
  await tester.pump(const Duration(seconds: 1));
  expect(find.byKey(loginCircularProgressKey), findsOneWidget);

  await tester.pump(Duration(seconds: 1));
  expect(find.byKey(loginCircularProgressKey), findsNothing);