Reputation: 126684
I am not sure how I would check whether a widget is currently shown or not using FlutterDriver
.
Using WidgetTester
this is really easy to perform using e.g. findsOneWidget
.
However, during integration tests using FlutterDriver
, there is no access to a WidgetTester
object.
The FlutterDriver.waitFor
method does not indicate whether the widget was found in the given duration.
How do I check whether a widget is on screen using FlutterDriver
?
Upvotes: 17
Views: 9320
Reputation: 11659
Flutter Driver doesn't have explicit method to check if a widget is present / exists, but we can create a custom method which will serve the purpose using waitFor
method. For example, I have a simple text
widget on screen and I will write a flutter driver test to check if that widget is present or not using a custom method isPresent
.
Main code:
body: Center(
child:
Text('This is Test', key: Key('textKey'))
Flutter driver test to check if this widget is present or not is as below:
test('check if text widget is present', () async {
final isExists = await isPresent(find.byValueKey('textKey'), driver);
if (isExists) {
print('widget is present');
} else {
print('widget is not present');
}
});
isPresent
is custom method whose definition is as below:
isPresent(SerializableFinder byValueKey, FlutterDriver driver, {Duration timeout = const Duration(seconds: 1)}) async {
try {
await driver.waitFor(byValueKey,timeout: timeout);
return true;
} catch(exception) {
return false;
}
}
Running the test detects the widget is present:
If I comment out the text
widget code and then run the test, it detects that widget is not present:
Upvotes: 24