简体   繁体   中英

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

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

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 Key s. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM