點選、拖動和輸入文字
許多小部件不僅顯示資訊,還會響應使用者互動。這包括可以點選的按鈕,以及用於輸入文字的 TextField。
要測試這些互動,您需要在測試環境中模擬它們。為此,請使用 WidgetTester 庫。
WidgetTester 提供了輸入文字、點選和拖動的方法。
在許多情況下,使用者互動會更新應用的 state。在測試環境中,當 state 發生變化時,Flutter 不會自動重建小部件。要確保在模擬使用者互動後重建小部件樹,請呼叫 WidgetTester 提供的 pump() 或 pumpAndSettle() 方法。此食譜使用以下步驟:
- 建立要測試的小部件。
- 在文字欄位中輸入文字。
- 確保點選按鈕可以新增待辦事項。
- 確保滑動以刪除可以移除待辦事項。
1. 建立一個要測試的小部件
#在此示例中,建立一個基本的待辦事項應用,用於測試三個功能:
- 將文字輸入
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 中輸入文字。
透過以下方式完成此任務:
- 在測試環境中構建小部件。
- 使用
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()方法持續重建小部件樹,直到刪除動畫完成。 - 確保該專案不再出現在螢幕上。
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),
),
),
);
}
}