Flutter find.bySemanticsLabel未找到小部件

时间:2019-07-03 15:19:19

标签: flutter flutter-test

我正在尝试使用flutter_test中的find.bySemanticsLabel方法建立一个单元测试,以测试Flutter中的可访问性。如下面的简单单元测试的代码所示,我希望通过正则表达式“。*”搜索语义标签会找到一个(或多个)元素。但是当我运行它时,我得到了一个例外,即在期望一个元素时找不到任何元素。

https://api.flutter.dev/flutter/flutter_test/CommonFinders/bySemanticsLabel.html

https://flutter.dev/docs/cookbook/testing/widget/finders

import 'package:flutter/material.dart';
import 'package:flutter/semantics.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {

  testWidgets("Testing semantics finder.", (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Container(
          child: Semantics.fromProperties(
            child: Text("Text"),
            properties: SemanticsProperties(label: "hello"),
            excludeSemantics: true,
          ),
        ),
      ),
    );

    // Prints true because the regex matches.
    print(RegExp(".*").hasMatch("hello"));

    final semanticsHandle = tester.ensureSemantics();
    final semanticsFinder = find.bySemanticsLabel(RegExp(".*"));
    final semanticsFinder2 = find.bySemanticsLabel("hello");

    expect(semanticsFinder, findsOneWidget,
        reason: "Finds the semantics widget with \".*\" regex.");
    // expect(semanticsFinder2, findsOneWidget,
    //     reason: "Finds the semantics widget with string equality.");
    semanticsHandle.dispose();
  });
}

运行上面的单元测试,print语句输出“ true”以显示正则表达式匹配项。但是未注释的Expect语句会输出一个未找到匹配项的异常。

The following TestFailure object was thrown running a test:
  Expected: exactly one matching node in the widget tree
  Actual: ?:<zero widgets with element matching predicate (Closure: (Element) => bool) (ignoring
  offstage widgets)>
   Which: means none were found but one was expected
Finds the semantics widget with ".*" regex.

0 个答案:

没有答案