Skip to content

WidgetTester.ensureVisible cannot find widgets in a long list #61458

@CareF

Description

@CareF

The 'ensureVisible' unit test in packages/flutter_test/test/widget_tester_test.dart only deal with a very short list, where all elements are already in cache. If I change the unit test to below (either as a ListView or as in the commented part ListView.builder),

  group('ensureVisible', () {
    testWidgets('scrolls to make widget visible', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: ListView(
              shrinkWrap: true,
              children: <Widget>[
                for (int i = 0; i < 50; i ++)
                  ListTile(title: Text('Item $i')),
              ],
            ),
          //   body: ListView.builder(,
          //     itemCount: 50,
          //     shrinkWrap: true,
          //     itemBuilder: (BuildContext context, int i) => ListTile(title: Text('Item $i')),
          //  ),
         ),
        ),
      );

      // Make sure widget isn't on screen
      expect(find.text('Item 45', skipOffstage: true), findsNothing);

      await tester.ensureVisible(find.text('Item 45', skipOffstage: false));
      await tester.pumpAndSettle();

      expect(find.text('Item 45', skipOffstage: true), findsOneWidget);
    });
  });

It will fail with:

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following StateError was thrown running a test:
Bad state: No element

Compare to its equivalence in flutter_driver FlutterDriver.scrollUntilVisible, the alternative implementation scroll until it finds such a widget.

It might be useful to provide a similar API in WidgetTester.

Metadata

Metadata

Assignees

Labels

a: tests"flutter test", flutter_test, or one of our testsc: crashStack traces logged to the consoleframeworkflutter/packages/flutter repository. See also f: labels.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions