找不到TextField文字数据

时间:2019-08-19 14:18:15

标签: flutter flutter-test

我正在尝试为TextField编写测试,其中我使用IconButton作为TextField的suffixIcon,以便在按下时将切换TextField的{{ 1}}属性。该小部件本身可以正常工作,但是在进行测试时遇到了困难。

代码示例:

password_field.dart

obscureText

password_field_test.dart

import 'package:flutter/material.dart';
import 'package:app/util/app_theme.dart';
import 'package:app/widgets/inputs/text_validator.dart';

class PasswordField extends StatefulWidget {
  final bool showValidator;
  final ValueChanged<String> onChanged;
  final String errorText;

  PasswordField({
    this.errorText,
    @required this.onChanged,
    this.showValidator = true,
  });

  @override
  _PasswordFieldState createState() => _PasswordFieldState();
}

class _PasswordFieldState extends State<PasswordField> {
  String _password;
  bool _shouldHidePassword = true;

  void _onChangedPassword(String password) {
    setState(() {
      _password = password;
    });
    widget.onChanged(password);
  }

  void _togglePasswordVisibility() {
    setState(() {
      _shouldHidePassword = !_shouldHidePassword;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        TextField(
          onChanged: _onChangedPassword,
          decoration: _buildInputDecoration(),
          obscureText: _shouldHidePassword,
        ),
      ],
    );
  }

  InputDecoration _buildInputDecoration() {
    return InputDecoration(
      suffixIcon: IconButton(
        icon: Icon(
          _shouldHidePassword ? CustomIcons.eye_show : CustomIcons.eye_hide),
        onPressed: _togglePasswordVisibility,
      ),
      labelText: 'Password',
    );
  }
}

到目前为止,我已经尝试遵循Flutter小部件测试文档,但是我无法通过testWidgets('should show an eye that obscures/unobscures the typed values', (WidgetTester tester) async { // buildWidget just wraps target in a MediaQuery/Directionality/Material Widget field = buildWidget(PasswordField(onChanged: (_) {})); String testPW = 'passwordtotest'; await tester.pumpWidget(field); expect(find.byType(TextField), findsOneWidget); await tester.enterText(find.byType(TextField), testPW); await tester.pump(); // These are what I've tried so far expect(find.text('•' * testPW.length), findsOneWidget) expect(find.byElementType(TextSpan)) await tester.tap(find.byType(IconButton)); await tester.pump(); expect(find.text(testPW), findsOneWidget); }); });

找到模糊/不模糊的文本

我也没有尝试对基础EditableText和TextSpan使用查找程序,但是即使能够找到它们,我也不知道如何定位文本值。我该怎么办?

0 个答案:

没有答案