0

如何在具有 GlobalKey 值的 AutoTextCompleteFiled 上使用颤振驱动程序进行集成测试。我们如何使用 Global 键识别 AutoTextCompleteFiled 小部件,以及如何在 BDD 中使用颤振驱动程序自动化在此小部件中输入文本?

4

1 回答 1

0

这是你可以做到的我已经给出了flutter_typeahead的例子,它也用于自动完成
这是测试用例

group('App', () {
final saveButton = find.byValueKey('save_button');
final cityField = find.byValueKey('city_field');
final city = 'Mumbai';

FlutterDriver driver;

// Connect to the Flutter driver before running any tests.
setUpAll(() async {
  driver = await FlutterDriver.connect();
});

// Close the connection to the driver after the tests have completed.
tearDownAll(() async {
  driver.close();
});

test('test cities field', () async {
  //for tapping city autocomplete field
  await driver.tap(cityField);
  //for entering city
  await driver.enterText(city);
  //for selecting suggestion or find.text(city) to search by text
  await driver.tap(find.byValueKey(city));
  //for tapping save button
  await driver.tap(saveButton);
});
});

这是自动完成字段
这里要注意的主要事项是,您需要在每个项目构建器小部件中使用密钥,稍后将使用该小部件来点击该小部件
您还可以使用 find.text(city) 通过文本而不是关键
城市名称进行搜索用于测试的城市列表中必须存在

Column(
      children: <Widget>[
        Text('What is your favorite city?'),
        TypeAheadFormField(
          key: Key('city_field'),
          textFieldConfiguration: TextFieldConfiguration(
              controller: this._typeAheadController,
              decoration: InputDecoration(labelText: 'City')),
          suggestionsCallback: (pattern) {
            return getSuggestions(pattern, citiesList);
          },
          itemBuilder: (context, suggestion) {
            return ListTile(
              key: Key(suggestion),
              title: Text(suggestion),
            );
          },
          transitionBuilder: (context, suggestionsBox, controller) {
            return suggestionsBox;
          },
          onSuggestionSelected: (suggestion) {
            this._typeAheadController.text = suggestion;
          },
          validator: (value) {
            if (value.isEmpty) {
              return 'Please select a city';
            }
            return null;
          },
          onSaved: (value) => this._selectedCity = value,
        ),
        TextButton(
          key: Key('save_button'),
          onPressed: () {},
          child: Text('Save'),
        ),
      ],
    )

城市列表

List<String> citiesList = [
'Mumbai',
'Pune',
'Delhi',
'Surat',
'Jaipur',
'Chennai',
'Kolkata',
'Bangalore'
];
于 2021-09-08T08:35:38.067 回答