【问题标题】:How to do integration test with flutter driver on autocomplete textfield in flutter如何在颤振的自动完成文本字段上使用颤振驱动程序进行集成测试
【发布时间】:2021-10-23 05:37:12
【问题描述】:

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

【问题讨论】:

    标签: flutter integration-testing bdd autocompletetextview flutterdriver


    【解决方案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(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-07-13
      • 2020-10-23
      • 2022-08-11
      • 2020-04-17
      • 2021-02-24
      • 2021-10-12
      • 1970-01-01
      • 2021-03-27
      • 2021-01-06
      相关资源
      最近更新 更多