2

我正在测试一个颤振应用程序。我要运行集成测试,就像 Espresso 和 Selenium 允许 UI 测试一样,根据我的阅读,我了解到测试 Flutter UI 需要设置集成模型。在 SeleniumfindById和 Espresso 中,我们可以使用 , R.id.unique_element_id

我曾尝试理解唯一 id 以引用 Flutter 中的小部件,但不明白。像这样的表格的任何简单测试

mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'You have pushed the button this many times:',
            ),
            new TextFormField(
              decoration: new InputDecoration(
                  labelText: 'Enter your username'
              ),
            ),
            new TextFormField(
              decoration: new InputDecoration(
                  labelText: 'Enter your phone number'
              ),
              keyboardType: TextInputType.number,
            ),

            new Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],

像这样设置测试文件

    import 'package:flutter_driver/flutter_driver.dart';
    import 'package:test/test.dart';

    void main() {
      FlutterDriver driver;

      setUpAll(() async {
        driver = await FlutterDriver.connect();
      });

      tearDownAll(() async {
        if(driver != null){
          driver.close();
        }
      });


test('tap on the button, verify result', () async {
    final SerializableFinder username = find.byValueKey('usernametextfield');
    expect(username, isNotNull);
    //insert value into username

    final SerializableFinder phonenumber = find.byValueKey('phone');
    expect(phonenumber, isNotNull);
    //insert value into phonenumber
});

我如何创建这样的 tetscase 并运行。评论//insert value into username//insert value into phonenumber是我想要完成的实际测试用例。

4

3 回答 3

3

flutter_driver 现在已经死了(已停产)你可以使用这个integration_test 我创建登录流程示例

  1. 首先在dev下的pubspec.yaml中添加一个依赖

           dev_dependencies:
             flutter_test:
               sdk: flutter
             test: ^1.9.4
             integration_test: ^1.0.1
    
  2. 您的包应具有如下结构: 在此处输入图像描述

  3. 在 test/test_driver/integration_test.dart 中添加这个

                   import'package:integration_test/integration_test_driver.dart';
    
                                   Future<void> main() => integrationDriver(); 
    

4.在 integration_test/foo_test.dart 示例中添加这个

                                    void main() {
                                      IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  
                                      testWidgets("Sign in test example", (WidgetTester tester) async {
                                        final Finder signInEmailField = find.byKey(Key('signInEmailField'));
                                        final Finder signInPasswordField = find.byKey(Key('signInPasswordField'));
                                        final Finder signInSaveButton = find.byKey(Key('signInSaveButton'));
  
                                        await tester.pumpWidget(MyApp());
  
                                        await tester.pumpAndSettle();
  
                                        await tester.tap(find.byKey(Key('signInEmailField')));
                                        await tester.enterText(signInEmailField, "paras@gmail.com");
  
                                        await tester.tap(signInPasswordField);
                                        await tester.enterText(signInPasswordField, "123456");
  
                                        await tester.tap(signInSaveButton);
                                        print("button tapped");
                                        await tester.pumpAndSettle(Duration(seconds: 1));
                                        expect(
                                            find.byWidgetPredicate((widget) =>
                                                widget is AppBar &&
                                                widget.title is Text &&
                                                (widget.title as Text).data.startsWith("ToDoApp")),
                                            findsOneWidget);
  
                                        await tester.pumpAndSettle(Duration(seconds: 1));
                                      });
                                    }
  1. 像我们在flutter_driver中设置的那样添加密钥

                       appBar: AppBar(
                         title: Text(
                           'ToDoApp',
                           key: Key("toDoKey"),
                         ),
                         backgroundColor: Colors.brown[400],
                       ),
    
  2. Foo 最后在终端中运行命令颤振驱动器
    --driver=test_driver/integration_test.dart
    --target=integration_test/foo_test.dart

谢谢..快乐飘扬

于 2020-12-16T13:17:06.400 回答
1

我创建了一种输入文本的方法。也许这对某人有用。

  Future<void> enterText(SerializableFinder itemFinder, String text) async {
    final isTextFieldPresent = await isPresent(itemFinder);

    if (isTextFieldPresent) {
      await _driver.tap(itemFinder);
      await _driver.enterText(text);
      await _driver.waitFor(find.text(text));
    } else {
      expect(isTextFieldPresent, true, reason: 'Cannot find text field');
    }
  }

这是我在上面使用的 isPresent 方法

  Future<bool> isPresent(SerializableFinder itemFinder,
      {int delayInMilis = 1000}) async {
    final timeout = Duration(milliseconds: delayInMilis);
    try {
      await _driver.waitFor(itemFinder, timeout: timeout);
      return true;
    } catch (exception) {
      return false;
    }
  }
于 2020-11-02T08:14:22.833 回答
0

不确定您是否找到了问题的答案,但我在这里发布了一个解决方案。希望能帮助到你。

基本上,您需要在小部件上定义key属性TextFormField,这将帮助您唯一地识别该小部件,然后您可以对其执行操作。例子:

new TextFormField(
              key: Key('userNameTextField'),
              decoration: new InputDecoration(
                  labelText: 'Enter your username'
              ),
            ),

然后在你的测试文件中,你会这样写:

test('tap on the button, verify result', () async {
    final SerializableFinder username = find.byValueKey('userNameTextField');
    expect(username, isNotNull);
    await driver.waitFor(username);
    await driver.enterText('Test');

  // repeat above for `phoneNumber` widget.

    final SerializableFinder phonenumber = find.byValueKey('phone');
    expect(phonenumber, isNotNull);
    //insert value into phonenumber
});
于 2019-05-24T09:22:48.287 回答