forked from firka/flutter
[Flutter Driver] Extend getText to support more widgets (#48809)
This commit is contained in:
1
AUTHORS
1
AUTHORS
@@ -50,3 +50,4 @@ Jefferson Quesado <jeff.quesado@gmail.com>
|
||||
Mark Diener <rpzrpzrpz@gmail.com>
|
||||
Alek Åström <alek.astrom@gmail.com>
|
||||
Efthymios Sarpmpanis <e.sarbanis@gmail.com>
|
||||
Cédric Wyss <cedi.wyss@gmail.com>
|
||||
|
||||
@@ -545,9 +545,30 @@ class FlutterDriverExtension {
|
||||
Future<GetTextResult> _getText(Command command) async {
|
||||
final GetText getTextCommand = command as GetText;
|
||||
final Finder target = await _waitForElement(_createFinder(getTextCommand.finder));
|
||||
// TODO(yjbanov): support more ways to read text
|
||||
final Text text = target.evaluate().single.widget as Text;
|
||||
return GetTextResult(text.data);
|
||||
|
||||
final Widget widget = target.evaluate().single.widget;
|
||||
String text;
|
||||
|
||||
if (widget.runtimeType == Text) {
|
||||
text = (widget as Text).data;
|
||||
} else if (widget.runtimeType == RichText) {
|
||||
final RichText richText = widget as RichText;
|
||||
if (richText.text.runtimeType == TextSpan) {
|
||||
text = (richText.text as TextSpan).text;
|
||||
}
|
||||
} else if (widget.runtimeType == TextField) {
|
||||
text = (widget as TextField).controller.text;
|
||||
} else if (widget.runtimeType == TextFormField) {
|
||||
text = (widget as TextFormField).controller.text;
|
||||
} else if (widget.runtimeType == EditableText) {
|
||||
text = (widget as EditableText).controller.text;
|
||||
}
|
||||
|
||||
if (text == null) {
|
||||
throw UnsupportedError('Type ${widget.runtimeType.toString()} is currently not supported by getText');
|
||||
}
|
||||
|
||||
return GetTextResult(text);
|
||||
}
|
||||
|
||||
Future<SetTextEntryEmulationResult> _setTextEntryEmulation(Command command) async {
|
||||
|
||||
@@ -543,6 +543,75 @@ void main() {
|
||||
expect(await getOffset(OffsetType.center), const Offset(40 + (100 / 2), 30 + (120 / 2)));
|
||||
});
|
||||
|
||||
testWidgets('getText', (WidgetTester tester) async {
|
||||
await silenceDriverLogger(() async {
|
||||
final FlutterDriverExtension extension = FlutterDriverExtension((String arg) async => '', true);
|
||||
|
||||
Future<String> getTextInternal(SerializableFinder search) async {
|
||||
final Map<String, String> arguments = GetText(search, timeout: const Duration(seconds: 1)).serialize();
|
||||
final Map<String, dynamic> result = await extension.call(arguments);
|
||||
if (result['isError'] as bool) {
|
||||
return null;
|
||||
}
|
||||
return GetTextResult.fromJson(result['response'] as Map<String, dynamic>).text;
|
||||
}
|
||||
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(body:Column(
|
||||
key: const ValueKey<String>('column'),
|
||||
children: <Widget>[
|
||||
const Text('Hello1', key: ValueKey<String>('text1')),
|
||||
Container(
|
||||
height: 25.0,
|
||||
child: RichText(
|
||||
key: const ValueKey<String>('text2'),
|
||||
text: const TextSpan(text: 'Hello2')
|
||||
)
|
||||
),
|
||||
Container(
|
||||
height: 25.0,
|
||||
child: EditableText(
|
||||
key: const ValueKey<String>('text3'),
|
||||
controller: TextEditingController(text: 'Hello3'),
|
||||
focusNode: FocusNode(),
|
||||
style: const TextStyle(),
|
||||
cursorColor: Colors.red,
|
||||
backgroundCursorColor: Colors.black)
|
||||
),
|
||||
Container(
|
||||
height: 25.0,
|
||||
child: TextField(
|
||||
key: const ValueKey<String>('text4'),
|
||||
controller: TextEditingController(text: 'Hello4')
|
||||
)
|
||||
),
|
||||
Container(
|
||||
height: 25.0,
|
||||
child: TextFormField(
|
||||
key: const ValueKey<String>('text5'),
|
||||
controller: TextEditingController(text: 'Hello5')
|
||||
)
|
||||
),
|
||||
],
|
||||
))
|
||||
)
|
||||
);
|
||||
|
||||
expect(await getTextInternal(ByValueKey('text1')), 'Hello1');
|
||||
expect(await getTextInternal(ByValueKey('text2')), 'Hello2');
|
||||
expect(await getTextInternal(ByValueKey('text3')), 'Hello3');
|
||||
expect(await getTextInternal(ByValueKey('text4')), 'Hello4');
|
||||
expect(await getTextInternal(ByValueKey('text5')), 'Hello5');
|
||||
|
||||
// Check if error thrown for other types
|
||||
final Map<String, String> arguments = GetText(ByValueKey('column'), timeout: const Duration(seconds: 1)).serialize();
|
||||
final Map<String, dynamic> response = await extension.call(arguments);
|
||||
expect(response['isError'], true);
|
||||
expect(response['response'], contains('is currently not supported by getText'));
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('descendant finder', (WidgetTester tester) async {
|
||||
await silenceDriverLogger(() async {
|
||||
final FlutterDriverExtension extension = FlutterDriverExtension((String arg) async => '', true);
|
||||
|
||||
Reference in New Issue
Block a user