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

发布于 2025-01-11 16:17:25 字数 2243 浏览 0 评论 0原文

我正在测试移动应用部门创建的应用程序。

几乎没有任何小部件有任何键或标签来区分彼此。

即使针对单个小部件,我也很难把握时机,更不用说同一页面上的 2 个类似的小部件了;示例:2 个文本字段小部件:用户名、密码。

现在,我唯一的测试是这个:

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

甚至这个也不起作用。这是回应:

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.

我一直在尝试使用这些 CommonFinders 类,但我似乎无法有效地利用它们。

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

对于任何有能力的人,请帮忙!

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; example: 2 text field widgets: username, password.

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.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁客 2025-01-18 16:17:25

您需要首先泵送您的小部件,如果不这样做,Finder 将无法找到您的小部件并会出错:

运行测试时抛出以下 TestFailure:预期:完全正确
小部件树中的一个匹配节点实际:_TextFinder:<零
带有文本“EMAIL”的小部件(忽略后台小部件)>其中:意味着
没有找到,但预计有一个

尝试以下代码:

  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);
  });

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:

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

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);
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文