Is it possible to screenshot a widgetTest? - flutter

I am new to Flutter and I feel like if I could see what the widgetTest is "rendering", as a debug method, things could be a bit easier. I don't fully understand at the moment the mechanics of the widgetTest, but I am wondering if it is possible to capture an actual output render in the middle of the widgetTest.
I am talking about widgetTest not integration test.

I think the package golden_toolkit might be what you are looking for. Instead of using widgetTest you will be using testGoldens. And you can even compare screenshots if you want to ensure non regression in your UI using the provided method screenMatchesGolden.
Add in your pubspec.yaml:
#This is a development environment dependency only
dev-dependencies:
golden_toolkit: ^0.9.0
Code Sample:
testGoldens('MyWidget test', (tester) async {
await tester.pumpWidget(MyWidget());
await screenMatchesGolden(tester, 'my_widget-example');
});
Then run the following command to generate or regenerate your reference images:
flutter test --update-goldens
You can check the documentation about testGoldens here.

Related

What are the permissions necessary for an 'Always-On-Display' app?

I would like to make an AOD (always on display) app using Flutter.
I just want to know what are the permissions that my app requires in order to be functional.
I didn't start the proj. yet, therefore I don't even have any code to share.
Would start the proj. after I get my answers.
Always on display,you can achieve it by using this package
https://pub.dev/packages/wakelock
you can easily call like this and achieve your goal.like follows
import 'package:wakelock/wakelock.dart';
// ...
// The following line will enable the Android and iOS wakelock.
Wakelock.enable();
// The next line disables the wakelock again.
Wakelock.disable();

Flutter package share_plus : unexpected async-await behaviour

This is a Flutter plugin to share content from your Flutter app via the platform's share dialog.
Reference: https://pub.dev/packages/share_plus/install
I am using the following commands to share a picture with a caption or a text message only.
await Share.shareFiles([imagePath],text: text); //share picture + text
or
await Share.share(text,); //share text only
Here .share is a Future Function and I would like to await until have it done. This seems not happening. You can quickly test it by adding a print command after one of the above command as following.
await Share.share(text,);
print('this should be printed after the sharing process');
You will notice the text is printed in the console before the sharing has completed (the platform's share dialog is still on).
Am I missing something? Or you believe there is an issue within the package?
Many thanks in advance
This is expected behavior. Futures and async-await functions cannot provide a result immediately when it is started. You can read the docs or view this tutorial to learn how to use Futures. In your case, perhaps you should try:
await Share.share(text,).then((result){
print('this should be printed after the sharing process ${result}');
}
)

How to add a Flutter API/Module in my app?

I am trying to make an anime app using Flutter. For that I needed an REST API but I fortunately found a dart API. The problem is I cannot understand how to incorporate it in my app. This would really help me reduce the work load.
I am attaching the github link.
https://github.com/charafau/jikan-dart
If you are talking about how to include a package in your flutter Application, then you need to do the following:
add the package dependencies in the pubspec.yaml file
Get the package by running pub get
and then import the packages in your app using import
steps by step instructions also available here: https://dart.dev/guides/packages
also if you check the utilization of the API in the example code, you can find this example:
import 'package:jikan_dart/jikan_dart.dart';
main() async {
var jikanApi = JikanApi();
var top = await jikanApi.getTop(TopType.manga, page: 2);
print('result $top');
}

Testing Flutter apps with Cypress

Is it possible to test Flutter applications using the Cypress framework instead of using the built-in testing components that Flutter provides? If so, what are the pros & cons of both for Flutter testing if I know Cypress well?
Yes, technically, it's possible.
Here's a spec that passes with the basic flutter counter app:
describe('Counter app', () => {
beforeEach(() => {
cy.visit('http://localhost:_example_port_/#/')
cy.get('flt-semantics-placeholder').click({force: true})
})
it('Increments on button press', ()=>{
cy.get(`[aria-label="0"]`)
cy.get(`[aria-label="Increment"]`).click()
cy.get(`[aria-label="1"]`)
})
})
To explain, if you enable semantics by clicking on the hidden 'flt-semantic-placeholder' element, flutter adds a semantics layer used by screen readers. Widgets with tooltips and text are automatically assigned aria-labels, which Cypress can use to find and click on elements. (You can get more control over these labels using a Semantics Widget.)
I found this worked for the canvas renderer, but it crashed when I tried to run multiple test cases in the same run. So, use the html renderer, ie run flutter for the test with something like:
flutter run -d chrome --web-renderer html --web-port 3443
Okay, so clicking a button is pretty straightforward. What about other interactions?
Inputing text into a field:
Pretty straightforward. See this example.
Simulating a scroll event:
In short, no solution yet. See this markdown.
Simulating drag and drop:
Not likely. See this markdown.
Now for the pros and cons...
Pros:
Cypress is more user friendly than the integration testing flutter provides. Being able to easily hot reload tests, and being able to click around and inspect a live state of a failing test are nice features.
Cons:
Can't (yet) simulate scroll or drag.
Flutter web isn't too performant, and in particular the first load takes a long time. Tests are slow running.
Nothing indicates either the Flutter team or the Cypress team have any plans to support testing Flutter with Cypress.
As of this posting, no online guides or articles for testing Flutter with Cypress.
See also:
What are the tags, <flt-*> generated by Flutter for Web?
https://www.didierboelens.com/2018/07/semantics/

Passing command line arguments to a flutter app

Is package:args ArgParser compatible with flutter apps? I see on Github that it is used several times in some Flutter tools, but I'm not sure it's used in any of the sample apps.
If it is not compatible, is there another way to pass configuration options to my app at compile time as part of its build rule?
package:args operates on List<String>, which can come from anywhere. For example, I've used it in a browser app, in which the arguments came from Chrome's JS console. If you are OK with using the HostMessages API, then the following might work for you:
On Android, turn Intent.getExtras into List<String> and pass it to package:args. Similarly, this answer may help on the iOS side.