Home > Back-end >  Flutter Test Cannot Find AlertDialog Widget
Flutter Test Cannot Find AlertDialog Widget

Time:04-02

I am currently writing a test to enter user information with miss-matching passwords to see if our AlertDialog widget shows up with the text: "Passwords do not match!"

Here is where my alert dialog is:

if (passwordController.text != reenterController.text) {
    showDialog(
            context: context,
            builder: (context) {
              return const AlertDialog(
                key: Key('signUpADKey'),
                content: Text('Passwords do not match!'),
              );
            },
          );
        }

I will note that this if-statement is located inside of an ElevatedButton onPressed() section.

Furthermore, here is my test:

testWidgets('Miss-matching Passwords Test', (WidgetTester tester) async {
    SignUpPage page = SignUpPage();
    await tester.pumpWidget(makeTestableApp(page));

    await tester.enterText(
        find.byKey(const Key('firstNameTextFormField')), 'Jimmy');
    await tester.enterText(
        find.byKey(const Key('lastNameTextFormField')), 'Lewis');
    await tester.enterText(find.byKey(const Key('shortBioTextFormField')),
        'I go to the University of ABC.');
    await tester.enterText(
        find.byKey(const Key('emailTextFormField')), '[email protected]');
    await tester.enterText(
        find.byKey(const Key('passwordTextFormField')), 'Test1234!');
    await tester.enterText(
        find.byKey(const Key('reEnterTextFormField')), 'Test4321!');
    await tester.pump();

    await tester.tap(find.text('Sign Up'));
    await tester.pump();

    expectLater(find.byKey(const Key('signUpADKey')), findsOneWidget);
  });

CodePudding user response:

You have to add actions for AlertDialog.

AlertDialog(
 title: Text("My title"),
 content: Text("This is my message."),
 actions: [
   okButton,
 ],
);

//Widget
  Widget okButton = TextButton(
    child: Text("OK"),
    onPressed: () { },
  );
  • Related