Repository navigation
WidgetTester.sendKeyEvent(LogicalKeyboardKey.enter) does not trigger TextField.onSubmitted #116752
Copy link
Copy link
Closed
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work lista: tests"flutter test", flutter_test, or one of our tests"flutter test", flutter_test, or one of our testsa: text inputEntering text in a text field or keyboard related problemsEntering text in a text field or keyboard related problemsd: api docsIssues with https://api.flutter.dev/Issues with https://api.flutter.dev/found in release: 3.3Found to occur in 3.3Found to occur in 3.3found in release: 3.7Found to occur in 3.7Found to occur in 3.7frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer version
Description
Steps to Reproduce
- Execute the first test below under code sample, and observe that it fails
- Execute the second test below under code sample, and observe that it passes
Expected results:
WidgetTester.sendKeyEvent(LogicalKeyboardKey.enter) would call the onSubmit callback in a TextField
Actual results:
WidgetTester.sendKeyEvent(LogicalKeyboardKey.enter) can interact with buttons, but not form fields.
Code sample
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Can submit form field via LogicalKeyboardKey', (tester) async {
bool wasSubmitted = false;
final app = MaterialApp(
home: Material(
child: Center(
child: TextField(
onSubmitted: ((value) => wasSubmitted = true),
),
),
),
);
await tester.pumpWidget(app);
// Set initial focus to window.
await tester.tapAt(const Offset(0, 0));
// Move focus to first focusable widget via keyboard (TextField).
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
// Press enter to submit
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
expect(wasSubmitted, true); // <- THIS FAILS
});
testWidgets('Can submit button via LogicalKeyboardKey', (tester) async {
bool wasSubmitted = false;
final app = MaterialApp(
home: Material(
child: Center(
child: ElevatedButton(
onPressed: (() => wasSubmitted = true),
child: const Text('Submit'),
),
),
),
);
await tester.pumpWidget(app);
// Set initial focus to window.
await tester.tapAt(const Offset(0, 0));
// Move focus to first focusable widget via keyboard (ElevatedButton).
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
// Press enter to submit
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
expect(wasSubmitted, true); // <- THIS PASSES
});
}
[✓] Flutter (Channel stable, 3.3.8, on macOS 12.6.1 21G217 darwin-x64, locale en-US)
• Flutter version 3.3.8 on channel stable at /Users/nejrd/development/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 52b3dc25f6 (4 weeks ago), 2022-11-09 12:09:26 +0800
• Engine revision 857bd6b74c
• Dart version 2.18.4
• DevTools version 2.15.0
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at /Users/nejrd/Library/Android/sdk
✗ cmdline-tools component is missing
Run `path/to/sdkmanager --install "cmdline-tools;latest"`
See https://developer.android.com/studio/command-line for more details.
✗ Android license status unknown.
Run `flutter doctor --android-licenses` to accept the SDK licenses.
See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 14B47b
• CocoaPods version 1.11.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2021.2)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
[✓] VS Code (version 1.73.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.54.0
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work lista: tests"flutter test", flutter_test, or one of our tests"flutter test", flutter_test, or one of our testsa: text inputEntering text in a text field or keyboard related problemsEntering text in a text field or keyboard related problemsd: api docsIssues with https://api.flutter.dev/Issues with https://api.flutter.dev/found in release: 3.3Found to occur in 3.3Found to occur in 3.3found in release: 3.7Found to occur in 3.7Found to occur in 3.7frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer version
Activity
darshankawar commentedon Dec 9, 2022
Thanks for the report. Using code sample provided, seeing same behavior, ie, first test fails and other passes:
stable, master flutter doctor -v
goderbauer commentedon Dec 13, 2022
@justinmc @dkwingsmt
TahaTesser commentedon Dec 14, 2022
I believe the key events for text inputs are handled by the framework.
The correct way to trigger
onSubmittedis by sending an input action.justinmc commentedon Dec 22, 2022
@TahaTesser is right. I think the real problem here is that the docs don't explain this very well. I've opened a PR that adds a small section about testing to the onSubmitted docs: #117550
github-actions commentedon Mar 4, 2023
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.