简体   繁体   English

Flutter:如何在测试中访问/检查小部件的属性?

[英]Flutter: How to access / inspect properties of a widget in tests?

I want to test properties of some widgets but I don't find a simple way of doing it.我想测试一些小部件的属性,但我没有找到一种简单的方法。

Here's a simple example with a password field, how can I check that obscureText is set to true?这是一个带有密码字段的简单示例,我如何检查 obscureText 是否设置为 true?


import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

const darkBlue = Color.fromARGB(255, 18, 32, 47);

Future<void> main() async {
  testWidgets('Wheelio logo appear on the login screen',
      (WidgetTester tester) async {
    final Key _formKey = GlobalKey<FormState>();
    final TextEditingController passwordController = TextEditingController();
    const Key _passwordKey = Key('PASSWORD_KEY');
    final Finder passwordField = find.byKey(_passwordKey);
    await tester.pumpWidget(MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: Form(
            key: _formKey,
            child: TextFormField(
              key: _passwordKey,
              obscureText: true,
              controller: passwordController,
            ),
          ),
        ),
      ),
    ));

    await tester.pump();
    expect(passwordField, findsOneWidget);
    final TextFormField myPasswordWidget =
        tester.widget(passwordField) as TextFormField;

    //    How can I check that obscureText property is set to true ?
  });
}

You can use tester and find to obtain anything in the widget tree.您可以使用testerfind来获取小部件树中的任何内容。

For example, if you want to test that a Text has the property textAlign set to TextAlign.center , you can do:例如,如果要测试Text的属性textAlign设置为TextAlign.center ,则可以执行以下操作:

expect(
  tester.widget(find.byType(Text)),
  isA<Text>().having((t) => t.textAlign, 'textAlign', TextAlign.center),
);

CommonFinders byWidgetPredicate method CommonFinders byWidgetPredicate 方法

final _passwordKey = GlobalKey(debugLabel: 'PASSWORD_KEY');

await tester.pumpWidget(
 // ...
);
bool isObscureTextTrue(TextFormField widget) {
  final TextField textField = widget.builder(_passwordKey.currentState);
  return textField.obscureText;
}

final finder = find.byWidgetPredicate(
  (widget) => widget is TextFormField && isObscureTextTrue(widget),
);

expect(finder, findsOneWidget);

您可以通过 Finder 获取小部件。

TabBar tabBar = find.byType(TabBar).evaluate().single.widget as TabBar
final passwordField = find.byKey(_passwordKey);
final input = tester.firstWidget<TextFormField>(passwordField);

The input will be your widget, so you can now check input将是您的小部件,因此您现在可以检查

expect(input.obscureText, true);

Here is an example to check that only 3 Text widgets have the property textAlign set to TextAlign.center :下面是一个示例,用于检查是否只有 3 个文本小部件将属性textAlign设置为TextAlign.center

final finderText = find.byType(Text);
final textWidgets = tester.widgetList<Text>(finderText);
expect(
  textWidgets
    .where((textWidget) => textWidget.textAlign == TextAlign.center)
      .length,
  3,
);

This could work with any widget, even your own widgets.这适用于任何小部件,甚至是您自己的小部件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM