how should i find a global key in flutter driver test?
Asked Answered
P

1

6

Normally I put Key: ValueKey in a flutter element that I want to find in my test cases for testing, however this particular element already has a globalKey. So, what should I use to find the globalkey used in the element?

value key

key: ValueKey('nameField')

In test case => final nameField = find.byValueKey('nameField')

global key

final LabeledGlobalKey<FormFieldState<String>> _passwordField = LabeledGlobalKey<FormFieldState<String>>("passwordFieldKey")

key: _passwordField

In test case => final passwordField = find.???('???')

Pickett answered 10/6, 2021 at 8:28 Comment(0)
P
1

I don't think it's possible to find a GlobalKeys (that I used to find the position of certain widgets), but what you can find is Keys. So what I did is: Wrap the widget that I had to find in my test by a Container Widget that had that GlobalKey.

GlobalKey someKeyName = GlobalKey();

Container(
    key: someKeyName,
    child: CustomButton(
      key: Key('Key that I would look for in my Integration Test'),
      child: Text('Press Me'),
    ),
);

I am not sure of what LabeledGlobalKey<FormFieldState<String>> does, but hope this work around will help you solve your issue.

Puentes answered 30/6, 2021 at 20:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.