使用Flutter驱动程序运行UI测试时,资产不会显示在iPhone模拟器上。当应用程序从VS Code以调试模式运行时,将显示它们。
我试图在flutter create -i swift -a kotlin --androidx --org com.example example
生成的示例项目中重现它,但没有成功。根据本文,我遵循了在两个应用程序中引入flutter_driver测试的完全相同的过程。不幸的是,在我的应用程序中没有显示图像,在示例应用程序中它们看起来还不错。我比较了project.pbxproj和pubspec.yaml文件,它们对我来说看起来不错。
例如我的pubspec.yaml:
dev_dependencies:
flutter_driver:
sdk: flutter
test: any
flutter_test:
sdk: flutter
screenshots:
flutter:
uses-material-design: true
assets:
- assets/lang/pl.json
- assets/images/
有什么可以帮助我解决这个问题的吗?我该怎么做才能调试问题?
当心,这是一个非常长的日志输出。
flutter drive --target=test_driver/app.dart --verbose
https://gist.github.com/orestesgaolin/861c4f191773ca152b400d97ced2daeb
flutter doctor -v
[✓] Flutter (Channel stable, v1.7.8+hotfix.4, on Mac OS X 10.14.6 18G87, locale pl-PL)
• Flutter version 1.7.8+hotfix.4 at /Users/dominik/Library/flutter
• Framework revision 20e59316b8 (6 weeks ago), 2019-07-18 20:04:33 -0700
• Engine revision fee001c93f
• Dart version 2.4.0
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at /Users/dominik/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-28, build-tools 28.0.3
• ANDROID_SDK_ROOT = /Users/dominik/Library/Android/sdk
• Java binary at: /Users/dominik/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/191.5791312/Android
Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 10.3)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 10.3, Build version 10G8
• CocoaPods version 1.7.5
[✓] iOS tools - develop for iOS devices
• ios-deploy 1.9.4
[✓] Android Studio (version 3.5)
• Android Studio at /Users/dominik/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/191.5791312/Android Studio.app/Contents
• Flutter plugin version 37.1.1
• Dart plugin version 183.6270
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
[✓] VS Code (version 1.37.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.3.0
[✓] Connected device (1 available)
• iPhone Xs Max • C1A5534A-AE3A-4FB3-95F6-993E351FF2D3 • ios • com.apple.CoreSimulator.SimRuntime.iOS-12-4 (simulator)
• No issues found!
答案 0 :(得分:5)
因此解决方案是创建单独的main_test.dart
文件,将整个应用程序包装在DefaultAssetBundle
中,如下所示:
runApp(DefaultAssetBundle(
bundle: TestAssetBundle(),
child: App(),
));
和TestAssetBundle
就像这样:
class TestAssetBundle extends CachingAssetBundle {
@override
Future<String> loadString(String key, {bool cache = true}) async {
final ByteData data = await load(key);
if (data == null) throw FlutterError('Unable to load asset');
return utf8.decode(data.buffer.asUint8List());
}
@override
Future<ByteData> load(String key) async => rootBundle.load(key);
}
在app.dart
中,您必须引用正确的文件:
import 'package:flutter_driver/driver_extension.dart';
import 'package:playoo_league_player/main_test.dart' as app;
void main() {
// This line enables the extension.
enableFlutterDriverExtension();
// Call the `main()` function of the app, or call `runApp` with
// any widget you are interested in testing.
app.main();
}
完整说明可在这里找到:https://medium.com/@sardox.vl/flutter-test-and-randomly-missing-assets-in-goldens-ea959cdd336a