Flutter find.bySemanticsLabel не находит виджет - PullRequest
0 голосов
/ 03 июля 2019

Я пытаюсь настроить модульный тест, который проверяет доступность во Flutter с помощью метода find.bySemanticsLabel в flutter_test.Как показано в коде для простого модульного теста ниже, я ожидаю, что поиск метки семантики с помощью регулярного выражения ". *" Найдет один (или более) элементов.Но когда я запускаю его, я получаю исключение, что ни один элемент не был найден, когда он ожидался.

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», чтобы показать совпадения с регулярным выражением.Но безкомментированный оператор ожидания выводит исключение, которое не нашло совпадений.

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.
...