Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter test not finding widget

Tags:

flutter

dart

I have this code that test if the button is on the screen:

void main() {
  testWidgets('Search for button', (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(
      home: HomeView(),
    ));
    final button = find.byType(BotaoRedirecionamentoInterno);
    expect(button, findsWidgets);
  });
}

Is working as I expected, but when I try this code that search for another type of button in the same view it doenst work:

void main() {
  testWidgets('Search for button', (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(
      home: HomeView(),
    ));
    final button = find.byType(BotaoRedirecionamentoExterno);
    expect(button, findsWidgets);
  });
}

I don't understand why because they are exactly one line below in the HomeView:

      Expanded(
        child: GridView.count(
          childAspectRatio: 6,
          padding: EdgeInsets.only(left: screenWidth * .05, right: screenWidth * .05),
          mainAxisSpacing: screenHeight * 0.02,
          crossAxisCount: 1,
          children: const [
            BotaoRedirecionamentoInterno(texto: "aaa", rota: '/bbbb'),
            BotaoRedirecionamentoExterno(texto: 'aaa', url: "bbb"),
            BotaoRedirecionamentoExterno(texto: "aaa", url: "bbb"),
            BotaoRedirecionamentoExterno(texto: "aaa", url: "bbb"),
            BotaoRedirecionamentoExterno(texto: "aaa", url: 'bbb'),
            BotaoRedirecionamentoExterno(texto: "aaa", url: "bbb"),
          ],
        ),
      )

this is the error message :

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
  Expected: at least one matching node in the widget tree
  Actual: _WidgetTypeFinder:<zero widgets with type "BotaoRedirecionamentoExterno" (ignoring
offstage widgets)>

I discovered that if I change the position inside the GridView to this:

 Expanded(
            child: GridView.count(
              childAspectRatio: 6,
              padding: EdgeInsets.only(left: screenWidth * .05, right: screenWidth * .05),
              mainAxisSpacing: screenHeight * 0.02,
              crossAxisCount: 1,
              children: const [
                BotaoRedirecionamentoExterno(texto: 'aaa', url: "bbb"),
                BotaoRedirecionamentoInterno(texto: "aaa", rota: '/bbbb'),

                BotaoRedirecionamentoExterno(texto: "aaa", url: "bbb"),
                BotaoRedirecionamentoExterno(texto: "aaa", url: "bbb"),
                BotaoRedirecionamentoExterno(texto: "aaa", url: 'bbb'),
                BotaoRedirecionamentoExterno(texto: "aaa", url: "bbb"),
              ],
            ),
          )

The test finds the BotaoRedirecionamentoInterno but wont find BotaoRedirecionamentoExterno, so my guess is that is only finding the first widget inside the GridView.

Anyone could help me figure out what is happening ?

like image 585
Filipe Gonçalves Ribeiro Costa Avatar asked Sep 18 '25 13:09

Filipe Gonçalves Ribeiro Costa


1 Answers

Use dragUntilVisible which scrolls the GridView in right direction, look below one it will scroll till the BotaoRedirecionamentoExterno widget, you can replace it with any Key or Text..

await tester.dragUntilVisible(
    find.byType(BotaoRedirecionamentoExterno), // what you want to find
    find.byType(GridView), // widget you want to scroll
    const Offset(-250, -0), // delta to move
);
like image 100
Jitesh Mohite Avatar answered Sep 20 '25 15:09

Jitesh Mohite