Mar*_*ary 6 testing text dart flutter
我有一段代码可以创建一个文本小部件表,如下所示:
return Table(
defaultColumnWidth: FixedColumnWidth(120.0),
children: <TableRow>[
TableRow(
children: <Widget>[Text('toffee'), Text('potato')],
),
TableRow(
children: <Widget>[Text('cheese'), Text('pie')],
),
],
);
Run Code Online (Sandbox Code Playgroud)
我想测试表中的第一项确实是“toffee”这个词。我设置了我的测试并进入了这一部分:
var firstCell = find
.descendant(
of: find.byType(Table),
matching: find.byType(Text),
)
.evaluate()
.toList()[0].widget;
expect(firstCell, 'toffee');
Run Code Online (Sandbox Code Playgroud)
这绝对不起作用,因为firstCell它是 Widget 类型,它不等于 String toffee。
我只看到一个toString()函数,像这样:
'Text("toffee", inherit: true, color: Color(0xff616161), size: 16.0,
textAlign: left)'
Run Code Online (Sandbox Code Playgroud)
如何提取text属性以获取单词toffee?
现在看来我所能做的就是检查.toString().contains('toffee')哪个不理想。
R\xc3\xa9mi 的示例不太有效 - 它可能在他回答时有效,但此时调用whereType<Text>()将始终返回空,Iterable因为evaluate()返回Iterable<Element>,而不是Iterable<Widget>。但是,您可以Element通过调用来获取 的 Widget .widget,因此以下代码应该可以工作:
Text firstText = find\n .descendant(\n of: find.byType(Table),\n matching: find.byType(Text),\n )\n .evaluate()\n .first\n .widget;\n\nexpect(firstText.data, 'toffee');\nRun Code Online (Sandbox Code Playgroud)\n\nOP 非常接近具有工作代码 - 只有 2 个小问题:
\n\nvar代替Text,变量的类型为WidgetWidget与 a 进行比较String- 这永远不会返回 true - 目的是将 the 的属性Widget与 the进行比较String- 在 a 的情况下Text,String它显示的 是通过调用获得.data的Text编辑:
\n\n现在有用于WidgetTester检索小部件的实用函数:widget(Finder)、widgetList(Finder)和。因此,对于 OP 的用例,您将像这样使用:firstWidget(Finder)allWidgetsfirstWidget
Text firstText = tester.firstWidget(\n find.descendant(\n of: find.byType(Table),\n matching: find.byType(Text),\n ));\n\nexpect(firstText.data, 'toffee');\nRun Code Online (Sandbox Code Playgroud)\n
你可以将你的firstCellto Text.
var firstCell = find
.descendant(
of: find.byType(Table),
matching: find.byType(Text),
)
.evaluate()
.whereType<Text>()
.first;
Run Code Online (Sandbox Code Playgroud)
然后测试 firstCell.data
expect(firstCell.data, 'toffee');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5358 次 |
| 最近记录: |