简体   繁体   中英

How to execute flutter terminal commands in dart code

I want to automate the testing for my project. I need to take screenshot and compare with other screenshot. Flutter command will take screenshot and save it to my directory. so i want to execute that command in dart instead of terminal.

I got problem in taking screenshot had already used some packages like screenshots etc.. will not works.

PS D:\flut-237-screenshot\flutter-charts\flutter_charts\flutter_charts_testbed> flutter screenshot Screenshot written to flutter_02.png (2797kB).

I expect that this command should be run using dart code instead of in terminal

There is a package called process_run which allows you to call terminal commands from your dart code. Here is the link for the package: https://pub.dev/packages/process_run For example, if you want to execute the command that tells you your dart version, you can use the package in the following way:

await run('dart', ['--version'], verbose: true);

Dart's io package has the Process.run command that allows you run terminal commands from Dart:

Process.run

In your case of taking screenshots of your app, you'd be doing something like:

import 'dart:io';

await Process.run('flutter', ['screenshot', '-o', 'image.jpg']);

If you're wanting to generate screenshots more directly from setting up integration tests, which is much faster than invoking flutter screenshot via a terminal command, here are some helpful references explaining how to take app snapshots via flutter's integration test framework:

https://medium.com/flutter-community/testing-flutter-ui-with-flutter-driver-c1583681e337

https://blog.codemagic.io/flutter-automated-screenshot-testing/

https://dev.to/mjablecnik/take-screenshot-during-flutter-integration-tests-435k

https://medium.com/@nocnoc/automated-screenshots-for-flutter-f78be70cd5fd

The limitation of taking screenshots this way, however, is that only the UI of your app will be captured, nothing else which might present outside of your app (eg local and push notifications, reminder alerts based on the OS's clock, calendar, etc, which your app may generate.)

On the other hand, using Process.run to execute flutter screenshot would be able to capture everything on the phone display, including notifications, system-based alerts, etc. Getting the timing right can be a little tricky, though, because it takes substantially longer for the terminal command to completely capture and save the screenshot. You'd still want to execute this in the context of an integration test driver in order to avoid system permissions issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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