やりたいこととできないこと
インテグレーションテストで画面遷移後にTextwidgetがあることを確認しようとしていますが、発見できていないというエラーが出ます。
エラー文
(The following exception is now available via WidgetTester.takeException:)
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
Expected: exactly one matching node in the widget tree
Actual: _TextFinder:<zero widgets with text "ホーム" (ignoring offstage widgets)>
Which: means none were found but one was expected
When the exception was thrown, this was the stack:
何を・どのように調べたのか
上記を解決するために、以下のキーワードで検索しました。
- flutter integration_test 画面遷移した時のwidgetを確認するやり方
- flutter integration_test How to check the widget at screen transition.
参考にした記事のURLは以下です。
Flutterの新しいIntegration Testの導入
ソースコード
// main.dart
import 'package:flutter/material.dart';
import 'package:integration_test_sample/first_page.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@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> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
ElevatedButton(onPressed: (){
// Navigator.push(context, MaterialPageRoute(builder: builder));
Navigator.push(context, MaterialPageRoute(
builder: (context) => FirstPage()
));
}, child: Text('移動')),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
// first_page.dart
import 'package:flutter/material.dart';
class FirstPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("ホーム"),
),
body: Column(
children: [
Text('テスト'),
],
),
);
}
}
// integration_test.dart
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:integration_test_sample/main.dart' as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
group('group', () {
testWidgets('start', (WidgetTester tester) async {
app.main();
await tester.pumpAndSettle();
expect(find.byType(FloatingActionButton), findsOneWidget);
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
await tester.tap(find.byType(ElevatedButton));
await tester.pump();
expect(find.text('ホーム'), findsOneWidget);
expect(find.text('0'), findsNothing);
});
});
}