0

如果我运行此代码段:

await tester.enterText(
        find.byKey(Key('titleControlKey')), 'Sample Account 1');
await tester.enterText(find.byKey(Key('usernameControlKey')), 'username1');

或者这个:

await tester.enterText(
        find.byKey(Key('titleControlKey')), 'Sample Account 1');
await tester.enterText(find.byKey(Key('passwordControlKey')), 'password1');

它都有效。

但如果我运行三个:

await tester.enterText(
        find.byKey(Key('titleControlKey')), 'Sample Account 1');
await tester.enterText(find.byKey(Key('usernameControlKey')), 'username1');
await tester.enterText(find.byKey(Key('passwordControlKey')), 'password1');

测试将失败并产生此错误:

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following StateError was thrown running a test:
Bad state: No element

When the exception was thrown, this was the stack:
#0      Iterable.single (dart:core/iterable.dart:654:25)
#1      WidgetController.state (package:flutter_test/src/controller.dart:156:42)
#2      WidgetTester.showKeyboard.<anonymous closure> (package:flutter_test/src/widget_tester.dart:1042:42)
#3      WidgetTester.showKeyboard.<anonymous closure> (package:flutter_test/src/widget_tester.dart:1041:39)
#6      TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:71:41)
#7      WidgetTester.showKeyboard (package:flutter_test/src/widget_tester.dart:1041:27)
#8      WidgetTester.enterText.<anonymous closure> (package:flutter_test/src/widget_tester.dart:1077:13)
#9      WidgetTester.enterText.<anonymous closure> (package:flutter_test/src/widget_tester.dart:1076:39)
#12     TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:71:41)
#13     WidgetTester.enterText (package:flutter_test/src/widget_tester.dart:1076:27)
#14     AddAccountScreenRobot.enterAccountDetails (file:///C:/Users/Noyruto/sources/repos/account_saver/integration_test/robots/add_account_screen_robot.dart:25:18)
<asynchronous suspension>
<asynchronous suspension>
(elided 5 frames from dart:async and package:stack_trace)
4

2 回答 2

0

两个 tester.enterText 之间必须有一些稳定时间才能正常工作。根据您的需要添加这些。

        await tester.pump();
        await tester.pumpAndSettle();
        await tester.pump(Duration(seconds: 3));

例子

await tester.enterText(
    find.byKey(Key('titleControlKey')), 'Sample Account 1');
await tester.pumpAndSettle();
await tester.enterText(find.byKey(Key('usernameControlKey')),'username1');
await tester.pumpAndSettle();
await tester.enterText(find.byKey(Key('passwordControlKey')), 'password1');
await tester.pump(Duration(seconds: 3));
于 2022-02-06T11:01:43.890 回答
0

我刚刚解决了我自己的问题。因为 TextFields 在ListView小部件内部,当屏幕键出现时,其他 TextField 小部件不可见。所以我所做的是向下滚动表单tester.fling()以使密码TextField可见。

await tester.enterText(
        find.byKey(Key('titleControlKey')), 'Sample Account 1');
await tester.pumpAndSettle();
await tester.enterText(find.byKey(Key('usernameControlKey')), 'username1');
await tester.pumpAndSettle();
await tester.fling(listFinder, const Offset(0, -500), 10000);
await tester.pumpAndSettle();
await tester.enterText(find.byKey(Key('passwordControlKey')), 'password1');
await tester.pump(Duration(seconds: 2));
于 2022-02-06T13:03:33.733 回答