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 also ran into a similar issue.
The tester was able to find the FlatButton in the drawer:
expect(find.byType(FlatButton), findsOneWidget);
tester.tap did not appear to be working:
await tester.tap(find.byType(FlatButton));
But the solution mentioned in this Flutter issue did work:
FlatButton button = find.widgetWithText(FlatButton, 'TextExample').evaluate().first.widget;
button.onPressed();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With