gpt4 book ai didi

flutter - Flutter集成测试中提交TextField

转载 作者:行者123 更新时间:2023-12-05 04:01:45 24 4
gpt4 key购买 nike

我有一个简单的文本字段,当用户在文本字段中提交值时它会执行一些操作:

TextField(
textInputAction: TextInputAction.search,
onSubmitted: onSearchEmail,
)

很简单...我正在尝试编写一个集成测试来检查用户提交文本字段时执行的操作 - 但是我不知道如何模拟用户按下“搜索”按钮键盘 (android) 或 iOS 中的任何等效...

我天真地尝试了 driver.enterText("a@b.c\r\n"); 但没有成功。

我的集成测试:

test('entering email performs a search', () async {
driver.tap(find.byValueKey('search-email-search-box'));
driver.enterText("a@b.c\r\n"); // <- doesn't work
await driver.waitFor(find.text("some text));
});

我真的不想要用户必须按下的屏幕上的按钮,键盘按钮工作正常。

编辑:只是为了澄清 - 文本已输入到字段中,以便该位有效,只是提交无效。

最佳答案

Flutter Driver 仍然没有触发onFieldSubmitted 的函数。我遵循了提到的解决方法 here触发 TextInputAction。解决方法适用于 integration_test - 当前推荐的在 Flutter 中编写集成测试的方法。

令目标测试脚本为:

即位于 /integration_test/foo_test.dart

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:sample55101120/main.dart' as app;

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

testWidgets("TextFormField Test", (WidgetTester tester) async {
// Build the app and trigger a frame.
await tester.pumpWidget(app.MyApp());

/// Find TextFormField widget by Key and enter text
await tester.enterText(find.byKey(Key('search-email-search-box')), 'a@b.c');
await tester.testTextInput.receiveAction(TextInputAction.done);
await tester.pump();

expect(find.text('a@b.c'), findsOneWidget);
});
}

test_driver/integration_test.dart

import 'package:integration_test/integration_test_driver.dart';

Future<void> main() => integrationDriver();

至于我使用的示例应用程序:

import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
void onSearchEmail(String value){
debugPrint('onSearchEmail $value');
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
key: Key('search-email-search-box'),
textInputAction: TextInputAction.search,
onFieldSubmitted: (String value) => onSearchEmail(value),
),
],
),
),
);
}
}

要运行此示例:

flutter drive --target=integration_test/foo_test.dart --driver=test_driver/integration_test.dart

关于flutter - Flutter集成测试中提交TextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55101120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com