点击、拖动和输入文本
许多 Widget 不仅显示信息,还会响应用户交互。这包括可以轻触的按钮,以及用于输入文本的 TextField
。
要测试这些交互,你需要一种在测试环境中模拟它们的方法。为此,请使用 WidgetTester
库。
WidgetTester
提供了输入文本、轻触和拖动的方法。
在许多情况下,用户交互会更新应用的状态。在测试环境中,当状态改变时,Flutter 不会自动重建 Widget。为确保在模拟用户交互后重建 Widget 树,请调用 WidgetTester
提供的 pump()
或 pumpAndSettle()
方法。本指南将使用以下步骤:
- 创建要测试的 Widget。
- 在文本字段中输入文本。
- 确保轻触按钮可以添加待办事项。
- 确保滑动删除可以移除待办事项。
1. 创建要测试的 Widget
#在此示例中,创建一个基本的待办事项应用,并测试三个功能:
- 在
TextField
中输入文本。 - 轻触
FloatingActionButton
将文本添加到待办事项列表。 - 滑动删除以将项目从列表中移除。
为了将重点放在测试上,本指南不会提供关于如何构建待办事项应用的详细说明。要了解此应用是如何构建的,请参阅相关指南:
Dart
class TodoList extends StatefulWidget {
const TodoList({super.key});
@override
State<TodoList> createState() => _TodoListState();
}
class _TodoListState extends State<TodoList> {
static const _appTitle = 'Todo List';
final todos = <String>[];
final controller = TextEditingController();
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _appTitle,
home: Scaffold(
appBar: AppBar(title: const Text(_appTitle)),
body: Column(
children: [
TextField(controller: controller),
Expanded(
child: ListView.builder(
itemCount: todos.length,
itemBuilder: (context, index) {
final todo = todos[index];
return Dismissible(
key: Key('$todo$index'),
onDismissed: (direction) => todos.removeAt(index),
background: Container(color: Colors.red),
child: ListTile(title: Text(todo)),
);
},
),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
todos.add(controller.text);
controller.clear();
});
},
child: const Icon(Icons.add),
),
),
);
}
}
2. 在文本字段中输入文本
#现在你已经有了待办事项应用,开始编写测试。首先在 TextField
中输入文本。
通过以下方式完成此任务:
- 在测试环境中构建 Widget。
- 使用
WidgetTester
的enterText()
方法。
Dart
testWidgets('Add and remove a todo', (tester) async {
// Build the widget
await tester.pumpWidget(const TodoList());
// Enter 'hi' into the TextField.
await tester.enterText(find.byType(TextField), 'hi');
});
3. 确保轻触按钮可以添加待办事项
#在 TextField
中输入文本后,确保轻触 FloatingActionButton
会将项目添加到列表中。
这包括三个步骤:
Dart
testWidgets('Add and remove a todo', (tester) async {
// Enter text code...
// Tap the add button.
await tester.tap(find.byType(FloatingActionButton));
// Rebuild the widget after the state has changed.
await tester.pump();
// Expect to find the item on screen.
expect(find.text('hi'), findsOneWidget);
});
4. 确保滑动删除可以移除待办事项
#最后,确保对代办事项执行滑动删除操作会将其从列表中移除。这包括三个步骤:
- 使用
drag()
方法执行滑动删除操作。 - 使用
pumpAndSettle()
方法持续重建 Widget 树,直到删除动画完成。 - 确保该项目不再显示在屏幕上。
Dart
testWidgets('Add and remove a todo', (tester) async {
// Enter text and add the item...
// Swipe the item to dismiss it.
await tester.drag(find.byType(Dismissible), const Offset(500, 0));
// Build the widget until the dismiss animation ends.
await tester.pumpAndSettle();
// Ensure that the item is no longer on screen.
expect(find.text('hi'), findsNothing);
});
完整示例
#Dart
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Add and remove a todo', (tester) async {
// Build the widget.
await tester.pumpWidget(const TodoList());
// Enter 'hi' into the TextField.
await tester.enterText(find.byType(TextField), 'hi');
// Tap the add button.
await tester.tap(find.byType(FloatingActionButton));
// Rebuild the widget with the new item.
await tester.pump();
// Expect to find the item on screen.
expect(find.text('hi'), findsOneWidget);
// Swipe the item to dismiss it.
await tester.drag(find.byType(Dismissible), const Offset(500, 0));
// Build the widget until the dismiss animation ends.
await tester.pumpAndSettle();
// Ensure that the item is no longer on screen.
expect(find.text('hi'), findsNothing);
});
}
class TodoList extends StatefulWidget {
const TodoList({super.key});
@override
State<TodoList> createState() => _TodoListState();
}
class _TodoListState extends State<TodoList> {
static const _appTitle = 'Todo List';
final todos = <String>[];
final controller = TextEditingController();
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _appTitle,
home: Scaffold(
appBar: AppBar(title: const Text(_appTitle)),
body: Column(
children: [
TextField(controller: controller),
Expanded(
child: ListView.builder(
itemCount: todos.length,
itemBuilder: (context, index) {
final todo = todos[index];
return Dismissible(
key: Key('$todo$index'),
onDismissed: (direction) => todos.removeAt(index),
background: Container(color: Colors.red),
child: ListTile(title: Text(todo)),
);
},
),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
todos.add(controller.text);
controller.clear();
});
},
child: const Icon(Icons.add),
),
),
);
}
}