I get an error when I try to test if there is a widget after the screen transition in the integration test

Viewed 141

What I want to do and what I can't do

I am trying to verify that there is a Textwidget after the screen transition in the integration test, but I get an error that it has not been discovered.

Error statement


    (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 "home" (ignoring offstage widgets)>
       Which: means none were found but one was expected
    
    When the exception was thrown, this was the stack:

App code


    // main.dart
    import 'package:flutter/material.dart';
    import 'package:integration_test_sample/first_page.dart';
    
    void main() {
      runApp(MyApp());
    }
    
        enter code here
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext coenter code here`ntext) {
        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(
            title: Text(widget.title),
          ),
          body: Center(
            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: (context) => FirstPage()
                  ));
                }, child: Text('Screen transition button')),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),
        );
      }
    }


    // first_page.dart
    
    import 'package:flutter/material.dart';
    
    class FirstPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("home"),
          ),
          body: Column(
            children: [
              Text('test'),
            ],
          ),
        );
      }
    }

test code


    // 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('home'), findsOneWidget);
       expect(find.text('test'), findsOneWidget);
        });
      });
    }

What and how did you search?

To solve the above, I searched for the following keywords.

  • flutter integration_test How to check the widget at screen transition.

However, I could not find any answer that would solve this problem.

Any advice would be appreciated.

0 Answers
Related