createFinder function
Creates a Finder from matching
.
This function maps types onto Flutter finders.
Usage
Usually, you won't use this function directly. Instead, you'll use PatrolTester.call and PatrolFinder.$, like this:
patrolTest(
'increase counter text',
($) async {
// calls createFinder method under the hood
await $(Scaffold).$(#passwordTextField).enterText('my password');
},
);
What does this method accept?
The Finder that this method returns depends on the type of matching
.
Supported types of matching
are:
- Type, which translates to CommonFinders.byType, for example:
final finder = createFinder(Button);
- Key, which translates to CommonFinders.byKey, for example:
final finder = createFinder(Key('signInWithGoogle'));
- Symbol, which translates to CommonFinders.byKey, for example:
final finder = createFinder(#signInWithGoogle);
- String, which translates to CommonFinders.text, for example:
final finder = createFinder('Sign in with Google');
- Pattern, which translates to CommonFinders.textContaining. Example
Pattern is a RegExp.
final finder = createFinder(RegExp('.*in with.*'));
- IconData, which translates to CommonFinders.byIcon, for example:
final finder = createFinder(Icons.add);
- PatrolFinder, which returns a Finder that the PatrolFinder resolves
to, for example:
final finder = createFinder($(Text('Sign in with Google')));
- Finder, which simply returns the Finder itself.
final finder = createFinder(find.text('Log in'));
- Widget, which translates to CommonFinders.byWidget, for example:
final finder = createFinder(Text('some text'));
See also:
Implementation
@visibleForTesting
Finder createFinder(dynamic matching) {
if (matching is Type) {
return find.byType(matching);
}
if (matching is Key) {
return find.byKey(matching);
}
if (matching is Symbol) {
return find.byKey(Key(matching.name));
}
if (matching is String) {
return find.text(matching, findRichText: true);
}
if (matching is Pattern) {
return find.textContaining(matching, findRichText: true);
}
if (matching is IconData) {
return find.byIcon(matching);
}
if (matching is PatrolFinder) {
return matching.finder;
}
if (matching is Finder) {
return matching;
}
if (matching is Widget) {
return find.byWidget(matching);
}
throw ArgumentError(
'Argument of type ${matching.runtimeType} is not supported. '
'Supported types: Type, Key, Symbol, String, Pattern, IconData, '
'PatrolFinder, Finder, Widget',
);
}