2021/3/20 : Flutter : Find widgetsの訳

null safety article

Contents

Find widgets

To locate widgets in a test environment, use the Finder classes.

テスト環境でウィジェットを見つけるには、Finder クラスを使用します。

 

While it’s possible to write your own Finder classes, it’s generally more convenient to locate widgets using the tools provided by the flutter_test package.

独自のFinderクラスを作成することも可能ですが、通常は、flutter_testパッケージで提供されているツールを使用してウィジェットを見つける方が便利です。

 

During a flutter run session on a widget test, you can also interactively tap parts of the screen for the Flutter tool to print the suggested [Finder].

flutter runウィジェットテストのセッションに、Flutterツールの画面の一部をインタラクティブにタップして、提案された[ Finder]を印刷することもできます

 

This recipe looks at the find constant provided by the flutter_test package, and demonstrates how to work with some of the Finders it provides.

このレシピではflutter_testパッケージによって提供されるfind定数を調べ、Flutter_testが提供するいくつかのFinder定数を操作する方法を示しています。

 

For a full list of available finders, see the CommonFinders documentation.

使用可能なファインダーの完全なリストについては、CommonFindersドキュメントを参照してください。

 

If you’re unfamiliar with widget testing and the role of Finder classes, review the Introduction to widget testing recipe.

ウィジェットテストとFinderクラスの役割に慣れていない場合は、Introduction to widget testing recipe.を確認してください。

 

今回は次のステップで進めていきます。

  1. Textウィジェットを見つけます。
  2. 特定のKeyを持つウィジェットを見つけます。
  3. 特定のウィジェットインスタンスを見つけます。

1. Find a Text widget

In testing, you often need to find widgets that contain specific text.

テストでは、特定のテキストを含むウィジェットを見つけることが必要な場合がよくあります。

 

This is exactly what the find.text() method is for.

まさにfind.text()メソッドがその役割です。(一つ前の回で使用した。)

 

It creates a Finder that searches for widgets that display a specific String of text.

find.text()メソッドはFinderを生成し、特定のStringまたはtextを表示するウィジェットを探します。

testWidgets('finds a Text widget', (WidgetTester tester) async {
  // Build an app with a Text widget that displays the letter 'H'.
  //文字'H'を表示するTextウィジェットを持つアプリをビルドする。
  await tester.pumpWidget(MaterialApp(
    home: Scaffold(
      body: Text('H'),
    ),
  ));

  // Find a widget that displays the letter 'H'.
  //文字'H'を表示するウィジェットを見つける。
  expect(find.text('H'), findsOneWidget);
});

 


2. Find a widget with a specific Key

In some cases, you might want to find a widget based on the Key that has been provided to it.

場合によっては、提供されているキーに基づいてウィジェットを検索したいことがあります。

 

This can be handy if displaying multiple instances of the same widget.

これは、同じ(種類の)ウィジェットのインスタンスが複数ある場合に便利です。

 

For example, a ListView might display several Text widgets that contain the same text.

例えば、ListViewが、同じ文字列を含むいくつかのTextウィジェットを表示するようなケースです。

 

In this case, provide a Key to each widget in the list.

この場合、リスト内の各ウィジェットにKeyを提供します。

 

This allows an app to uniquely identify a specific widget, making it easier to find the widget in the test environment.

これにより、アプリは特定のウィジェットを一意に識別できるようになり、テスト環境でウィジェットを簡単に見つけることができます。

testWidgets('finds a widget using a Key', (WidgetTester tester) async {
  // Define the test key.
  //テスト用のKeyを生成します。
  final testKey = Key('K');

  // Build a MaterialApp with the testKey.
  //MaterialAppを構築して、testKeyを設定します。
  await tester.pumpWidget(MaterialApp(key: testKey, home: Container()));

  // Find the MaterialApp widget using the testKey.
  //↓testKeyを持つウィジェットを検索します(結果として今回はMaterialAppが該当する)。
  expect(find.byKey(testKey), findsOneWidget);
});

 


3. Find a specific widget instance

Finally, you might be interested in locating a specific instance of a widget.

最後に、ウィジェットの特定のインスタンスを見つけることに興味があるかもしれません。

 

For example, this can be useful when creating widgets that take a child property and you want to ensure you’re rendering the child widget.

たとえば、これは、child プロパティを持つウィジェットを作成し、childウィジェットを確実にレンダリングする場合に役立ちます。
testWidgets('finds a specific instance', (WidgetTester tester) async {
  final childWidget = Padding(padding: EdgeInsets.zero);

  // Provide the childWidget to the Container.
  //ContainerにchildWidgetを渡します。
  await tester.pumpWidget(Container(child: childWidget));

  // Search for the childWidget in the tree and verify it exists.
  //ウィジェットツリーの中にchildWidgetがあることを確認します。
  expect(find.byWidget(childWidget), findsOneWidget);
});

 


Summary

The find constant provided by the flutter_test package provides several ways to locate widgets in the test environment.

flutter_testパッケージが提供するfind定数は、テスト環境でwidgetを検索する(探す)いくつかの方法を提供します。

 

This recipe demonstrated three of these methods, and several more methods exist for different purposes.

このレシピは、これらの方法のうち3つを示しましたが、これ以外の目的のためにさらにいくつかのメソッドが存在します。

 

If the above examples do not work for a particular use-case, see the CommonFinders documentation to review all available methods.

上記サンプルではうまくいかないユースケースについては、 CommonFinders documentationで全ての利用可能なメソッドから解決策を検討してみてください。


Complete example

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

void main() {
  testWidgets('finds a Text widget', (WidgetTester tester) async {
    // Build an App with a Text widget that displays the letter 'H'.
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: Text('H'),
      ),
    ));

    // Find a widget that displays the letter 'H'.
    expect(find.text('H'), findsOneWidget);
  });

  testWidgets('finds a widget using a Key', (WidgetTester tester) async {
    // Define the test key.
    final testKey = Key('K');

    // Build a MaterialApp with the testKey.
    await tester.pumpWidget(MaterialApp(key: testKey, home: Container()));

    // Find the MaterialApp widget using the testKey.
    expect(find.byKey(testKey), findsOneWidget);
  });

  testWidgets('finds a specific instance', (WidgetTester tester) async {
    final childWidget = Padding(padding: EdgeInsets.zero);

    // Provide the childWidget to the Container.
    await tester.pumpWidget(Container(child: childWidget));

    // Search for the childWidget in the tree and verify it exists.
    expect(find.byWidget(childWidget), findsOneWidget);
  });
}

 

参考

https://flutter.dev/docs/cookbook/testing/widget/finders

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です