简体   繁体   English

在 Flutter 中测试期间如何找到 Widget 的 `text` 属性?

[英]How to find the `text` property of a Widget during testing in Flutter?

I have a piece of code that creates a Table of Text widgets, like this:我有一段代码可以创建一个文本小部件表,如下所示:

return Table(
  defaultColumnWidth: FixedColumnWidth(120.0),
  children: <TableRow>[
    TableRow(
      children: <Widget>[Text('toffee'), Text('potato')],
    ),
    TableRow(
      children: <Widget>[Text('cheese'), Text('pie')],
    ),
  ],
);

I want to test that the first item in the Table is indeed the word 'toffee'.我想测试表中的第一项确实是“toffee”这个词。 I set up my test and get to this part:我设置了我的测试并进入了这一部分:

var firstCell = find
      .descendant(
        of: find.byType(Table),
        matching: find.byType(Text),
      )
      .evaluate()
      .toList()[0].widget;

  expect(firstCell, 'toffee');

This definitely doesn't work because firstCell is of type Widget, which is not equal to the String toffee .这绝对行不通,因为firstCell是 Widget 类型,它不等于 String toffee

I only see a toString() function, like this:我只看到一个toString()函数,像这样:

'Text("toffee", inherit: true, color: Color(0xff616161), size: 16.0,
 textAlign: left)'

How do I extract the text property to get the word toffee ?如何提取text属性以获取单词toffee

Right now it seems like all I can do is check that the .toString().contains('toffee') which isn't ideal.现在看来我所能做的就是检查.toString().contains('toffee')是否不理想。

You can cast your firstCell to Text .您可以将firstCellText

var firstCell = find
    .descendant(
      of: find.byType(Table),
      matching: find.byType(Text),
    )
    .evaluate()
    .whereType<Text>()
    .first;

Then test firstCell.data然后测试firstCell.data

expect(firstCell.data, 'toffee');

Rémi's example doesn't quite work - it may have worked at the time he answered, but at this time calling whereType<Text>() will always return an empty Iterable because evaluate() returns Iterable<Element> , not Iterable<Widget> . Rémi 的例子不太有效 - 在他回答时它可能有效,但此时调用whereType<Text>()将始终返回一个空的Iterable因为evaluate()返回Iterable<Element> ,而不是Iterable<Widget> . You can however get an Element 's Widget by calling .widget on it, so the following code should work:但是,您可以通过调用.widget来获取Element的 Widget,因此以下代码应该可以工作:

Text firstText = find
    .descendant(
      of: find.byType(Table),
      matching: find.byType(Text),
    )
    .evaluate()
    .first
    .widget;

expect(firstText.data, 'toffee');

The OP was very close to having working code - there were only 2 minor issues: OP 非常接近具有工作代码 - 只有 2 个小问题:

  • By using var instead of Text , the type of the variable was Widget通过使用var而不是Text ,变量的类型是Widget
  • The Widget was compared to a String - this could never return true - the intent was to compare a property of the Widget to the String - in the case of a Text , the String it displays is obtained by calling .data on the TextWidgetString进行比较 - 这永远不会返回 true - 目的是将Widget的属性与String进行比较 - 在Text的情况下,它显示的String是通过在Text上调用.data获得的

Edit:编辑:

The WidgetTester now has utility functions for retrieving widgets: widget(Finder) , widgetList(Finder) , firstWidget(Finder) and allWidgets . WidgetTester现在具有用于检索小部件的实用函数: widget(Finder)widgetList(Finder)firstWidget(Finder)allWidgets So for the OP's use case you would be using firstWidget like so:因此,对于 OP 的用例,您将像这样使用firstWidget

Text firstText = tester.firstWidget(
    find.descendant(
      of: find.byType(Table),
      matching: find.byType(Text),
    ));

expect(firstText.data, 'toffee');

按文字查找?

expect(find.text('toffee'), findsOneWidget);

You can use short form for any type of widgets:您可以对任何类型的小部件使用简写形式:

expect(
     (find.byType(InAppWebView).evaluate().single.widget as InAppWebView)
          .initialUrl,
          'https://www.google.com/');

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

相关问题 如何在Flutter小部件测试中使用List的find.byType匹配 - How to use find.byType matching of List in Flutter widget testing Flutter - 如何在小部件测试中获取文本小部件 - Flutter - how to get Text widget on widget test Flutter 小部件测试和集成测试 - Flutter Widget Testing and Integration Testing BlocBuilder 小部件的 Flutter 小部件测试 - Flutter Widget Testing for BlocBuilder Widgets 如何在基于CamelSpringTestSupport的测试的测试过程中设置属性值 - How to set property values during testing for CamelSpringTestSupport based tests 我正在为这个项目使用颤振小部件测试,我想测试 TextFormField 验证是否有效但找不到上下文 - i am using flutter widget testing for this project where i want to test whether the TextFormField validation is working but could not find the context 如何在 Flutter 中小部件测试 BoxDecoration - How to Widget Test a BoxDecoration in Flutter Flutter 单元测试 - 无法抽出完整的小部件树 - Flutter unit testing - unable to pump full widget tree 如何在React / Jest / Enzyme浅层渲染测试期间将文本输入到表单中? - How to input text into form during React/Jest/Enzyme Shallow Rendering testing? 如何在小部件测试中等待Future方法? - How to wait for Future method inside widget testing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM