[英]Submit TextField in Flutter integration test
I have a simple text field that does something when the user submits the value in the text field:我有一个简单的文本字段,当用户提交文本字段中的值时,它会执行某些操作:
TextField(
textInputAction: TextInputAction.search,
onSubmitted: onSearchEmail,
)
Simple enough... I'm trying to write an integration test that checks the action is performed when the user submits the text field - however i can't work out how to simulate the user pressing the 'search' button on the keyboard (android) or whatever the equivalent in iOS is...足够简单......我正在尝试编写一个集成测试来检查用户提交文本字段时执行的操作 - 但是我无法弄清楚如何模拟用户按下键盘上的“搜索”按钮( android) 或 iOS 中的任何等效项...
I naively tried driver.enterText("a@bc\\r\\n");
我天真地尝试了
driver.enterText("a@bc\\r\\n");
to no avail.无济于事。
My integration test:我的集成测试:
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));
});
I don't really want a button on the screen that the user has to press, the keyboard button works fine.我真的不想要用户必须按下的屏幕上的按钮,键盘按钮工作正常。
Edit: Just to clarify - the text IS entered into the field so that bit works, its just the submission that doesn't.编辑:只是为了澄清 - 文本被输入到该字段中,以便该位有效,它只是提交的无效。
Flutter Driver still doesn't have a function to trigger onFieldSubmitted
. Flutter Driver 仍然没有触发
onFieldSubmitted
的功能。 I followed the workaround mentioned here to trigger a TextInputAction
.我按照这里提到的解决方法来触发
TextInputAction
。 The workaround is applicable with integration_test - the current recommended method to write integration tests in Flutter.该解决方法适用于integration_test - 当前推荐的在 Flutter 中编写集成测试的方法。
Let the target test script be:让目标测试脚本为:
ie located in /integration_test/foo_test.dart
即位于
/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);
});
}
Let test_driver/integration_test.dart
be让
test_driver/integration_test.dart
成为
import 'package:integration_test/integration_test_driver.dart';
Future<void> main() => integrationDriver();
As for the sample app that I've used:至于我使用过的示例应用程序:
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),
),
],
),
),
);
}
}
To run this sample:要运行此示例:
flutter drive --target=integration_test/foo_test.dart --driver=test_driver/integration_test.dart
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.