Add semantic label finders (#29342)

* Add semantic label finders
This commit is contained in:
Dan Field
2019-03-18 20:32:03 -07:00
committed by GitHub
parent 79e3bf4ad3
commit 5e27ebbe8d
6 changed files with 206 additions and 0 deletions

View File

@@ -135,6 +135,7 @@ abstract class SerializableFinder {
case 'ByType': return ByType.deserialize(json);
case 'ByValueKey': return ByValueKey.deserialize(json);
case 'ByTooltipMessage': return ByTooltipMessage.deserialize(json);
case 'BySemanticsLabel': return BySemanticsLabel.deserialize(json);
case 'ByText': return ByText.deserialize(json);
case 'PageBack': return PageBack();
}
@@ -164,6 +165,44 @@ class ByTooltipMessage extends SerializableFinder {
}
}
/// A Flutter Driver finder that finds widgets by semantic label.
///
/// If the [label] property is a [String], the finder will try to find an exact
/// match. If it is a [RegExp], it will return true for [RegExp.hasMatch].
class BySemanticsLabel extends SerializableFinder {
/// Creates a semantic label finder given the [label].
BySemanticsLabel(this.label);
/// A [Pattern] matching the [Semantics.properties.label].
///
/// If this is a [String], it will be treated as an exact match.
final Pattern label;
@override
final String finderType = 'BySemanticsLabel';
@override
Map<String, String> serialize() {
if (label is RegExp) {
final RegExp regExp = label;
return super.serialize()..addAll(<String, String>{
'label': regExp.pattern,
'isRegExp': 'true',
});
} else {
return super.serialize()..addAll(<String, String>{
'label': label,
});
}
}
/// Deserializes the finder from JSON generated by [serialize].
static BySemanticsLabel deserialize(Map<String, String> json) {
final bool isRegExp = json['isRegExp'] == 'true';
return BySemanticsLabel(isRegExp ? RegExp(json['label']) : json['label']);
}
}
/// A Flutter Driver finder that finds widgets by [text] inside a [Text] or
/// [EditableText] widget.
class ByText extends SerializableFinder {

View File

@@ -939,6 +939,9 @@ class CommonFinders {
/// Finds widgets with a tooltip with the given [message].
SerializableFinder byTooltip(String message) => ByTooltipMessage(message);
/// Finds widgets with the given semantics [label].
SerializableFinder bySemanticsLabel(Pattern label) => BySemanticsLabel(label);
/// Finds widgets whose class name matches the given string.
SerializableFinder byType(String type) => ByType(type);

View File

@@ -135,6 +135,7 @@ class FlutterDriverExtension {
_finders.addAll(<String, FinderConstructor>{
'ByText': (SerializableFinder finder) => _createByTextFinder(finder),
'ByTooltipMessage': (SerializableFinder finder) => _createByTooltipMessageFinder(finder),
'BySemanticsLabel': (SerializableFinder finder) => _createBySemanticsLabelFinder(finder),
'ByValueKey': (SerializableFinder finder) => _createByValueKeyFinder(finder),
'ByType': (SerializableFinder finder) => _createByTypeFinder(finder),
'PageBack': (SerializableFinder finder) => _createPageBackFinder(),
@@ -262,6 +263,22 @@ class FlutterDriverExtension {
}, description: 'widget with text tooltip "${arguments.text}"');
}
Finder _createBySemanticsLabelFinder(BySemanticsLabel arguments) {
return find.byElementPredicate((Element element) {
if (element is! RenderObjectElement) {
return false;
}
final String semanticsLabel = element.renderObject?.debugSemantics?.label;
if (semanticsLabel == null) {
return false;
}
final Pattern label = arguments.label;
return label is RegExp
? label.hasMatch(semanticsLabel)
: label == semanticsLabel;
}, description: 'widget with semantic label "${arguments.label}"');
}
Finder _createByValueKeyFinder(ByValueKey arguments) {
switch (arguments.keyValueType) {
case 'int':

View File

@@ -158,6 +158,35 @@ void main() {
});
});
group('BySemanticsLabel', () {
test('finds by Semantic label using String', () async {
when(mockIsolate.invokeExtension(any, any)).thenAnswer((Invocation i) {
expect(i.positionalArguments[1], <String, String>{
'command': 'tap',
'timeout': _kSerializedTestTimeout,
'finderType': 'BySemanticsLabel',
'label': 'foo',
});
return makeMockResponse(<String, dynamic>{});
});
await driver.tap(find.bySemanticsLabel('foo'), timeout: _kTestTimeout);
});
test('finds by Semantic label using RegExp', () async {
when(mockIsolate.invokeExtension(any, any)).thenAnswer((Invocation i) {
expect(i.positionalArguments[1], <String, String>{
'command': 'tap',
'timeout': _kSerializedTestTimeout,
'finderType': 'BySemanticsLabel',
'label': '^foo',
'isRegExp': 'true',
});
return makeMockResponse(<String, dynamic>{});
});
await driver.tap(find.bySemanticsLabel(RegExp('^foo')), timeout: _kTestTimeout);
});
});
group('tap', () {
test('requires a target reference', () async {
expect(driver.tap(null), throwsA(isInstanceOf<DriverError>()));