๐จ 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:
abstract class AppKeys {
// previous keys
static const errorDialog = ValueKey('ErrorDialog');
static const errorDialogOKButton = ValueKey('ErrorDialogOKButton');
}
... and assign them to widgets:
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.
// 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
typeaction replaces existing text. Theabsentassertion 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! ๐