adar2378 / pin_code_fields

A flutter package which will help you to generate pin code fields with beautiful design and animations. Can be useful for OTP or pin code inputs 🤓🤓
https://pub.dev/packages/pin_code_fields
MIT License
706 stars 344 forks source link

Flutter integration Test Hangs with `pumpAndSettle()` when using `PinCodeTextField` #362

Closed iltafah closed 9 months ago

iltafah commented 1 year ago

I have this simple app where I have a button in the homepage, when clicking on it the otp page shows up, it works perfectly fine,

but when I try to do integration tests on it, it hangs when calling pumpAndSettle(), I am not sure from where the problem is, I think it's waiting for the animations to end, though I tried to disable them but still the same problem.

I tried also to replace the PinCodeTextField with a simple input text field and the test integration worked without any hanging.

all I want to know is it possible to run integration test using this package or not.

this is my Flutter app:

main.dart ```dart import 'package:flutter/material.dart'; import 'package:pin_code_fields/pin_code_fields.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter OTP Form Demo', home: HomePage(), ); } } class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Home Page'), ), body: Center( child: ElevatedButton( onPressed: () { Navigator.push( context, MaterialPageRoute(builder: (context) => OtpScreen()), ); }, child: Text('Go to OTP Page'), ), ), ); } } class OtpScreen extends StatefulWidget { @override _OtpScreenState createState() => _OtpScreenState(); } class _OtpScreenState extends State { final TextEditingController _otpController = TextEditingController(); void _submitOtp(String otp) { // Handle OTP submission logic here print("Entered OTP: $otp"); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Enter OTP'), ), body: Center( child: Padding( padding: const EdgeInsets.all(16.0), child: PinCodeTextField( appContext: context, length: 6, controller: _otpController, autoFocus: true, animationType: AnimationType.fade, pinTheme: PinTheme( shape: PinCodeFieldShape.box, borderRadius: BorderRadius.circular(5), fieldHeight: 50, fieldWidth: 40, activeFillColor: Colors.white, ), onChanged: (value) {}, onCompleted: (value) { _submitOtp(value); }, ), ), ), ); } @override void dispose() { _otpController.dispose(); super.dispose(); } } ````
pubspec.yaml: ```dart name: test_integreation_tests description: A new Flutter project. # The following line prevents the package from being accidentally published to # pub.dev using `flutter pub publish`. This is preferred for private packages. publish_to: 'none' # Remove this line if you wish to publish to pub.dev # The following defines the version and build number for your application. # A version number is three numbers separated by dots, like 1.2.43 # followed by an optional build number separated by a +. # Both the version and the builder number may be overridden in flutter # build by specifying --build-name and --build-number, respectively. # In Android, build-name is used as versionName while build-number used as versionCode. # Read more about Android versioning at https://developer.android.com/studio/publish/versioning # In iOS, build-name is used as CFBundleShortVersionString while build-number is used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. version: 1.0.0+1 environment: sdk: '>=2.19.4 <3.0.0' # Dependencies specify other packages that your package needs in order to work. # To automatically upgrade your package dependencies to the latest versions # consider running `flutter pub upgrade --major-versions`. Alternatively, # dependencies can be manually updated by changing the version numbers below to # the latest version available on pub.dev. To see which dependencies have newer # versions available, run `flutter pub outdated`. dependencies: flutter: sdk: flutter pin_code_fields: 7.4.0 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 dev_dependencies: integration_test: sdk: flutter flutter_test: sdk: flutter # The "flutter_lints" package below contains a set of recommended lints to # encourage good coding practices. The lint set provided by the package is # activated in the `analysis_options.yaml` file located at the root of your # package. See that file for information about deactivating specific lint # rules and activating additional ones. flutter_lints: ^2.0.0 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec # The following section is specific to Flutter packages. flutter: # The following line ensures that the Material Icons font is # included with your application, so that you can use the icons in # the material Icons class. uses-material-design: true # To add assets to your application, add an assets section, like this: # assets: # - images/a_dot_burr.jpeg # - images/a_dot_ham.jpeg # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.dev/assets-and-images/#resolution-aware # For details regarding adding assets from package dependencies, see # https://flutter.dev/assets-and-images/#from-packages # To add custom fonts to your application, add a fonts section here, # in this "flutter" section. Each entry in this list should have a # "family" key with the font family name, and a "fonts" key with a # list giving the asset and other descriptors for the font. For # example: # fonts: # - family: Schyler # fonts: # - asset: fonts/Schyler-Regular.ttf # - asset: fonts/Schyler-Italic.ttf # style: italic # - family: Trajan Pro # fonts: # - asset: fonts/TrajanPro.ttf # - asset: fonts/TrajanPro_Bold.ttf # weight: 700 # # For details regarding fonts from package dependencies, # see https://flutter.dev/custom-fonts/#from-packages ```
integration tests: ```dart import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; import 'package:test_integreation_tests/main.dart' as app; import 'package:pin_code_fields/pin_code_fields.dart'; void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); testWidgets("Navigate to OTP Page and Enter OTP", (WidgetTester tester) async { app.main(); await tester.pumpAndSettle(); print('1. Start App'); // Find the 'Go to OTP Page' button and tap it final Finder goToOtpButton = find.text('Go to OTP Page'); await tester.tap(goToOtpButton); await tester.pumpAndSettle(); //Hangs here -_- print('2. button clicked'); // Ensure OTP Screen is displayed expect(find.text('Enter OTP'), findsOneWidget); // Enter OTP final Finder otpFieldFinder = find.byType(PinCodeTextField); await tester.enterText(otpFieldFinder, '123456'); await tester.pump(); print('3. plzz OTP SaMa work'); // Verify OTP has been entered (optional, depending on your logic) // This part might need to be adjusted based on how your app handles the entered OTP expect(find.text('123456'), findsOneWidget); }); } ```

ezgif com-optimize

himal-rawal commented 1 year ago

Did you find solution

iltafah commented 1 year ago

Did you find solution

Yeah I've managed to fix it, instead of working with pumpAndSettle() I used pump(Duration(seconds: 1)), and when entering the OTP text, I call pump(Duration(seconds: 1)) two times, for some reason calling it once doesn't work.

Here is the updated integration test:

integration tests: ```dart import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; import 'package:test_integreation_tests/main.dart' as app; import 'package:pin_code_fields/pin_code_fields.dart'; void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); testWidgets("Navigate to OTP Page and Enter OTP", (WidgetTester tester) async { app.main(); await tester.pumpAndSettle(); print('1. Start App'); // Find the 'Go to OTP Page' button and tap it final Finder goToOtpButton = find.text('Go to OTP Page'); await tester.tap(goToOtpButton); await tester.pump(Duration(seconds: 1)); // await tester.pumpAndSettle();// Hangs Here -_- print('2. button clicked'); // Ensure OTP Screen is displayed // expect(find.text('Enter OTP'), findsOneWidget); // Enter OTP final Finder otpFieldFinder = find.byType(PinCodeTextField); await tester.enterText(otpFieldFinder, '123456'); await tester.pump(Duration(seconds: 1)); await tester.pump(Duration(seconds: 1)); print('3. plzz OTP SaMa work'); // Verify OTP has been entered (optional, depending on your logic) // This part might need to be adjusted based on how your app handles the entered OTP expect(find.text('123456'), findsOneWidget); }); } ```
stale[bot] commented 10 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.