【问题标题】:Finds Element without key查找没有键的元素
【发布时间】:2021-02-05 17:55:36
【问题描述】:

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

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

  Widget _buildNextButton() {
    return Align(
      alignment: Alignment.bottomRight,
      child: Container(
        child: IconButton(
          icon: Icon(Icons.arrow_forward),
          onPressed: () => _controller.nextPage(),
        ),
      ),
    );
  }

该小部件在扩展 StatefulWidget 的类上的位置。

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

static final arrowKey = find.byValueKey(LoginKey.nextButton);
TestDriverUtil.tap(driver, arrowKey);

【问题讨论】:

    标签: flutter integration-testing flutter-test flutterdriver


    【解决方案1】:
    **In Lib directory dart class for connecting that widget**
    
    class Testing extends StatelessWidget {
    
      Testing();
    
      // This widget is the root of your application.
      Widget build(BuildContext context) {
    
        return MaterialApp(
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: YourClass(), // Next button containing class that need to test
        );
      }
    }
    
    **In Test directory**
    
    testWidgets('Next widget field test', (WidgetTester tester) async {
    
          // Build our app and trigger a frame.
          await tester.pumpWidget(Testing());
          // find Widget
          var buttonFind = find.byIcon(Icons.arrow_forward);
          expect(buttonFind, findsOneWidget);
    
          IconButton iconButton = tester.firstWidget(buttonFind);
          expect(iconButton.color, Colors.blue);
        });
    

    【讨论】:

    • 这也适用于集成测试吗?因为我正在做的是编写集成测试
    • 是的,您也可以进行集成测试。
    • 顺便说一句,我应该在集成测试中使用哪个库?因为flutter驱动库上没有find.byIcon()
    • 来自单元测试flutter_test库而不是flutter驱动库。两者很难合并,但无论如何想要在同一个测试用例上合并业务测试和 UI 测试,这没有意义。
    【解决方案2】:

    我们在 Flutter Driver 中进行了文本和值检查,但如果您没有,您可以随时查看应用程序的层次结构。 我所说的层次结构是什么按钮有修复或特定的父权限?

    让我们在这里举个例子,我们有 Align > Container > IconButton > Icon 小部件层次结构,这对于其他人来说是不正确的,比如可能有 IconButton 但没有 Container 父级。 或 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 Widget 的后代。 我还添加了 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));
    });
    

    要检查多个具有相同 Align 作为父级的 IconButtons,我们需要有一些区别,例如父级应该具有文本视图或其他小部件。

    find.descendant(of: find.ancestor(
                      of: find.byValue("somevalue"),
                      matching: find.byType("CustomWidgetClass")), matching: find.byType("IconButton"), firstMatchOnly: true)
    

    通常我会像上面那样将代码拆分到单独的文件中并检查该小部件。

    但最终你会发现那个小部件的独特之处,然后你就可以继续努力了。

    【讨论】:

    • 我找不到 byValue() 方法,还是你的意思是 byValueKey()?
    • 这对我有用,感谢您的清晰解释,如果您介意在这里看到我的其他问题吗?我被另一个颤振驱动程序困住了stackoverflow.com/questions/64682323/…
    • 肯定会检查的
    猜你喜欢
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多