Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flutter_test: Tap on a button don't work in drawer #31066

Closed
hawkbee1 opened this issue Apr 15, 2019 · 6 comments
Closed

flutter_test: Tap on a button don't work in drawer #31066

hawkbee1 opened this issue Apr 15, 2019 · 6 comments
Labels
a: tests "flutter test", flutter_test, or one of our tests

Comments

@hawkbee1
Copy link

I may have found an issue.

I'm trying to do some TDD with flutter and when the test is running, tapping on a button doesn't work if it's in a drawer. The button works perfectly fine with a normal user.

In the following example we press on two buttons which print a message in the console. Here's the actions :
locate and tap on a button in the scaffold: OK
open the drawer: OK
Locate button in drawer: OK
Tap on the drawer button: nothing happen

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

void main() {
  testWidgets('Test that drawer is apparing and we can click on button',
  (WidgetTester tester) async {
final scaffoldKey = GlobalKey<ScaffoldState>();

await tester.pumpWidget(new MaterialApp(
  title: 'Where Assistant',
  home: Scaffold(
    key: scaffoldKey,
    body: Column(
      children: <Widget>[
        Text('test text'),
        RaisedButton(
          onPressed: () {
            print('OK on main screen');
          },
          child: Icon(Icons.access_alarm),
        ),
      ],
    ),
    drawer: Drawer(
      // Add a ListView to the drawer. This ensures the user can scroll
      // through the options in the Drawer if there isn't enough vertical
      // space to fit everything.
      child: ListView(
        // Important: Remove any padding from the ListView.
        padding: EdgeInsets.zero,
        children: <Widget>[
          DrawerHeader(
            child: Text('Drawer Header'),
            decoration: BoxDecoration(
              color: Colors.blue,
            ),
          ),
          ListTile(
            title: Text('Item 2'),
            onTap: () {
              // Update the state of the app
              // ...
            },
          ),
          RaisedButton(
            onPressed: () {
              print('OK drawer');
            },
            child: Icon(Icons.add),
          )
        ],
      ),
    ),
  ),
));

await tester.pump();

expect(find.text('test text'), findsOneWidget);
expect(find.byIcon(Icons.access_alarm), findsOneWidget);
await tester.tap(find.byIcon(Icons.access_alarm));
expect(find.byIcon(Icons.add), findsNothing);

scaffoldKey.currentState.openDrawer();
await tester.pump(); // drawer should appear

expect(find.text('Item 2'), findsOneWidget);
expect(find.byIcon(Icons.add), findsOneWidget);
await tester.tap(find.byIcon(Icons.add));
print('end of test');
  });
}

I've posted about it on stackoverflow

@jonahwilliams jonahwilliams added the a: tests "flutter test", flutter_test, or one of our tests label Apr 15, 2019
@hawkbee1
Copy link
Author

I tried to investigate deeper by using methods getTopLeft, getBottomRight, getCenter and tapAt :
Scafold is between offset 0;0 and 800;600
Drawer is between offset -304;0 and 0;600
The button the test is trying to tap is between -164;237 and -140;261

With for loops I tapped everywhere and found a zone where it's working : same y coordinates as the button and x=0

@hawkbee1
Copy link
Author

finally found a workaround : just need to add some delay before tapping in the drawer. :-/

So I added
await tester.pump(const Duration(milliseconds: 100));
before tapping on buttons

took me too long to figure it.

@evaldobratti
Copy link

For those reaching here because tapping something in BottomAppBar, like a IconButton, is not working, the solution was the same.

    await tester.pumpWidget(widget);

    await tester.pump(Duration(milliseconds: 200));

    await tester.tap(find.widgetWithIcon(IconButton, Icons.touch_app));

It seems that, due to entering animation of the whole screen, the IconButton was out of the screen, and waiting a little make it show on the screen and tapping on it will now work.

Sorry for using your issue @hawkbee1, but as the solution was almost the same for a related problem, it may help some people.

@GreenAppers
Copy link

GreenAppers commented Sep 11, 2019

I'm still seeing that tester.tap() stops working. I don't know what precise conditions trigger it. But depending on the hierarchy of pumpWidget() it seems tap() will stop working (despite all pump and pumpAndSettle).

For now I went with this workaround:

    //await tester.tap(find.widgetWithText(RaisedGradientButton, l10n.send));
    RaisedGradientButton send = find.widgetWithText(RaisedGradientButton, l10n.send).evaluate().first.widget;
    send.onPressed();

@zeienko-vitalii
Copy link

Calling await tester.pump(Duration(milliseconds: 300)); with exactly 300ms before tapping, fix this bug
But still, it's a bug

@lock
Copy link

lock bot commented Apr 5, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@lock lock bot locked and limited conversation to collaborators Apr 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: tests "flutter test", flutter_test, or one of our tests
Projects
None yet
Development

No branches or pull requests

5 participants