Many apps feature lists of content, from email clients to music apps and beyond.In order to verify that lists contain the content we expect using integrationtests, we need a way to scroll through lists to search for particular items.

In order to scroll through lists via integration tests, we can use the methodsprovided by theFlutterDriverclass, which is included in theflutter_driverpackage:

In this recipe, we’ll learn how to scroll through a list of items in order toverify a specific Widget is being displayed, and discuss the pros on cons ofdifferent approaches. If you’re just getting started with integration testing,please read through the Introduction to integrationtesting recipe.

Directions

  • Create an app with a list of items
  • Instrument the app
  • Write a test that scrolls through the list
  • Run the test

1. Create an app with a list of items

In this recipe, we’ll build an app that shows a long list of items. In order tokeep this recipe focused on testing, we’ll use the app we created in theWorking with long lists recipe. If you’re unsureof how to work with lists of content, please see that recipe for anintroduction.

As we did in the Introduction to integrationtesting recipe, we’ll also add keys to thewidgets we want to interact with inside our integration tests.

  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. void main() {
  4. runApp(MyApp(
  5. items: List<String>.generate(10000, (i) => "Item $i"),
  6. ));
  7. }
  8. class MyApp extends StatelessWidget {
  9. final List<String> items;
  10. MyApp({Key key, @required this.items}) : super(key: key);
  11. @override
  12. Widget build(BuildContext context) {
  13. final title = 'Long List';
  14. return MaterialApp(
  15. title: title,
  16. home: Scaffold(
  17. appBar: AppBar(
  18. title: Text(title),
  19. ),
  20. body: ListView.builder(
  21. // Add a key to the ListView. This allows us to find the list and
  22. // scroll through it in our tests
  23. key: Key('long_list'),
  24. itemCount: items.length,
  25. itemBuilder: (context, index) {
  26. return ListTile(
  27. title: Text(
  28. '${items[index]}',
  29. // Add a key to the Text Widget for each item. This allows
  30. // us to look for a particular item in the list and verify the
  31. // text is correct
  32. key: Key('item_${index}_text'),
  33. ),
  34. );
  35. },
  36. ),
  37. ),
  38. );
  39. }
  40. }

2. Instrument the app

Next, we’ll need to create an instrumented version of our app. This code livesin a file called test_driver/app.dart.

  1. import 'package:flutter_driver/driver_extension.dart';
  2. import 'package:scrollable_app/main.dart' as app;
  3. void main() {
  4. // This line enables the extension
  5. enableFlutterDriverExtension();
  6. // Call the `main()` function of your app or call `runApp` with any widget you
  7. // are interested in testing.
  8. app.main();
  9. }

3. Write a test that scrolls through the list

Now, we can write our test! In this example, we need to scroll through the listof items and verify that a particular item exists in the list. TheFlutterDriverclass provides three methods for scrolling through lists:

  • The scroll method allows us to scroll through a specific list by a given amount.
  • The scrollIntoView method finds a specific Widget that’s already been rendered, and will scroll it completely into view. Some Widgets, such as ListView.builder, render items on-demand.
  • The scrollUntilVisible method scrolls through a list until a specific Widget is visible.While all three methods work for specific use-cases, scrollUntilVisible isoftentimes the most robust option. Why?

  • If we use the scroll method alone, we might incorrectly assume the height of each item in the list. This could lead to scrolling too much or too little.

  • If we use the scrollIntoView method, we assume the Widget has been instantiated and rendered. In order to verify our apps work on a broad range of devices, we might run our integration tests against devices with different screen sizes. Since ListView.builder will render items on-demand, whether or not a particular Widget has been rendered can depend on the size of the screen.Therefore, rather than assuming we know the height of all the items in a list,or that a particular Widget will be rendered on all devices, we can use thescrollUntilVisible method to repeatedly scroll through a list of items untilwe find what we’re looking for!

Let’s see how we can use the scrollUntilVisible method to look through thelist for a particular item! This code lives in a file calledtest_driver/app_test.dart.

  1. // Imports the Flutter Driver API
  2. import 'package:flutter_driver/flutter_driver.dart';
  3. import 'package:test/test.dart';
  4. void main() {
  5. group('Scrollable App', () {
  6. FlutterDriver driver;
  7. // Connect to the Flutter driver before running any tests
  8. setUpAll(() async {
  9. driver = await FlutterDriver.connect();
  10. });
  11. // Close the connection to the driver after the tests have completed
  12. tearDownAll(() async {
  13. if (driver != null) {
  14. await driver.close();
  15. }
  16. });
  17. test('verifies the list contains a specific item', () async {
  18. // Create two SerializableFinders. We will use these to locate specific
  19. // Widgets displayed by the app. The names provided to the byValueKey
  20. // method correspond to the Keys we provided to our Widgets in step 1.
  21. final listFinder = find.byValueKey('long_list');
  22. final itemFinder = find.byValueKey('item_50_text');
  23. await driver.scrollUntilVisible(
  24. // Scroll through this list
  25. listFinder,
  26. // Until we find this item
  27. itemFinder,
  28. // In order to scroll down the list, we need to provide a negative
  29. // value to dyScroll. Ensure this value is a small enough increment to
  30. // scroll the item into view without potentially scrolling past it.
  31. //
  32. // If you need to scroll through horizontal lists, provide a dxScroll
  33. // argument instead
  34. dyScroll: -300.0,
  35. );
  36. // Verify the item contains the correct text
  37. expect(
  38. await driver.getText(itemFinder),
  39. 'Item 50',
  40. );
  41. });
  42. });
  43. }

4. Run the test

Finally, we can run the test using the following command from the root of theproject:

  1. flutter drive --target=test_driver/app.dart