简体   繁体   English

Flutter 测试:如何定位(特定)小部件

[英]Flutter Testing: How to target (specific) widgets

I am testing an application created by the mobile apps division.我正在测试由移动应用程序部门创建的应用程序。

Hardly any of the widgets have any keys or labels to distinguish themselves from each other.几乎没有任何小部件有任何键或标签来相互区分。

I'm having a hard timing even targeting a single widget, let alone 2 similar widgets on the same page;即使针对单个小部件,我也很难把握时机,更不用说同一页面上的 2 个类似小部件了; example: 2 text field widgets: username, password.示例:2 个文本字段小部件:用户名、密码。

Right now, the only test I have is this:现在,我唯一的测试是:

testWidgets('Empty Login Box', (WidgetTester tester) async {
    app.main();
    
    await tester.pumpAndSettle();
    
    final emailText = find.text("EMAIL");
    expect(emailText, findsOneWidget);
    
    });

And even this doesn't work.即使这样也行不通。 Here's the response:这是回应:

00:40 +0: ...\EndevStudios\MedicalApp\gshDevWork\medical-app-frontend\integration_test\mock_image_upload_test.dart     I00:43 +0: ...\EndevStudios\MedicalApp\gshDevWork\medical-app-frontend\integration_test\mock_image_upload_test.d 2,854ms
00:47 +0: Login Page Tests Empty Login Box
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure was thrown running a test:
Expected: exactly one matching node in the widget tree
  Actual: _TextFinder:<zero widgets with text "EMAIL" (ignoring offstage widgets)>
   Which: means none were found but one was expected

When the exception was thrown, this was the stack:
#4      main.<anonymous closure>.<anonymous closure> (file:///D:/WEBDEV/EndevStudios/MedicalApp/gshDevWork/medical-app-frontend/integration_test/mock_image_upload_test.dart:29:7)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)

This was caught by the test expectation on the following line:
  file:///D:/WEBDEV/EndevStudios/MedicalApp/gshDevWork/medical-app-frontend/integration_test/mock_image_upload_test.dart line 29
The test description was:
  Empty Login Box
════════════════════════════════════════════════════════════════════════════════════════════════════
00:47 +0 -1: Login Page Tests Empty Login Box [E]
  Test failed. See exception logs above.
  The test description was: Empty Login Box

00:48 +0 -1: Some tests failed.

I've been trying to use these CommonFinders class, but I can't seem to utilize them effectively.我一直在尝试使用这些CommonFinders class,但我似乎无法有效地利用它们。

https://docs.flutter.dev/cookbook/testing/widget/finders
https://api.flutter.dev/flutter/flutter_driver/CommonFinders-class.html
https://api.flutter.dev/flutter/flutter_test/CommonFinders-class.html

To anyone who can, please help!对于任何可以的人,请提供帮助!

You need to first pump your widget, if you don't do that, the Finder is not going to find your widget and will error:你需要先启动你的小部件,如果你不这样做,Finder 将不会找到你的小部件并且会出错:

The following TestFailure was thrown running a test: Expected: exactly one matching node in the widget tree Actual: _TextFinder:<zero widgets with text "EMAIL" (ignoring offstage widgets)> Which: means none were found but one was expected运行测试时抛出以下 TestFailure:预期:小部件树中只有一个匹配节点实际:_TextFinder:<零个带有文本“EMAIL”的小部件(忽略后台小部件)>其中:意味着没有找到预期有一个

Try the following code:试试下面的代码:

  testWidgets('Empty Login Box', (WidgetTester tester) async {
    /// Pump your widget first:
    await tester.pumpWidget(const LoginBoxWidget(
      title: 'My Title',
      message: 'Another parameter...',
    )); // Parameters depend on your widget

    final emailText = find.text("EMAIL");
    expect(emailText, findsOneWidget);
  });

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

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