-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
get_text_command.dart
180 lines (145 loc) · 5.22 KB
/
get_text_command.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_driver/driver_extension.dart';
import 'package:flutter_driver/src/common/find.dart';
import 'package:flutter_driver/src/common/message.dart';
import 'package:flutter_test/flutter_test.dart';
class Base64URL {
static String encode(String str) {
String base64 = base64Encode(utf8.encode(str));
return base64.replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '');
}
static String decode(String str) {
String base64 = str.replaceAll('-', '+').replaceAll('_', '/');
// Add padding if needed
switch (base64.length % 4) {
case 2:
base64 += '==';
break;
case 3:
base64 += '=';
break;
}
return utf8.decode(base64Decode(base64));
}
}
class FinderHelper {
static SerializableFinder deserializeBase64(String base64Str) {
try {
// Decode base64 to JSON string
final jsonStr = Base64URL.decode(base64Str);
// Parse JSON
final dynamic finderData = json.decode(jsonStr);
if (finderData is! Map<String, dynamic>) {
throw Exception('finder is not valid');
}
if (!finderData.containsKey('finderType')) {
throw Exception('Invalid finder format: missing finderType');
}
final String finderType = finderData['finderType'] as String;
switch (finderType) {
case 'ByText':
return ByText(finderData['text'] as String);
case 'ByType':
return ByType(finderData['type'] as String);
case 'ByValueKey':
final keyType = finderData['keyValueType'] as String?;
final keyValue = finderData['keyValueString'] as String;
if (keyType == 'int') {
return ByValueKey(int.parse(keyValue));
}
return ByValueKey(keyValue);
case 'Ancestor':
// Parse of and matching which are JSON strings
final ofJson = json.decode(finderData['of'] as String);
final matchingJson = json.decode(finderData['matching'] as String);
return Ancestor(
of: deserializeBase64(Base64URL.encode(json.encode(ofJson))),
matching:
deserializeBase64(Base64URL.encode(json.encode(matchingJson))),
matchRoot: finderData['matchRoot'] == 'true',
firstMatchOnly: finderData['firstMatchOnly'] == 'true',
);
case 'Descendant':
final ofJson = json.decode(finderData['of'] as String);
final matchingJson = json.decode(finderData['matching'] as String);
return Descendant(
of: deserializeBase64(Base64URL.encode(json.encode(ofJson))),
matching:
deserializeBase64(Base64URL.encode(json.encode(matchingJson))),
matchRoot: finderData['matchRoot'] == 'true',
firstMatchOnly: finderData['firstMatchOnly'] == 'true',
);
default:
throw Exception('Unsupported finder type: $finderType');
}
} catch (e) {
throw Exception('Error deserializing finder: $e');
}
}
}
class GetTextCommandExtension extends CommandExtension {
String? getTextFromWidget(Text widget) {
return widget.data ?? widget.textSpan?.toPlainText();
}
@override
Future<Result> call(
Command command,
WidgetController prober,
CreateFinderFactory finderFactory,
CommandHandlerFactory handlerFactory) async {
final GetTextCommand dragCommand = command as GetTextCommand;
// Create finder for Text widget
final type = dragCommand.base64Element;
// decodeBase64 to json
SerializableFinder serializableFinder =
FinderHelper.deserializeBase64(type);
final Finder finder = finderFactory.createFinder(serializableFinder);
// Get the widget element
final Element element = prober.element(finder);
// if element is not a Text widget, return false with error
if (element.widget is! Text) {
return const GetTextResult(false, data: {
'errorCode': 'NOT_A_TEXT_WIDGET',
'error': 'Found element is not a Text widget'
});
}
final text = getTextFromWidget(element.widget as Text);
return text != null
? GetTextResult(true, data: {'text': text})
: const GetTextResult(false, data: {
'errorCode': 'NO_TEXT_CONTENT',
'error': 'No text content found'
});
}
@override
String get commandKind => 'getTextWithCommandExtension';
@override
Command deserialize(
Map<String, String> params,
DeserializeFinderFactory finderFactory,
DeserializeCommandFactory commandFactory) {
return GetTextCommand.deserialize(params);
}
}
class GetTextCommand extends Command {
final String base64Element;
GetTextCommand(this.base64Element);
@override
String get kind => 'getTextWithCommandExtension';
GetTextCommand.deserialize(Map<String, String> params)
: base64Element = params['findBy']!;
}
class GetTextResult extends Result {
final bool success;
final Map<String, dynamic>? data;
const GetTextResult(this.success, {this.data});
@override
Map<String, dynamic> toJson() {
return <String, dynamic>{
'success': success,
if (data != null) ...data!,
};
}
}