【问题标题】:How can I run a flutter widget test for cupertino data picker?如何为 Cupertino 数据选择器运行颤振小部件测试?
【发布时间】:2021-09-02 04:17:08
【问题描述】:

我正在尝试为 cupertino 日期选择器编写测试,但我找不到任何结果。如何为小部件测试选择一个特定的日期时间。

testWidgets("description", (WidgetTester tester)async{
    await tester.pumpWidget(MaterialApp(home: MyPage()));

    final textField=find.byKey(Key("nameTextField"));
    final datePicker=find.byKey(Key("birthDatePicker"));
    final button=find.byType(ElevatedButton);


    await tester.enterText(textField, "not important");
    await tester.drag(datePicker, Offset(0.0, 10.0)); ///Here the problem ?

    await tester.press(button);

    await tester.pump();
    expect(find.text("Error Text"), findsOneWidget);
  });

我的问题是,如何选择 1/1/2021 进行小部件测试?

【问题讨论】:

    标签: flutter flutter-test


    【解决方案1】:

    查看为小部件实施的测试,这些测试结果是不言自明的。他们倾向于在整个文本中使用参考,并以恒定的偏移量进行动作。

    https://github.com/flutter/flutter/blob/master/packages/flutter/test/cupertino/date_picker_test.dart

    下面我也给你举个例子

    testWidgets('picker automatically scrolls away from invalid date on day change', (WidgetTester tester) async {
          late DateTime date;
          await tester.pumpWidget(
            CupertinoApp(
              home: Center(
                child: SizedBox(
                  height: 400.0,
                  width: 400.0,
                  child: CupertinoDatePicker(
                    mode: CupertinoDatePickerMode.date,
                    onDateTimeChanged: (DateTime newDate) {
                      date = newDate;
                    },
                    initialDateTime: DateTime(2018, 2, 27), // 2018 has 28 days in Feb.
                  ),
                ),
              ),
            ),
          );
    
          await tester.drag(find.text('27'), const Offset(0.0, -32.0), touchSlopY: 0.0, warnIfMissed: false); // see top of file
          await tester.pump();
          expect(
            date,
            DateTime(2018, 2, 28),
          );
    
          await tester.drag(find.text('28'), const Offset(0.0, -32.0), touchSlopY: 0.0, warnIfMissed: false); // see top of file
          await tester.pump(); // Once to trigger the post frame animate call.
    
          // Callback doesn't transiently go into invalid dates.
          expect(
            date,
            DateTime(2018, 2, 28),
          );
          // Momentarily, the invalid 29th of Feb is dragged into the middle.
          expect(
            tester.getTopLeft(find.text('2018')).dy,
            tester.getTopLeft(find.text('29')).dy,
          );
    
          await tester.pump(); // Once to start the DrivenScrollActivity.
          await tester.pump(const Duration(milliseconds: 500));
    
          expect(
            date,
            DateTime(2018, 2, 28),
          );
          expect(
            tester.getTopLeft(find.text('2018')).dy,
            tester.getTopLeft(find.text('28')).dy,
          );
        });
    

    【讨论】:

    • 非常感谢你,我还没有测试你的答案,但我可以看到逻辑,它必须工作:)
    • 不客气,虽然这对我来说也是合乎逻辑的,但我在拖动功能的行为方面遇到了问题,而不是真正的运动。如果您也是这种情况,您可以使用以下解决方法await tester.dragFrom(tester.getCenter(find.text(now.year.toString())), const Offset(0.0, 50.0));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 2021-06-28
    相关资源
    最近更新 更多