Sample integration test with Flutter new integration_test?

1.1k views Asked by At
1

There are 1 answers

2
Paras Arora On
  1. First add a dependency in pubspec.yaml under dev
dev_dependencies:
  flutter_test:
    sdk: flutter
  integration_test:
    sdk: flutter
  test: ^1.9.4
  1. Your package should have a structure that looks like this: enter image description here

  2. in test/test_driver/integration_test.dart

                   import'package:integration_test/integration_test_driver.dart';
    
                                   Future<void> main() => integrationDriver(); 
    

4.In integration_test/foo_test.dart example

                                    void main() {
                                      IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  
                                      testWidgets("Sign in test example", (WidgetTester tester) async {
                                        final Finder signInEmailField = find.byKey(Key('signInEmailField'));
                                        final Finder signInPasswordField = find.byKey(Key('signInPasswordField'));
                                        final Finder signInSaveButton = find.byKey(Key('signInSaveButton'));
  
                                        await tester.pumpWidget(MyApp());
  
                                        await tester.pumpAndSettle();
  
                                        await tester.tap(find.byKey(Key('signInEmailField')));
                                        await tester.enterText(signInEmailField, "[email protected]");
  
                                        await tester.tap(signInPasswordField);
                                        await tester.enterText(signInPasswordField, "123456");
  
                                        await tester.tap(signInSaveButton);
                                        print("button tapped");
                                        await tester.pumpAndSettle(Duration(seconds: 1));
                                        expect(
                                            find.byWidgetPredicate((widget) =>
                                                widget is AppBar &&
                                                widget.title is Text &&
                                                (widget.title as Text).data.startsWith("ToDoApp")),
                                            findsOneWidget);
  
                                        await tester.pumpAndSettle(Duration(seconds: 1));
                                      });
                                    }
  1. Add key like we set in flutter_driver

                       appBar: AppBar(
                         title: Text(
                           'ToDoApp',
                           key: Key("toDoKey"),
                         ),
                         backgroundColor: Colors.brown[400],
                       ),
    
  2. Foo last run the command in your terminal flutter drive
    --driver=test_driver/integration_test.dart
    --target=integration_test/foo_test.dart

Thanks.. happy Fluttering