我正在尝试进行一些TDD测试,当测试运行时,如果它在抽屉中,则点击按钮不起作用。该按钮对于普通用户而言效果很好。
在下面的示例中,我们按下两个按钮,这些按钮将在控制台中打印一条消息。这是动作:
找到并点击支架中的按钮:确定
打开抽屉:确定
在抽屉中找到按钮:确定
点击抽屉按钮:什么都没发生
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');
});
}
答案 0 :(得分:1)
我还没有玩过WidgetTest
,但是我能够使用颤动驱动程序来敲击抽屉内的按钮。
test('test button tap in drawer', () async {
final SerializableFinder drawerOpenButton = find.byTooltip('Open navigation menu');
final SerializableFinder btn = find.byValueKey('firstButton');
await driver.waitFor(drawerOpenButton);
await driver.tap(drawerOpenButton);
await driver.waitFor(btn);
await driver.tap(btn);
print('tapped');
});
在main.dart
中,我将key
属性用于要点击的RaisedButton
。
答案 1 :(得分:0)
尝试此代码。.这是在单击抽屉时起作用的。.
class DrawerItem {
String title;
IconData icon;
DrawerItem(this.title, this.icon);
}
class MyHomeScreenStateFull extends StatefulWidget {
// here define drawer item
final drawerItems = [
new DrawerItem("Home", Icons.save),
new DrawerItem("Event", Icons.local_pizza)
];
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return MyHomeScreenState();
}
}
class MyHomeScreenState extends State<MyHomeScreenStateFull> {
int selectedDrawerIndex = 0;
// here define other screen.
getDrawerItemWidget(int pos) {
switch (pos) {
case 0:
return new FirstFragmentStateFull();
case 1:
return new SecondFragmentStateFull();
default:
return new Text("Error");
}
}
onSelectItem(int index) {
setState(() => selectedDrawerIndex = index);
Navigator.of(context).pop(); // close the drawer
}
@override
Widget build(BuildContext context) {
var drawerOptions = <Widget>[];
for (var i = 0; i < widget.drawerItems.length; i++) {
var d = widget.drawerItems[i];
drawerOptions.add(new ListTile(
leading: new Icon(d.icon),
title: new Text(
d.title,
style: TextStyle(fontSize: 15),
),
selected: i == selectedDrawerIndex,
onTap: () => onSelectItem(i),
));
}
return Scaffold(
appBar: AppBar(
title: new Text(widget.drawerItems[selectedDrawerIndex].title),
),
/*body: Center(
child: Text("Welcome To Home"),
),*/
drawer: Drawer(
child: Column(
children: <Widget>[
new UserAccountsDrawerHeader(
decoration: BoxDecoration(color: Colors.blue),
accountName: Text("Vikas Pandey "),
accountEmail: null,
currentAccountPicture: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.fill,
image: NetworkImage(
"https://via.placeholder.com/600/92c952"))),
),
),
new Column(
children: drawerOptions,
)
],
),
),
body: getDrawerItemWidget(selectedDrawerIndex),
);
}
}
答案 2 :(得分:0)
最终找到了一种解决方法:只需在点击抽屉中添加一些延迟即可。
所以我添加了
await tester.pump(const Duration(milliseconds: 100));
在点击按钮之前
答案 3 :(得分:0)
我也遇到了类似的问题。
测试人员能够在抽屉中找到FlatButton:
expect(find.byType(FlatButton), findsOneWidget);
tester.tap似乎不起作用:
await tester.tap(find.byType(FlatButton));
但是此Flutter issue中提到的解决方案确实有效:
FlatButton button = find.widgetWithText(FlatButton, 'TextExample').evaluate().first.widget;
button.onPressed();