Flutter 查找没有键的元素

Flutter 查找没有键的元素,flutter,integration-testing,flutter-test,flutterdriver,Flutter,Integration Testing,Flutter Test,Flutterdriver,在测试中使用颤振驱动程序方面我还是新手,但据我所知,我们可以使用很少的标识符来定位/识别元素,比如通过文本、类型等 但问题是,我想测试的应用程序没有我可以用来定位它们的标识符(如果我错了,请纠正我)。。应用程序的小部件代码如下所示 Widget _buildNextButton() { return Align( alignment: Alignment.bottomRight, child: Container( child: IconBut

在测试中使用颤振驱动程序方面我还是新手,但据我所知,我们可以使用很少的标识符来定位/识别元素,比如通过文本、类型等

但问题是,我想测试的应用程序没有我可以用来定位它们的标识符(如果我错了,请纠正我)。。应用程序的小部件代码如下所示

  Widget _buildNextButton() {
    return Align(
      alignment: Alignment.bottomRight,
      child: Container(
        child: IconButton(
          icon: Icon(Icons.arrow_forward),
          onPressed: () => _controller.nextPage(),
        ),
      ),
    );
  }
其中该小部件位于扩展
StatefulWidget
的类上

如何在测试脚本中找到该图标并单击它?我可以用这样的东西吗?我应该使用什么类型的查找器?(byValueKey?bySemanticLabel?byType?还是什么?)


我们在颤振驱动程序中有文本和值检查,但如果您没有,您可以随时查看应用程序的层次结构。 我所说的层次结构是指按钮具有修复或特定父级权限

让我们举一个例子,我们有Align>Container>IconButton>Icon小部件层次结构,这对于其他小部件来说不是真的,比如可能有IconButton,但对于容器父级来说不是。 或者StreamBuilder或者我们能想到的任何东西

Widget _buildNextButton() {
    return Align(
      alignment: Alignment.bottomRight,
      child: Container(
        child: IconButton(
          icon: Icon(Icons.arrow_forward),
          onPressed: () => print("clicked button"),
        ),
      ),
    );
  }
这种层次结构至少应该是自上而下或自下而上方法的理想选择

现在我所说的自上而下的方法是,Align必须有IconButton,对于自下而上的方法,我们说IconButton必须有Align小部件作为父项

这里我采用了自上而下的方法,所以我从下面的代码中检查的是找到IconButton,他是Align小部件的核心。 此外,我还添加了firstMatchOnly true,因为我正在检查如果两者都出现相同的层次结构会发生什么

test('IconButton find and tap test', () async {
  var findIconButton = find.descendant(of: find.byType("Align"), matching: find.byType("IconButton"), firstMatchOnly: true);
  await driver.waitFor(findIconButton);
  await driver.tap(findIconButton);

  await Future.delayed(Duration(seconds: 3));
});
要检查多个图标按钮是否与父项对齐,我们需要有一些区别,比如父项应该有文本视图或其他小部件

find.genderant(属于:find.祖先)(
of:find.byValue(“somevalue”),
匹配:find.byType(“CustomWidgetClass”)),匹配:find.byType(“IconButton”),firstMatchOnly:true)
通常我会像上面那样将代码拆分成单独的文件,然后检查小部件


但最终你会发现这个小部件的独特之处,然后你就可以使用它了。

这也适用于集成测试吗?因为我的工作是编写集成测试系统,所以您也可以进行集成测试。顺便问一下,我应该在集成测试中使用哪个库?因为在颤振驱动程序库(来自单元测试颤振测试库而非颤振驱动程序库)上没有find.byIcon()。很难将两者合并,但无论如何,谁想在同一个测试用例上合并业务测试和UI测试,这是没有意义的。我找不到byValue()方法,或者你的意思是byValueKey()?这对我很有用,谢谢你的清晰解释,如果你介意的话,你能在这里看到我的其他问题吗?我被另一个颤振驱动程序卡住了,肯定也会检查的
Widget _buildNextButton() {
    return Align(
      alignment: Alignment.bottomRight,
      child: Container(
        child: IconButton(
          icon: Icon(Icons.arrow_forward),
          onPressed: () => print("clicked button"),
        ),
      ),
    );
  }
test('IconButton find and tap test', () async {
  var findIconButton = find.descendant(of: find.byType("Align"), matching: find.byType("IconButton"), firstMatchOnly: true);
  await driver.waitFor(findIconButton);
  await driver.tap(findIconButton);

  await Future.delayed(Duration(seconds: 3));
});