Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if widget is visible using FlutterDriver

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?

like image 827
creativecreatorormaybenot Avatar asked Jun 14 '19 17:06

creativecreatorormaybenot


People also ask

How do you use scrollUntilVisible in flutter?

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 .

What is widget testing in flutter?

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.


1 Answers

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:

enter image description here

If I comment out the text widget code and then run the test, it detects that widget is not present:

enter image description here

like image 181
Darshan Avatar answered Oct 03 '22 18:10

Darshan