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:
