#flutter #integration-testing #bdd #autocompletetextview #flutterdriver
Вопрос:
Как выполнить интеграционный тест с драйвером flutter на AutoTextCompleteFiled со значением GlobalKey. Как мы можем идентифицировать виджет, заполненный автотекстом, с помощью глобального ключа и как мы можем вводить текст в этот виджет с помощью автоматизации драйверов flutter в BDD?
Ответ №1:
Вот как вы можете это сделать Я привел пример 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(город) для поиска по тексту вместо ключа
название города, используемого для тестирования, должно присутствовать в используемом списке городов
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'
];