gpt4 book ai didi

flutter 自动测试: Tap on a button don't work in drawer

转载 作者:IT王子 更新时间:2023-10-29 06:57:31 25 4
gpt4 key购买 nike

我正在尝试使用 flutter 进行一些 TDD,当测试运行时,如果按钮在抽屉中,则点击它不起作用。该按钮对普通用户来说工作得很好。

在下面的示例中,我们按下两个按钮,它们会在控制台中打印一条消息。以下是操作:
找到并点击脚手架中的按钮:OK
打开抽屉:OK
在抽屉中找到按钮:确定
点击抽屉按钮:没有任何反应

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');
});
}

最佳答案

我也遇到了类似的问题。

测试人员能够在抽屉中找到 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();

关于 flutter 自动测试: Tap on a button don't work in drawer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55628369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com