我正在尝试使用在运行测试之前设置特定语言环境的integration_test包来编写一个测试。
await tester.binding.setLocale('en', 'US');
app.main();
await tester.idle();
await tester.pumpAndSettle();
// The app is still using the default locale of the phone...下面是我在驱动程序中的当前设置:
// Some adb commands for granting permissions...
print('Starting test.');
final FlutterDriver driver = await FlutterDriver.connect();
final String data = await driver.requestData(
null,
timeout: const Duration(minutes: 1),
);
await driver.close();
// Some more adb commands to revoke permissions.这似乎行不通。
我找到了this issue here,但它没有使用integration_test包,因此具有完全不同的设置。
发布于 2021-11-30 21:47:39
我能够通过将我的目标小部件包装在Localizations小部件中来本地化我的集成测试。类似于:
class WidgetTestWrapper extends StatelessWidget {
const WidgetTestWrapper({
Key? key,
required this.locale,
required this.child,
}) : super(key: key);
final Widget child;
final Locale locale;
static const localizationsDelegates = <LocalizationsDelegate>[
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
];
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Material(
child: Localizations(
locale: locale,
delegates: localizationsDelegates,
child: child,
),
),
);
}
}然后,我在对runApp的调用中传入测试所需的Locale。
例如,对于西班牙语US (es-US):
runApp(WidgetTestWrapper(locale: const Locale('es', 'US'), child: widgetUnderTest));https://stackoverflow.com/questions/63650780
复制相似问题