我有一个简单的文本字段,当用户在文本字段中提交值时,它会执行某些操作:
TextField(
textInputAction: TextInputAction.search,
onSubmitted: onSearchEmail,
)
Run Code Online (Sandbox Code Playgroud)
足够简单......我正在尝试编写一个集成测试,检查用户提交文本字段时执行的操作 - 但是我无法弄清楚如何模拟用户按下键盘上的“搜索”按钮( 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));
});
Run Code Online (Sandbox Code Playgroud)
我真的不希望用户必须按下屏幕上的按钮,键盘按钮工作正常。
编辑:只是为了澄清 - 文本被输入到该字段中,以便该位起作用,只是提交不起作用。
Flutter Driver 还是没有触发的功能onFieldSubmitted。我按照此处提到的解决方法触发了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);
});
}
Run Code Online (Sandbox Code Playgroud)
就test_driver/integration_test.dart这样吧
import 'package:integration_test/integration_test_driver.dart';
Future<void> main() => integrationDriver();
Run Code Online (Sandbox Code Playgroud)
至于我使用过的示例应用程序:
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),
),
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
要运行此示例:
flutter drive --target=integration_test/foo_test.dart --driver=test_driver/integration_test.dart
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4127 次 |
| 最近记录: |