OliverDeLange
OliverDeLange

Reputation: 633

Submit TextField in Flutter integration test

I have a simple text field that does something when the user submits the value in the text field:

TextField(
        textInputAction: TextInputAction.search,
        onSubmitted: onSearchEmail,
      )

Simple enough... I'm trying to write an integration test that checks the action is performed when the user submits the text field - however i can't work out how to simulate the user pressing the 'search' button on the keyboard (android) or whatever the equivalent in iOS is...

I naively tried driver.enterText("[email protected]\r\n"); to no avail.

My integration test:

test('entering email performs a search', () async {
  driver.tap(find.byValueKey('search-email-search-box'));
  driver.enterText("[email protected]\r\n"); // <- doesn't work
  await driver.waitFor(find.text("some text));
});

I don't really want a button on the screen that the user has to press, the keyboard button works fine.

Edit: Just to clarify - the text IS entered into the field so that bit works, its just the submission that doesn't.

Upvotes: 3

Views: 4631

Answers (1)

Omatt
Omatt

Reputation: 10463

Flutter Driver still doesn't have a function to trigger onFieldSubmitted. I followed the workaround mentioned here to trigger a TextInputAction. The workaround is applicable with integration_test - the current recommended method to write integration tests in Flutter.

Let the target test script be:

i.e. located in /integration_test/foo_test.dart

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:sample55101120/main.dart' as app;

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  testWidgets("TextFormField Test", (WidgetTester tester) async {
    // Build the app and trigger a frame.
    await tester.pumpWidget(app.MyApp());

    /// Find TextFormField widget by Key and enter text
    await tester.enterText(find.byKey(Key('search-email-search-box')), '[email protected]');
    await tester.testTextInput.receiveAction(TextInputAction.done);
    await tester.pump();

    expect(find.text('[email protected]'), findsOneWidget);
  });
}

Let test_driver/integration_test.dart be

import 'package:integration_test/integration_test_driver.dart';

Future<void> main() => integrationDriver();

As for the sample app that I've used:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @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> {
  void onSearchEmail(String value){
    debugPrint('onSearchEmail $value');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextFormField(
              key: Key('search-email-search-box'),
              textInputAction: TextInputAction.search,
              onFieldSubmitted: (String value) => onSearchEmail(value),
            ),
          ],
        ),
      ),
    );
  }
}

To run this sample:

flutter drive --target=integration_test/foo_test.dart --driver=test_driver/integration_test.dart

Upvotes: 6

Related Questions