模拟滚动操作

许多应用程序(电子邮件客户端、音乐应用程序等)都具有内容列表功能。要使用 widget 测试来验证列表是否包含预期内容,你需要一种方法来滚动列表以检索指定的项目。

请在集成测试中使用 WidgetTester 类提供的方法测试滚动列表,该类包含在 flutter_test package 中:

在本指南中,你将学习如何滚动列表来验证指定的 widget 是否正在显示,还会了解到不同做法的利弊。

本指南会采用以下步骤:

  1. 创建带有列表的应用程序。

  2. 编写滚动列表的测试。

  3. 运行测试。

1、创建带有列表的应用程序

本章节创建了一个显示长列表的应用程序。为了专注于测试的编写,这里使用 长列表的处理 指南中创建的应用程序。如果你不确定如何使用长列表,请查阅该指南。

请在需要交互的 widget 上添加 key 以便进行集成测试。

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp(
    items: List<String>.generate(10000, (i) => 'Item $i'),
  ));
}

class MyApp extends StatelessWidget {
  final List<String> items;

  const MyApp({super.key, required this.items});

  @override
  Widget build(BuildContext context) {
    const title = 'Long List';

    return MaterialApp(
      title: title,
      home: Scaffold(
        appBar: AppBar(
          title: const Text(title),
        ),
        body: ListView.builder(
          // Add a key to the ListView. This makes it possible to
          // find the list and scroll through it in the tests.
          key: const Key('long_list'),
          itemCount: items.length,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text(
                items[index],
                // Add a key to the Text widget for each item. This makes
                // it possible to look for a particular item in the list
                // and verify that the text is correct
                key: Key('item_${index}_text'),
              ),
            );
          },
        ),
      ),
    );
  }
}

2、编写滚动列表的测试

现在,你可以编写一个测试:滚动列表并验证列表中是否存在指定的项目。使用 WidgetTester 类提供的 scrollUntilVisible() 方法,它可以滚动列表,直到某个指定的 widget 可见为止。这个方法非常有用,因为列表中项目的高度会根据设备的不同而变化。

scrollUntilVisible() 方法只会缓慢地滚动列表,直至找到指定的内容。它不会假定你知道列表中所有项目的高度,也不会假定指定的 widget 会在所有设备上渲染。

以下代码展示了如何使用 scrollUntilVisible() 方法在列表中查找到指定的项目。这段代码位于 test/widget_test.dart 文件中。


// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:scrolling/main.dart';

void main() {
  testWidgets('finds a deep item in a long list', (tester) async {
    // Build our app and trigger a frame.
    await tester.pumpWidget(MyApp(
      items: List<String>.generate(10000, (i) => 'Item $i'),
    ));

    final listFinder = find.byType(Scrollable);
    final itemFinder = find.byKey(const ValueKey('item_50_text'));

    // Scroll until the item to be found appears.
    await tester.scrollUntilVisible(
      itemFinder,
      500.0,
      scrollable: listFinder,
    );

    // Verify that the item contains the correct text.
    expect(itemFinder, findsOneWidget);
  });
}

3、运行测试

在项目根目录下使用以下指令运行测试:

flutter test test/widget_test.dart