我正在为我的flutter-web应用程序开发integration_test ....我遇到的tester.enterText()
方法不输入整数,双值,因为它的数据类型仅限于字符串类型。如果我TextField
只接受数字和keyboardType
属性设置为keyboardType: TextInputType.number
怎么办?
尝试将其类型转换为 String 类型,但结果TextField
不会接受任何非字符串值。下面的测试用例失败,但有以下异常:期望值类型String
,但得到类型之一int
可重复的样品:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _scaleTextController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
alignment: Alignment.center,
child: TextFormField(
controller: _scaleTextController,
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(r'^\d*\.?\d{0,6}'),
),
],
keyboardType: TextInputType.number,
autofocus: true,
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:hello_world/main.dart' as app;
void main() async {
group('Complete E2E Test', () {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
setUp(() {
app.main();
});
testWidgets('Hello World test', (WidgetTester tester) async {
final inputField = find.byType(TextField).first;
await tester.tap(inputField);
await tester.enterText(inputField, 2 as String);
});
});
}