How we can automate image picker and file picker (file upload event) in flutter apps using flutter integration test??
I tried with patrol but it is not applicable for web ?? is there any other solution??
How we can automate image picker and file picker (file upload event) in flutter apps using flutter integration test??
I tried with patrol but it is not applicable for web ?? is there any other solution??
You Can Mock Method channel I'm assumeing that you are using Filepicker to pick file from your device
main.dart
TextButton(
onPressed: _pickFile,
child: Text('Open file picker'),
),
and define your _pickFile method to pick file.
Future<void> _pickFile() async {
final result = await FilePicker.platform.pickFiles();
final path = result?.files.single.path;
if (path != null) {
setState(() {
final file = io.File(path);
_files.add(file);
});
} else {
print('User canceled the picker');
}
}
And now in your test file mock filepicker package:
import 'package:anotherapp/main.dart' as mainapp;
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:path_provider/path_provider.dart';
import 'dart:io';
void main() async {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
mockFilePicker() {
const MethodChannel channelFilePicker =
MethodChannel('miguelruivo.flutter.plugins.filepicker');
TestDefaultBinaryMessengerBinding.instance?.defaultBinaryMessenger
.setMockMethodCallHandler(channelFilePicker,
(MethodCall methodCall) async {
final ByteData data = await rootBundle.load('assets/images/pic.png');
final Uint8List bytes = data.buffer.asUint8List();
final Directory tempDir = await getTemporaryDirectory();
final File file = await File(
'${tempDir.path}/tmp.tmp',
).writeAsBytes(bytes);
print(file.path);
return [
{
'name': "pic.png",
'path': file.path,
'bytes': bytes,
'size': bytes.lengthInBytes,
}
];
});
}
setUp(() {
mockFilePicker();
});
testWidgets(
'selects an image using a native file picker',
(tester) async {
mainapp.main();
await tester.pumpWidget(const mainapp.MyApp());
expect(find.text('Open file picker'), findsOneWidget);
await tester.tap(find.text('Open file picker'));
await tester.pumpAndSettle();
expect(find.byKey(const Key('image_0')), findsOneWidget);
await Future.delayed(Duration(seconds: 4));
},
);
}