How check if text its obscured on InputField

Issue

Im trying to test if password its obscured, this is how far I go, but seems like flutter can read a text even if it is obscured.

 testWidgets('password must be hidden', (WidgetTester tester) async {
  await tester.pumpWidget(wrapWithMaterialApp(child: page));

  await tester.enterText(find.byKey(Key('pass')), '1234');
  final passFinder = find.text('1234');
  expect(passFinder, findsNothing);

});

the test actually find ‘1234’ but im completely sure its obscured.

Solution

Regardless of obscureText flag, entered text is always stored in memory (this parameter affects only visual representation). In tests we can only check this property as follows:

  testWidgets('TextField', (WidgetTester tester) async {
    await tester.pumpWidget(MyApp());
    final finder = find.byKey(Key('pass'));
    final input = tester.firstWidget<TextField>(finder);
    expect(input.obscureText, true);
  });

Answered By – Spatz

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.