Flutter 测试 find.byValueKey() 对下拉菜单中的项目不起作用

标签 flutter dart

我的测试没有在下拉菜单中找到带有值键的项目。
它适用于 getText() 和值。

我创建了一个动态函数来用一个值填充每个项目,一个带有 Text(value) 的子项。和一个带有 Key('sign_$value_item') 的 key ;

这是我在应用程序中的完整表格:

  static const menuSigns = <String>[
    'aries',
    'taurus',
    'gemini',
    'cancer',
    'leo',
    'virgo',
    'libra',
    'scorpio',
    'sagittarius',
    'capricorn',
    'aquarius',
    'pisces'
  ];

  final List<DropdownMenuItem<String>> _dropDownMenuSigns = menuSigns
      .map<DropdownMenuItem<String>>((String value) => DropdownMenuItem<String>(
            key: new ValueKey('sign_$value_item'), // i even try with new Key('sign_$value')
            value: value,
            child: new Text(value),
          ))
      .toList();

@override
  Widget build(BuildContext context) {
    return Form(
      key: _formKey,
      child: Container(
        margin: EdgeInsets.fromLTRB(_hPad, 16.0, _hPad, 0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Center(
              child: Container(
                padding: EdgeInsets.fromLTRB(0, 0, 0, 16.0),
                width: CustomTheme.customFormSize.width(context),
                child: DropdownButton(
                  key: Key('sign_list'),
                  isExpanded: true,
                  value: _sign,
                  style: CustomTheme.style.dropDownMenu(context),
                  hint: Text('Choose a sign'),
                  icon: Icon(Icons.arrow_drop_down_circle),
                  onChanged: ((newValue) {
                    setState(() {
                      _sign = newValue;
                    });
                  }),
                  items: _dropDownMenuSigns,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

奇怪的是,如果值的长度很长,例如超过 10 个字符,则测试可以使用 Key 进行。

这是我的测试:
import 'package:flutter_driver/flutter_driver.dart';
import 'package:flutter_gherkin/flutter_gherkin.dart';
import 'package:gherkin/gherkin.dart';

class AstroSignValidation extends AndWithWorld<FlutterWorld> {
  @override
  Future<void> executeStep() async {
    await FlutterDriverUtils.getText(world.driver, find.text('AstroDay'));
    await FlutterDriverUtils.tap(world.driver, find.byValueKey('sign_list')); // open drop down menu is ok
    await FlutterDriverUtils.tap(world.driver, find.byValueKey('sign_cancer_item')); // here test not passed
  }

  RegExp get pattern => RegExp(r"I expect the user enters sign");
}

编辑:这是我的功能文件:
Feature: Get Astro day
  User should be able to get successfully his astro after cliking astro button.

  Scenario: User get astro in successfully
    Given I expect the "user" 1 sign
    And I expect the user enters day
    When user hits Show your astro button
    Then user should land on result screen

最佳答案

我重新创建了你的案例。而不是使用 key属性(property)在 DropdownMenuItem ,你需要在它的 child 内部使用它,即在 Text小部件。这样,由于 flutter 驱动程序会在下拉菜单打开时查找要选择的文本,key属性将在显示菜单项时发挥作用,然后更容易单击我们在测试中通过的任何选项。它运作良好。下面更新了工作代码:

final List<DropdownMenuItem<String>> _dropDownMenuSigns = menuSigns
      .map<DropdownMenuItem<String>>((String value) => DropdownMenuItem<String>(
  //  key: new ValueKey('sign_$value'),
    value: value,
    child: new Text(value, key: Key('sign_$value'),),  // use key here on text
  ))
      .toList();

驱动测试:
class AstroSignValidation extends GivenWithWorld<FlutterWorld> {
  @override
  Future<void> executeStep() async {
    await FlutterDriverUtils.getText(world.driver, find.text('Choose a sign'));
    await FlutterDriverUtils.tap(world.driver, find.byValueKey('sign_list')); // open drop down menu is ok
    await FlutterDriverUtils.tap(world.driver, find.byValueKey('sign_virgo')); // selects sign properly
    print('selected sign');
  }

  RegExp get pattern => RegExp(r"I expect the user enters sign");
}

测试通过:

enter image description here

注:我直接用了Given功能文件中的语句并相应扩展 GivenWithWorld在我的考试中上课。您需要根据需要使用它。

希望这能回答你的问题。

关于Flutter 测试 find.byValueKey() 对下拉菜单中的项目不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58503216/

相关文章:

flutter - 在 Flutter 中展开时如何去除 ExpansionTile 的分隔线

dart - 扩展运算符可以用于提供函数参数吗?

listview - 如何创建一个像在 flutter 中使用 listview 一样的按钮?

regex - 为什么这个正则表达式单元测试失败?

flutter - 获取 map 键的位置或如何从 map 设置Stepper isActive

android-studio - 无法为 AMD 处理器安装 Android Emulator Hypervisor 驱动程序

flutter - 限制特殊字符输入 flutter

flutter - 未处理的异常 : inheritFromWidgetOfExactType(_LocalizationsScope) or inheritFromElement() was called before _ScreenState. initState() 已完成

list - 从列表中删除非唯一对象(可通过属性识别)

flutter - 在 Flutter 中将图像 Assets 转换为 base64