简体   繁体   English

如何在 Flutter Driver 中测试 ImagePicker?

[英]How to test ImagePicker in Flutter Driver?

In Flutter integration testing, how can we handle ImagePicker?在 Flutter 集成测试中,我们如何处理 ImagePicker? as well as other platform related plugins?以及其他平台相关的插件?

Finally, I got a solution for this question.最后,我得到了这个问题的解决方案。 this is the code in app.dart:这是 app.dart 中的代码:

prepare an image file in assets, for example: images/sample.png.在资产中准备一个图像文件,例如:images/sample.png。

import 'dart:io';
import 'dart:typed_data';
import 'package:path_provider/path_provider.dart';

import 'package:image_picker_test/main.dart' as app;
import 'package:flutter_driver/driver_extension.dart';
import 'package:flutter/services.dart';
void main() {
  // This line enables the extension.
  enableFlutterDriverExtension();

  const MethodChannel channel =
  MethodChannel('plugins.flutter.io/image_picker');

  channel.setMockMethodCallHandler((MethodCall methodCall) async {
    ByteData data = await rootBundle.load('images/sample.png');
    Uint8List bytes = data.buffer.asUint8List();
    Directory tempDir = await getTemporaryDirectory();
    File file = await File('${tempDir.path}/tmp.tmp', ).writeAsBytes(bytes);
    print(file.path);
    return file.path;
  });


  app.main();
}

Frank Yan's solution works fine. Frank Yan 的解决方案工作正常。 Basically he is using MethodChannel as an interceptor of request to ImagePicker基本上他使用 MethodChannel 作为对 ImagePicker 请求的拦截器

MethodChannel('plugins.flutter.io/image_picker')

In this part he defines which plugin has to be mocked在这一部分中,他定义了必须模拟哪个插件

channel.setMockMethodCallHandler((MethodCall methodCall) async {
    ByteData data = await rootBundle.load('images/sample.png');
    Uint8List bytes = data.buffer.asUint8List();
    Directory tempDir = await getTemporaryDirectory();
    File file = await File('${tempDir.path}/tmp.tmp', ).writeAsBytes(bytes);
    print(file.path);
    return file.path;
  });

This function defines what has to be returned from request to image picker plugin.此函数定义了从请求到图像选择器插件必须返回的内容。 So your program will do these actions each time when user uses image picker.因此,每次用户使用图像选择器时,您的程序都会执行这些操作。 Right here it will just return the image from 'images/sample.png'.在这里,它只会从“images/sample.png”返回图像。 In my case I had to put image into assets/image.png in my project root.就我而言,我必须将图像放入项目根目录中的 assets/image.png 中。 Anyway you can mock any plugin like that.无论如何,您可以模拟任何这样的插件。 I had to also mock cropper plugin which is called after image picker ends it's job.我还必须模拟裁剪器插件,该插件在图像选择器结束工作后调用。

**Note: **mocking is not the best way to go with e2e or as they are called in flutter integration tests. **注意:**mocking 不是 e2e 的最佳方式,也不是 Flutter 集成测试中调用的方式。 I used it just because there is no workaround (I couldn't find it) at the moment and I am blocked with step of picture upload in my scenario.我使用它只是因为目前没有解决方法(我找不到它)并且我在我的场景中被图片上传步骤阻止。 So be careful with using such approach.所以要小心使用这种方法。

You don't need to call this function anywhere in test.你不需要在测试中的任何地方调用这个函数。 Your app will run with mocked plugin that we define in MethodChannel constuctor MethodChannel('');您的应用程序将使用我们在 MethodChannel 构造函数 MethodChannel(''); 中定义的模拟插件运行;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM