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
?
scrollUntilVisible method Null safetyRepeatedly scroll the widget located by scrollable by dxScroll and dyScroll until item is visible, and then use scrollIntoView to ensure the item's final position matches alignment . The scrollable must locate the scrolling widget that contains item .
The widget test is testing UI components, so it is also called Component Testing. It is used to test a single widget. The main goal of widget testing is to check whether the widget works as expected. A user can write test cases for every widget present in the project.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With