7

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

2
  • Could you show some code? What is flutter terminal? Is it process? Did you mean fetch the data from Terminal? And you might need to add a Windows tag or mentioned in the post, you showed powershell and Windows' path. Commented Sep 21, 2019 at 8:42
  • yes @Tokenyet, i wants to run this windows terminal command 'flutter screenshot' in my dart project in button click. Is it possible? Commented Sep 23, 2019 at 7:14

2 Answers 2

7

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 (e.g. 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.

Sign up to request clarification or add additional context in comments.

Comments

4

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.