17

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?

4
  • What's the use-case? That may not be what you want. Integration tests shouldn't care about how widget-tree looks like. Commented Jun 20, 2019 at 11:13
  • But would widget testing be possible? Widget-testing can be used for small integration tests too. Commented Jun 20, 2019 at 12:09
  • @RémiRousselet No, it is not. I can currently avoid the problem by taking manual action while the integration test is running because it only happens at some point, however, this is bad for automation as I cannot always ensure that the integration test runs all the way. Commented Jun 20, 2019 at 12:12
  • You can get item's measurements Commented Jun 22, 2019 at 20:12

1 Answer 1

24
+50

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

Sign up to request clarification or add additional context in comments.

3 Comments

Can we make the test fail intentionally if the widget is not present?
@Donki: just use an expect: expect(await isPresent(find.byValueKey('textKey'), driver), isTrue);
Hi... I try to use this but i got error like "Unexpected <collected> sentinel."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.