Reputation: 3
So I'm trying to create some UI test cases within Flutter and I am having a lot of difficulty with it at the moment. The exact moment it is failing is when it presses the add button, a save window pops up. Once the save window is completed the UI tester should wait for the next screen and then press okay but it seems that it immediately presses the 'Add' button a second time which causes it to fail as the user has already been added by the first click. No matter how many waits or anything I put in it keeps immediately pressing the 'Add' button whenever it can. The code below is the core area of the issue, thanks so much!
// Find the 'Add' button
expect(find.text('Add'), findsOneWidget);
Finder addButton = find.text('Add');
print("About to tap Add button ONCE");
await tester.tap(addButton);
print("Add button tapped once. Now waiting for 'Select Player 2:' text");
// Immediately remove the Add button from the widget tree to prevent any possibility of tapping it again
await tester.pumpAndSettle();
// Wait for the "Select Player 2:" text to appear
bool selectPlayer2Found = false;
for (int i = 0; i < 30; i++) { // 30-second timeout
await tester.pump(Duration(seconds: 1));
print("Checking for 'Select Player 2:' (attempt ${i + 1})");
if (find.text('Select Player 2:').evaluate().isNotEmpty) {
print("'Select Player 2:' text found!");
selectPlayer2Found = true;
break;
}
// Double-check that the Add button is no longer present
if (find.text('Add').evaluate().isNotEmpty) {
throw Exception("Add button is still visible after tapping it once!");
}
}
if (!selectPlayer2Found) {
throw Exception("'Select Player 2:' text did not appear after 30 seconds");
}
// Now that we've confirmed "Select Player 2:" is visible, look for the OK button
print("Looking for OK button");
expect(find.text('OK'), findsOneWidget);
Finder okButton = find.text('OK');
// Tap the OK button
await tester.tap(okButton);
print("OK button tapped");
await tester.pumpAndSettle();
Upvotes: 0
Views: 50