dart 如何在Flutter小部件测试中测试语义属性

gdrx4gfi  于 2024-01-04  发布在  Flutter
关注(0)|答案(1)|浏览(268)

如何在Flutter小部件测试中测试语义属性?
我想测试一个特定的小部件是否具有预期的语义标签。
这是一个widget:

  1. class Tag extends StatelessWidget {
  2. const Tag({
  3. required this.semanticsLabel,
  4. required this.name,
  5. required this.onPressed,
  6. super.key,
  7. });
  8. final String name;
  9. final VoidCallback onPressed;
  10. final String semanticsLabel;
  11. @override
  12. Widget build(BuildContext context) {
  13. return Semantics(
  14. label: semanticsLabel,
  15. button: true,
  16. child: ExcludeSemantics(
  17. child: OutlinedButton(
  18. onPressed: onPressed,
  19. child: Text(
  20. name,
  21. ),
  22. ),
  23. ),
  24. );
  25. }
  26. }

字符串

9jyewag0

9jyewag01#

标签的测试用例可能如下所示:

  1. final tagCategorySemantics = tester.getSemantics(find.byType(Tag));
  2. expect(tagCategorySemantics.label, 'Semantic Tag Label');

字符串
如果你也有一个语义动作,它可以像这样测试:

  1. final semanticsOwner =
  2. RendererBinding.instance.rootPipelineOwner.semanticsOwner;
  3. final tagSemantics = tester.getSemantics(find.byType(Tag));
  4. semanticsOwner?.performAction(
  5. tagSemantics.id,
  6. SemanticsAction.didGainAccessibilityFocus,
  7. );

展开查看全部

相关问题