Reputation: 10503
In a Flutter integration test, I want to wait for a button to become enabled before pressing it. Is this possible?
Upvotes: 2
Views: 1453
Reputation: 1736
This function gets you whether a widget is enabled, and you could then wait for it to change state:
Future<bool> isEnabled(FlutterDriver driver, SerializableFinder widgetFinder) async {
Map widgetDiagnostics = await driver.getWidgetDiagnostics(widgetFinder);
return widgetDiagnostics["properties"]
.firstWhere((property) => property["name"] == 'enabled')["value"];
}
Update: here's that wait- friend
Future<void> waitForEnabled(
FlutterDriver driver, SerializableFinder widgetFinder,
{Duration timeout,
Duration interval,
bool value = true,
String reason}) async {
await driver.waitFor(widgetFinder, timeout: timeout);
timeout ??= Duration(seconds: 5);
interval ??= Duration(milliseconds: 250);
var endTime = DateTime.now().add(timeout);
while (true) {
if (await isEnabled(driver, widgetFinder) == value) {
return;
} else if (DateTime.now().isAfter(endTime)) {
throw TimeoutException(reason ??
'widget did not become ${value ? 'enabled' : 'disabled'} within timeout');
} else {
await Future.delayed(interval);
continue;
}
}
}
You can pass value: false
to have it wait for not-enabled
Upvotes: 3