Question: What controls the bounds of the "render tree" when running widget tests (flutter_test)?
I ask because I am getting an error on very basic button where it can't find the widget because of its vertical offset being outside the bounds of the "render tree" which seems fixed at 800x600.
I get the message:
Warning: A call to tap() with finder "exactly one widget with text "More Info" (ignoring offstage widgets): Text("More Info", dependencies: [MediaQuery, DefaultTextStyle])" derived an Offset (Offset(400.0, 641.8)) that would not hit test on the specified widget. Maybe the widget is actually off-screen, or another widget is obscuring it, or the widget cannot receive pointer events. Indeed, Offset(400.0, 641.8) is outside the bounds of the root of the render tree, Size(800.0, 600.0). The finder corresponds to this RenderBox: RenderParagraph#1b6b1 relayoutBoundary=up27 The hit test result at that offset is: HitTestResult(HitTestEntry#b18dd(RenderView#408c3), HitTestEntry#a2393()) #0 WidgetController._getElementPoint (package:flutter_test/src/controller.dart:953:25) #1 WidgetController.getCenter (package:flutter_test/src/controller.dart:836:12) #2 WidgetController.tap (package:flutter_test/src/controller.dart:271:18) #3 main. (file:///Users/tommy/Repos/surveyapp/survey/test/widget_test.dart:99:18)
The size of the render tree in that error message is 800 x 600. (Not sure how that is set or why. It is curious to me that it is exactly 800x600? So, it is being set somewhere. Is it because I have a web project and it defaults to that size for some reason when running a test?). Any widget that is past 600 on the offset height can't be found in the test. The screen runs fine on iOS emulator and on Chrome under flutter web. When you use devtools, widget inspector, there are not any layout issues. It is just a button with ancestors column, padding, center, safearea and scaffold as part of a simple stateful widget page.
(I have had this happening on Fluter version 2.12 or higher.)
Thanks to the synchronous nature of Flutter tests, at times developers stumble upon a weird behaviour when they try to run asynchronous code beyond the context of pumped frames for widgets.
This method works the same way as the test () method and will basically execute your flutter code in a test environment. The main difference being that this test environment is made to build and interact with your widgets.
And you can now find all previous articles in a dedicated List: TDD in Flutter. This method works the same way as the test () method and will basically execute your flutter code in a test environment. The main difference being that this test environment is made to build and interact with your widgets.
The calls to testWidgets denote each test that we want to run. The first argument is the name and the second argument is a callback to execute our test. Our 'Header adds todo' test is testing that after we enter text into the header box, it properly calls the addTodo callback.
I had the same problem when tapping a button during my tests.
I don't know how to change the 800x600 size but I manage to find a way to scroll to the button, thanks to this answer https://stackoverflow.com/a/67990754/992201
Here's the final code :
final Finder buttonToTap = find.byKey(const Key('keyOfTheButton'));
await tester.dragUntilVisible(
buttonToTap, // what you want to find
find.byType(SingleChildScrollView), // widget you want to scroll
const Offset(0, 50), // delta to move
);
await tester.tap(buttonToTap);
await tester.pump();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With