Skip to main content

๐Ÿšจ Error handling

Happy path testing is fantastic, but error scenarios separate amateur from professional testing! Users will inevitably:

  • ๐Ÿ“ Submit empty forms
  • ๐Ÿ”„ Retry failed actions
  • ๐Ÿšซ Encounter validation errors
  • ๐Ÿ˜… Make unexpected inputs

๐Ÿ”‘ Keysโ€‹

Let's add keys:

lib/app_keys.dart
abstract class AppKeys {
// previous keys
static const errorDialog = ValueKey('ErrorDialog');
static const errorDialogOKButton = ValueKey('ErrorDialogOKButton');
}

... and assign them to widgets:

lib/main.dart
showDialog(
context: context,
builder: (context) => AlertDialog(
key: AppKeys.errorDialog, // ๐Ÿ‘ˆ here
title: Text('No name'),
content: Text('Please enter a name.'),
actions: [
TextButton(
key: AppKeys.errorDialogOKButton, // ๐Ÿ‘ˆ and here
onPressed: () => Navigator.of(context).pop(),
child: Text('OK'),
),
],
),
);

๐Ÿงช Update testโ€‹

Now let's test the error scenario - what happens when users submit empty form? Let's clear the name field, submit it, and handle the error dialog that appears.

test/e2e_test.dart
// previous steps

await tt.type(AppKeys.nameField, '');
await tt.tap(AppKeys.submitButton);
await tt.exists(AppKeys.errorDialog);
await tt.tap(AppKeys.errorDialogOKButton);
await tt.absent(AppKeys.errorDialog);

๐Ÿ’ก Pro Tips: The type action replaces existing text. The absent assertion is optional but excellent practice - it explicitly verifies the dialog actually disappeared, making your tests more reliable and self-documenting!

Run the test flutter test test and you should see the following output:

My E2E Widget test
...
โœ… Typed "" into NameField
โœ… Tapped SubmitButton
โœ… ErrorDialog exists
โœ… Tapped ErrorDialogOKButton
โœ… ErrorDialog is absent
00:01 +1: All tests passed!

๐ŸŽ‰ Achievement Unlocked!โ€‹

You've mastered error scenario testing! Your test now handles form validation, dialog interactions, and error recovery - all the messy edge cases that separate professional apps from amateur ones. And notice that even as our E2E test gets more and more advanced, it still completes under 1 second! ๐Ÿš€

๐Ÿ“š Next stepsโ€‹