4

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.???('???')

natasha
  • 41
  • 3

1 Answers1

0

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.

Merouane T.
  • 746
  • 1
  • 10
  • 14