Skip to content

Widget test issue with DropdownButton #89905

@zeshuaro

Description

@zeshuaro

Description

I was trying to write tests to test for a DropdownButton. I made the tester to first tap on the DropdownButton to expand the options. But when I tried to make the tester to tap on one of the dropdown options, it failed to do so. It was giving error saying that the finder matched two widgets but realistically there should only be one that matches.

Steps to Reproduce

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

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const Center(
          child: MyStatefulWidget(),
        ),
      ),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  String dropdownValue = 'One';

  @override
  Widget build(BuildContext context) {
    return DropdownButton<String>(
      key: const Key('dropdownButton'),
      value: dropdownValue,
      onChanged: (String? newValue) {
        setState(() {
          dropdownValue = newValue!;
        });
      },
      items: <String>['One', 'Two', 'Free', 'Four']
          .map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      }).toList(),
    );
  }
}
widget_test.dart
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:hello_app/main.dart';

void main() {
  testWidgets('dropdown button', (WidgetTester tester) async {
    await tester.pumpWidget(MyApp());

    await tester.tap(find.byKey(const Key('dropdownButton')));
    await tester.pumpAndSettle();
    await tester.tap(find.text('Two'));

    final widget = find.byWidgetPredicate((widget) {
      return widget is DropdownButton && widget.value == 'Two';
    });
    expect(widget, findsOneWidget);
  });
}

The test would fail on the following line:

await tester.tap(find.text('Two'));

Expected results:

The test should pass

Actual results:

The test failed when the tester tried to tap on one of the dropdown options

The following assertion was thrown running a test:
The finder "2 widgets with text "Two" (ignoring offstage widgets): [Text("Two", dependencies:
[MediaQuery, DefaultTextStyle]), Text("Two", dependencies: [DefaultTextStyle, MediaQuery])]" (used
in a call to "tap()") ambiguously found multiple matching widgets. The "tap()" method needs a single
target.
Logs

Analyze

No issues found! (ran in 4.7s)

Doctor

[✓] Flutter (Channel stable, 2.2.3, on macOS 11.5.2 20G95 darwin-x64, locale en-AU)
    • Flutter version 2.2.3 at <scrubbed>
    • Framework revision f4abaa0735 (2 months ago), 2021-07-01 12:46:11 -0700
    • Engine revision 241c87ad80
    • Dart version 2.13.4

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at <scrubbed>
    • Platform android-30, build-tools 30.0.2
    • ANDROID_HOME = <scrubbed>
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.5.1, Build version 12E507
    • CocoaPods version 1.10.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)

[✓] VS Code (version 1.60.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.26.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-x64     • macOS 11.5.2 20G95 darwin-x64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 93.0.4577.63

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: error messageError messages from the Flutter frameworka: tests"flutter test", flutter_test, or one of our testsf: material designflutter/packages/flutter/material repository.found in release: 2.5Found to occur in 2.5found in release: 2.6Found to occur in 2.6frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions