信息:
我创建了一个样例Flutter单元测试,用于测试登录界面,在该界面有电子邮件和密码输入框以及登录按钮。
要求:
需要测试虚假情况,为此,我根据以下步骤编写了代码。
- 打开main.dart文件
- 填写电子邮件和密码字段
- 点击登录按钮。在此处调用API,并在屏幕上显示加载器,直到API获得成功或失败响应为止。
- 需要检查是否显示带有消息的失败对话框。
问题/查询:
现在当调用API时,我想等待加载器可见,直到加载器消失。因此,目前我只是放置了手动延迟以执行下一个代码,但我想使其动态化。所以,请让我知道如何基于加载器可见性设置动态延迟?
代码:
void main() {
group('App Test', () {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('Login Fail Test', (WidgetTester tester) async {
await app.main();
await tester.pumpAndSettle();
await tester.pump(new Duration(seconds: 2));
final emailField = find.byType(TextFormField).first;
final passwordField = find.byType(TextFormField).last;
final loginButton = find.byType(RaisedButton).first;
await tester.enterText(emailField, 'Test');
await tester.pumpAndSettle();
await tester.pump(new Duration(seconds: 1));
await tester.enterText(passwordField, 'Test123');
await tester.pumpAndSettle();
await tester.pump(new Duration(seconds: 1));
await tester.tap(loginButton);
await tester.pumpAndSettle();
await tester.pump(new Duration(seconds: 3));
final dialog = find.byType(AlertDialog).first;
await tester.element(dialog);
await tester.pumpAndSettle();
await tester.pump(new Duration(seconds: 1));
final dialogButton = find.byType(FlatButton).first;
await tester.tap(dialogButton);
await tester.pumpAndSettle();
await tester.pump(new Duration(seconds: 2));
});
}