-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathelement_helper.dart
More file actions
681 lines (614 loc) · 22.8 KB
/
element_helper.dart
File metadata and controls
681 lines (614 loc) · 22.8 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
import 'dart:io';
import 'package:appium_flutter_server/src/driver.dart';
import 'package:appium_flutter_server/src/exceptions/element_not_found_exception.dart';
import 'package:appium_flutter_server/src/exceptions/flutter_automation_error.dart';
import 'package:appium_flutter_server/src/internal/element_lookup_strategy.dart';
import 'package:appium_flutter_server/src/internal/flutter_element.dart';
import 'package:appium_flutter_server/src/logger.dart';
import 'package:appium_flutter_server/src/models/api/drag_drop.dart';
import 'package:appium_flutter_server/src/models/api/gesture.dart';
import 'package:appium_flutter_server/src/models/api/find_element.dart';
import 'package:appium_flutter_server/src/models/session.dart';
import 'package:appium_flutter_server/src/utils/flutter_settings.dart';
import 'package:appium_flutter_server/src/utils/ui_serialization/element_serializer.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
enum NATIVE_ELEMENT_ATTRIBUTES { enabled, displayed, clickable }
typedef WaitPredicate = Future<bool> Function();
class ElementHelper {
static Future<Finder> findElement(Finder by, {String? contextId}) async {
List<Finder> elementList =
await findElements(by, contextId: contextId, evaluatePresence: true);
final Finder? hitTestableElement =
getFirstHitTestableElementIfPresent(elementList);
if (hitTestableElement != null) {
log("The hitTestable element found $hitTestableElement");
return hitTestableElement;
}
log("The non-hitTestable element found ${elementList.first}");
return elementList.first;
}
static Finder? getFirstHitTestableElementIfPresent(List<Finder> elementList) {
for (Finder element in elementList) {
try {
if (element.hitTestable().tryEvaluate()) {
return element;
}
} catch (_) {}
}
return null;
}
static Future<List<Finder>> findElements(Finder by,
{String? contextId, bool evaluatePresence = false}) async {
Finder finder = by;
if (contextId != null) {
FlutterElement? parent = await FlutterDriver.instance
.getSessionOrThrow()!
.elementsCache
.get(contextId);
finder = find.descendant(of: parent.by, matching: by);
}
final FinderResult<Element> elements = finder.evaluate();
if (evaluatePresence) {
await waitForElementExist(FlutterElement.fromBy(finder),
timeout: Duration(
milliseconds: FlutterDriver.instance.settings
.getSetting(FlutterSettings.flutterElementWaitTimeout)));
if (elements.isEmpty) {
throw ElementNotFoundException("Unable to locate element");
}
}
List<Finder> elementList = [];
for (int i = 0; i < elements.length; i++) {
if (isVisibleOnAppScreen(elements.elementAt(i))) {
elementList.add(finder.at(i));
}
}
return elementList;
}
static bool isVisibleOnAppScreen(Element finderElement) {
if (finderElement.renderObject is! RenderBox) {
return true;
}
final WidgetTester tester = _getTester();
final widgetRect =
tester.getRect(find.byElementPredicate((e) => e == finderElement));
final allWidgets = tester.allWidgets.toList();
for (final widget in allWidgets) {
final elements = find.byWidget(widget).evaluate();
for (final element in elements) {
if (element.renderObject is RenderBox) {
final rootRect = tester.getRect(
find.byElementPredicate((e) => e == element),
);
return widgetRect.overlaps(rootRect);
}
}
}
return false;
}
static Future<void> click(FlutterElement element) async {
WidgetTester tester = _getTester();
await tester.tap(element.by);
await pumpAndTrySettle();
}
static Future<void> setText(FlutterElement element, String text) async {
WidgetTester tester = _getTester();
await tester.enterText(element.by, text);
await tester.pump(const Duration(milliseconds: 400));
}
static Future<void> gestureDoubleClick(GestureModel doubleClickModel) async {
await TestAsyncUtils.guard(() async {
final String? elementId = doubleClickModel.origin?.id;
WidgetTester tester = _getTester();
FlutterElement? element;
if (elementId == null && doubleClickModel.locator != null) {
Finder by = await locateElement(doubleClickModel.locator!);
element = FlutterElement.fromBy(by);
} else if (elementId != null) {
Session session = FlutterDriver.instance.getSessionOrThrow()!;
element = await session.elementsCache.get(elementId);
}
if (element == null) {
if (doubleClickModel.offset == null) {
throw ArgumentError(
"Double click offset coordinates must be provided "
"if element is not set");
}
await tester.tapAt(
Offset(doubleClickModel.offset!.x, doubleClickModel.offset!.y));
} else {
if (doubleClickModel.offset == null) {
await doubleClick(element);
} else {
Rect bounds = getElementBounds(element.by);
log("Click by offset $bounds");
await tester.tapAt(Offset(bounds.left + doubleClickModel.offset!.x,
bounds.top + doubleClickModel.offset!.y));
await tester.pump(kDoubleTapMinTime);
await tester.tapAt(Offset(bounds.left + doubleClickModel.offset!.x,
bounds.top + doubleClickModel.offset!.y));
await pumpAndTrySettle();
}
}
});
}
static Future<void> doubleClick(FlutterElement element) async {
WidgetTester tester = _getTester();
await tester.tap(element.by);
await tester.pump(kDoubleTapMinTime);
await tester.tap(element.by);
await pumpAndTrySettle();
}
static Future<void> longPress(GestureModel longPressModel) async {
return TestAsyncUtils.guard(() async {
final String? elementId = longPressModel.origin?.id;
WidgetTester tester = _getTester();
FlutterElement? element;
if (elementId == null && longPressModel.locator != null) {
Finder by = await locateElement(longPressModel.locator!);
element = FlutterElement.fromBy(by);
} else if (elementId != null) {
Session session = FlutterDriver.instance.getSessionOrThrow()!;
element = await session.elementsCache.get(elementId);
}
if (element == null) {
if (longPressModel.offset == null) {
throw ArgumentError("LongPress offset coordinates must be provided "
"if element is not set");
}
await tester.longPressAt(
Offset(longPressModel.offset!.x, longPressModel.offset!.y));
} else {
if (longPressModel.offset == null) {
await tester.longPress(element.by);
} else {
Rect bounds = getElementBounds(element.by);
log("Click by offset $bounds");
await tester.longPressAt(
Offset(longPressModel.offset!.x, longPressModel.offset!.y));
await pumpAndTrySettle();
}
}
});
}
static Future<String> getText(FlutterElement element) async {
String getElementTextRecursively(dynamic element, {Set<dynamic>? visited}) {
visited ??= <dynamic>{};
if (visited.contains(element)) {
return '';
}
visited.add(element);
final StringBuffer buffer = StringBuffer();
final widget = element.widget;
if (widget is Text) {
if (widget.data != null) {
buffer.write(widget.data);
} else if (widget.textSpan != null) {
buffer.write(widget.textSpan!.toPlainText());
}
} else if (widget is RichText) {
buffer.write(widget.text.toPlainText());
} else if (widget is EditableText) {
buffer.write(widget.controller.text);
} else if (widget is TextField) {
buffer.write(widget.controller?.value.text);
} else if (widget is ButtonStyleButton) {
buffer.write(getElementTextRecursively(widget.child, visited: visited));
}
if (element is RenderObjectElement) {
element.visitChildren((child) {
final childText = getElementTextRecursively(child, visited: visited);
buffer.write(childText);
});
}
return buffer.toString();
}
return getElementTextRecursively(element.by.evaluate().first);
}
static Future<dynamic> getAttribute(
FlutterElement element, String attribute) async {
if (NATIVE_ELEMENT_ATTRIBUTES.displayed.name == attribute) {
return element.by.evaluate().isNotEmpty;
} else if (NATIVE_ELEMENT_ATTRIBUTES.enabled.name == attribute) {
return _isElementEnabled(element);
} else if (NATIVE_ELEMENT_ATTRIBUTES.clickable.name == attribute) {
return _isElementClickable(element);
} else {
List<DiagnosticsNode> nodes = FlutterDriver.instance.tester
.widget(element.by)
.toDiagnosticsNode()
.getProperties();
List<DiagnosticsNode> data = [];
try {
data = FlutterDriver.instance.tester
.getSemantics(element.by)
.toDiagnosticsNode()
.getChildren()
.first
.getProperties();
FlutterDriver.instance.tester
.getSemantics(element.by)
.getSemanticsData()
.toDiagnosticsNode()
.getProperties()
.forEach((element) {
log("Semantics data : ${element.name} -> ${element.value}");
});
} catch (err) {
log(err);
}
data.addAll(nodes);
log("Available attributes for the element : ${element.by}");
for (DiagnosticsNode node in nodes) {
log("${node.name} -> ${node.value}");
}
log("Attribute in else block");
log(data);
try {
if (attribute == "all") {
Map<String, dynamic> values = {};
for (DiagnosticsNode node in data) {
log("${node.name.toString()} -> ${node.value.toString()}");
var value = node.name.toString();
values[value] = node.value.toString();
}
return values;
} else {
return data
.firstWhere((node) => node.name == attribute)
.value
.toString();
}
} catch (err) {
log(err);
return null;
}
}
}
static WidgetTester _getTester() {
return FlutterDriver.instance.tester;
}
static Future<Finder> locateElement(FindElementModel model,
{bool evaluatePresence = true}) async {
/// Support for backward compatibility
final String method = model.strategy.startsWith("-flutter")
? model.strategy
: '-flutter ${model.strategy.trim()}';
final String? contextId = model.context == "" ? null : model.context;
if (contextId == null) {
log('"method: $method, selector: ${model.selector}');
} else {
log('"method: $method, selector: ${model.selector}, contextId: $contextId');
}
// Get the strategy and create the finder
final strategy =
ElementLookupStrategy.values.firstWhere((val) => val.name == method);
final Finder by = await strategy.toFinder(model);
if (evaluatePresence) {
return await findElement(by, contextId: contextId);
} else {
return by;
}
}
static Rect getElementBounds(Finder by) {
var tester = _getTester();
return Rect.fromPoints(tester.getTopLeft(by), tester.getBottomRight(by));
}
static Size getElementSize(Finder by) {
var tester = _getTester();
return tester.getSize(by);
}
static String getElementName(Finder by) {
var tester = _getTester();
Element element = tester.element(by);
if (element is RenderObjectElement &&
element.renderObject.debugSemantics?.label != null) {
final String? semanticsLabel = element.renderObject.debugSemantics?.label;
if (semanticsLabel != null) {
return semanticsLabel.toString();
}
}
return element.widget.runtimeType.toString();
}
static DiagnosticsNode? _getElementPropertyNode(Finder by, String propertry) {
try {
return FlutterDriver.instance.tester
.widget(by)
.toDiagnosticsNode()
.getProperties()
.where((node) => node.name == propertry)
.first;
} catch (e) {
return null;
}
}
static dynamic _isElementEnabled(FlutterElement element) {
String attribute = NATIVE_ELEMENT_ATTRIBUTES.enabled.name;
DiagnosticsNode? enabledProperty =
_getElementPropertyNode(element.by, attribute);
if (enabledProperty == null) {
//For Button type elements, onPressed will be null if the element is disabled
DiagnosticsNode? onPressed =
_getElementPropertyNode(element.by, "onPressed");
return (onPressed == null || onPressed.value == null) ? "false" : "true";
} else {
return enabledProperty.value.toString();
}
}
static bool _isElementClickable(FlutterElement flutterElement) {
/*
* Reference taken from https://github.com/flutter/flutter/blob/master/packages/flutter_test/lib/src/controller.dart#L1880
* Method: _getElementPoint
*/
TestAsyncUtils.guardSync();
Finder finder = flutterElement.by;
WidgetTester tester = _getTester();
IntegrationTestWidgetsFlutterBinding binding =
FlutterDriver.instance.binding;
final Iterable<Element> elements = finder.evaluate();
final Element element = elements.single;
final RenderObject? renderObject = element.renderObject;
if (renderObject == null) {
log('The finder "$finder" found an element, but it does not have a corresponding render object. '
'Maybe the element has not yet been rendered?');
return false;
}
if (renderObject is! RenderBox) {
log('The finder "$finder" found an element whose corresponding render object is not a RenderBox (it is a ${renderObject.runtimeType}: "$renderObject"). '
'Unfortunately it only supports targeting widgets that correspond to RenderBox objects in the rendering.');
return false;
}
final RenderBox box = element.renderObject! as RenderBox;
final Offset location = box.localToGlobal(box.size.center(Offset.zero));
final FlutterView view = tester.viewOf(finder);
final HitTestResult result = HitTestResult();
binding.hitTestInView(result, location, view.viewId);
final bool found =
result.path.any((HitTestEntry entry) => entry.target == box);
if (!found) {
return false;
}
return true;
}
static Future<void> waitForElementExist(FlutterElement element,
{required Duration timeout}) async {
await waitFor(() async {
try {
return element.by.evaluate().isNotEmpty;
} catch (e) {
return false;
}
},
timeout: timeout,
errorMessage:
"Element with locator ${element.by.describeMatch(Plurality.one)} is not present in DOM");
}
static Future<void> waitForElementVisible(FlutterElement element,
{required Duration timeout}) async {
await waitFor(() async {
try {
return element.by.hitTestable().evaluate().isNotEmpty;
} catch (e) {
return false;
}
},
timeout: timeout,
errorMessage:
"Element with locator ${element.by.describeMatch(Plurality.one)} is not visible");
}
static Future<void> waitForElementAbsent(FlutterElement element,
{required Duration timeout}) async {
await waitFor(
() async {
try {
return element.by.evaluate().isEmpty;
} catch (e) {
return true;
}
},
timeout: timeout,
errorMessage:
"Element with locator ${element.by.describeMatch(Plurality.one)} not visible",
);
}
static Future<void> waitForElementEnable(FlutterElement element) async {
await waitFor(() async {
return bool.parse(await ElementHelper.getAttribute(
element, NATIVE_ELEMENT_ATTRIBUTES.enabled.name));
},
errorMessage:
"Element with locator ${element.by.describeMatch(Plurality.one)} not enabled");
}
static Future<void> waitForElementClickable(FlutterElement element) async {
await waitFor(() async {
return bool.parse(await ElementHelper.getAttribute(
element, NATIVE_ELEMENT_ATTRIBUTES.clickable.name));
},
errorMessage:
"Element with locator ${element.by.describeMatch(Plurality.one)} not clickable");
}
static Future<void> waitFor(
WaitPredicate predicate, {
String? errorMessage,
Duration timeout = const Duration(seconds: 20),
}) async {
WidgetTester tester = FlutterDriver.instance.tester;
final end = tester.binding.clock.now().add(timeout);
do {
if (tester.binding.clock.now().isAfter(end)) {
throw Exception(errorMessage != null
? '$errorMessage with ${timeout.inSeconds} seconds'
: 'Timed out waiting for condition');
}
if (Platform.isAndroid) {
await pumpAndTrySettle(timeout: const Duration(milliseconds: 200));
}
await Future.delayed(const Duration(milliseconds: 100));
} while (!(await predicate()));
}
static Future<void> dragAndDrop(DragAndDropModel model) async {
return TestAsyncUtils.guard(() async {
WidgetTester tester = _getTester();
final String sourceElementId = model.source.id;
final String targetElementId = model.target.id;
Session session = FlutterDriver.instance.getSessionOrThrow()!;
FlutterElement sourceEl =
await session.elementsCache.get(sourceElementId);
FlutterElement targetEl =
await session.elementsCache.get(targetElementId);
final Offset sourceElementLocation = tester.getCenter(sourceEl.by);
final Offset targetElementLocation = tester.getCenter(targetEl.by);
final TestGesture gesture =
await tester.startGesture(sourceElementLocation, pointer: 7);
await gesture.moveTo(targetElementLocation);
await tester.pump();
await gesture.up();
await tester.pump();
});
}
static Future<Finder> scrollUntilVisible({
required FindElementModel finder,
FindElementModel? scrollView,
double? delta,
AxisDirection? scrollDirection,
int? maxScrolls,
Duration? settleBetweenScrollsTimeout,
Duration? dragDuration,
}) async {
delta ??= FlutterDriver.instance.settings
.getSetting(FlutterSettings.flutterScrollDelta);
maxScrolls ??= FlutterDriver.instance.settings
.getSetting(FlutterSettings.flutterScrollMaxIteration);
WidgetTester tester = _getTester();
Finder scrollViewElement = scrollView != null
? await locateElement(scrollView)
: find.byType(Scrollable);
Finder elementToFind = await locateElement(finder, evaluatePresence: false);
await waitForElementExist(FlutterElement.fromBy(scrollViewElement),
timeout: Duration(
milliseconds: FlutterDriver.instance.settings
.getSetting('flutterElementWaitTimeout')));
AxisDirection direction;
if (scrollDirection == null) {
if (scrollViewElement.evaluate().first.widget is Scrollable) {
direction =
tester.firstWidget<Scrollable>(scrollViewElement).axisDirection;
} else {
direction = AxisDirection.down;
}
} else {
direction = scrollDirection;
}
return TestAsyncUtils.guard<Finder>(() async {
Offset moveStep;
switch (direction) {
case AxisDirection.up:
moveStep = Offset(0, delta!);
case AxisDirection.down:
moveStep = Offset(0, -delta!);
case AxisDirection.left:
moveStep = Offset(delta!, 0);
case AxisDirection.right:
moveStep = Offset(-delta!, 0);
}
scrollViewElement = scrollViewElement.hitTestable().first;
dragDuration ??= const Duration(milliseconds: 100);
settleBetweenScrollsTimeout ??= const Duration(seconds: 5);
var iterationsLeft = maxScrolls!;
while (iterationsLeft > 0 &&
elementToFind.hitTestable().evaluate().isEmpty) {
await tester.timedDrag(
scrollViewElement,
moveStep,
dragDuration!,
);
await pumpAndTrySettle(timeout: settleBetweenScrollsTimeout!);
iterationsLeft -= 1;
}
if (iterationsLeft <= 0) {
throw FlutterAutomationException("Wait timeout");
}
return elementToFind;
});
}
static Future<void> pumpAndTrySettle({
Duration duration = const Duration(milliseconds: 100),
EnginePhase phase = EnginePhase.sendSemanticsUpdate,
Duration timeout = const Duration(milliseconds: 200),
}) async {
return TestAsyncUtils.guard(() async {
try {
WidgetTester tester = _getTester();
await tester.pumpAndSettle(
duration,
phase,
timeout,
);
} on FlutterError catch (err) {
if (err.message == 'pumpAndSettle timed out') {
//This method ignores pumpAndSettle timeouts on purpose
} else {
rethrow;
}
}
});
}
static Future<Map<String, dynamic>> _serializeElement(
Element element, {
Set<Element>? visited,
int depth = 0,
}) =>
ElementSerializer.serialize(element, visited: visited, depth: depth);
static Future<List<Map<String, dynamic>>> getRenderTreeByType({
String? widgetType,
String? text,
String? key,
}) async {
final tester = _getTester();
final rootElement = tester.binding.rootElement;
if ((widgetType == null || widgetType.isEmpty) && rootElement != null) {
return [await _serializeElement(rootElement)];
}
if (rootElement == null) {
return [];
}
final matchedElements = <Element>[];
Future<void> search(Element element) async {
final widget = element.widget;
final typeMatches = widget.runtimeType.toString() == widgetType;
final keyMatches =
key == null || widget.key?.toString().contains(key) == true;
bool textMatches = text == null;
if (text != null &&
(widget is Text ||
widget is RichText ||
widget is EditableText ||
widget is TextField)) {
try {
final flutterElement = FlutterElement.fromBy(find.byWidget(widget));
final elementText = await ElementHelper.getText(flutterElement);
textMatches = elementText == text;
} catch (_) {
textMatches = false;
}
}
if (typeMatches && keyMatches && textMatches) {
matchedElements.add(element);
}
element.visitChildren(search);
}
await search(rootElement);
if (matchedElements.isEmpty) {
return [];
}
final results = <Map<String, dynamic>>[];
for (final element in matchedElements) {
results.add(await _serializeElement(element));
}
return results;
}
}