|
| 1 | +import 'package:appium_flutter_server/src/internal/flutter_element.dart'; |
| 2 | +import 'package:appium_flutter_server/src/utils/element_helper.dart'; |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | + |
| 6 | +class ElementSerializer { |
| 7 | + static Future<Map<String, dynamic>> serialize( |
| 8 | + Element element, { |
| 9 | + Set<Element>? visited, |
| 10 | + int depth = 0, |
| 11 | + }) async { |
| 12 | + visited ??= <Element>{}; |
| 13 | + if (visited.contains(element)) { |
| 14 | + return {}; |
| 15 | + } |
| 16 | + visited.add(element); |
| 17 | + |
| 18 | + final widget = element.widget; |
| 19 | + final renderObject = element.renderObject; |
| 20 | + final rect = renderObject?.paintBounds; |
| 21 | + |
| 22 | + final children = |
| 23 | + await _serializeChildren(element, visited: visited, depth: depth); |
| 24 | + |
| 25 | + final attributes = await _serializeAttributes(widget); |
| 26 | + final visualProperties = await _serializeVisualProperties(widget); |
| 27 | + final stateProperties = _serializeStateProperties(widget); |
| 28 | + |
| 29 | + return { |
| 30 | + 'type': widget.runtimeType.toString(), |
| 31 | + 'elementType': element.runtimeType.toString(), |
| 32 | + 'description': widget.toStringShort(), |
| 33 | + 'depth': depth, |
| 34 | + if (widget.key != null) 'key': widget.key.toString(), |
| 35 | + 'attributes': attributes, |
| 36 | + 'visual': visualProperties, |
| 37 | + 'state': stateProperties, |
| 38 | + if (rect != null) |
| 39 | + 'rect': { |
| 40 | + 'x': rect.left, |
| 41 | + 'y': rect.top, |
| 42 | + 'width': rect.width, |
| 43 | + 'height': rect.height, |
| 44 | + }, |
| 45 | + 'children': children, |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + static Future<List<Map<String, dynamic>>> _serializeChildren( |
| 50 | + Element element, { |
| 51 | + required Set<Element> visited, |
| 52 | + required int depth, |
| 53 | + }) async { |
| 54 | + final List<Map<String, dynamic>> children = []; |
| 55 | + final List<Future<Map<String, dynamic>>> childrenFutures = []; |
| 56 | + element.visitChildren((child) { |
| 57 | + childrenFutures.add(serialize(child, visited: visited, depth: depth + 1)); |
| 58 | + }); |
| 59 | + final resolvedChildren = await Future.wait(childrenFutures); |
| 60 | + for (final childJson in resolvedChildren) { |
| 61 | + if (childJson.isNotEmpty) { |
| 62 | + children.add(childJson); |
| 63 | + } |
| 64 | + } |
| 65 | + return children; |
| 66 | + } |
| 67 | + |
| 68 | + static Future<Map<String, String?>> _serializeAttributes( |
| 69 | + Widget widget) async { |
| 70 | + String? text; |
| 71 | + try { |
| 72 | + final flutterElement = FlutterElement.fromBy(find.byWidget(widget)); |
| 73 | + text = await ElementHelper.getText(flutterElement); |
| 74 | + } catch (_) { |
| 75 | + text = null; |
| 76 | + } |
| 77 | + |
| 78 | + String? semanticsLabel; |
| 79 | + String? tooltip; |
| 80 | + String? hintText; |
| 81 | + |
| 82 | + if (widget is Semantics) { |
| 83 | + semanticsLabel = widget.properties.label; |
| 84 | + } else if (widget is Tooltip) { |
| 85 | + tooltip = widget.message; |
| 86 | + } else if (widget is TextField) { |
| 87 | + hintText = widget.decoration?.hintText; |
| 88 | + } |
| 89 | + |
| 90 | + return { |
| 91 | + if (text?.isNotEmpty ?? false) 'text': text, |
| 92 | + if (semanticsLabel != null) 'semanticsLabel': semanticsLabel, |
| 93 | + if (tooltip != null) 'tooltip': tooltip, |
| 94 | + if (hintText != null) 'hintText': hintText, |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | + static Future<Map<String, dynamic>> _serializeVisualProperties( |
| 99 | + Widget widget) async { |
| 100 | + double? fontSize; |
| 101 | + String? fontWeight; |
| 102 | + String? fontStyle; |
| 103 | + String? backgroundColor; |
| 104 | + String? color; |
| 105 | + |
| 106 | + if (widget is Text) { |
| 107 | + final style = widget.style; |
| 108 | + fontSize = style?.fontSize; |
| 109 | + fontWeight = style?.fontWeight?.toString(); |
| 110 | + fontStyle = style?.fontStyle?.toString(); |
| 111 | + color = style?.color?.toString(); |
| 112 | + } else if (widget is Container) { |
| 113 | + if (widget.decoration is BoxDecoration) { |
| 114 | + backgroundColor = |
| 115 | + (widget.decoration as BoxDecoration).color?.toString(); |
| 116 | + } |
| 117 | + } else if (widget is ElevatedButton) { |
| 118 | + final backgroundColorProperty = widget.style?.backgroundColor; |
| 119 | + final resolvedColor = backgroundColorProperty?.resolve(<WidgetState>{}); |
| 120 | + if (resolvedColor != null) { |
| 121 | + backgroundColor = resolvedColor.toString(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return { |
| 126 | + if (fontSize != null) 'fontSize': fontSize, |
| 127 | + if (fontWeight != null) 'fontWeight': fontWeight, |
| 128 | + if (fontStyle != null) 'fontStyle': fontStyle, |
| 129 | + if (backgroundColor != null) 'backgroundColor': backgroundColor, |
| 130 | + if (color != null) 'color': color, |
| 131 | + }; |
| 132 | + } |
| 133 | + |
| 134 | + static Map<String, dynamic> _serializeStateProperties(Widget widget) { |
| 135 | + bool? enabled; |
| 136 | + bool? focused; |
| 137 | + bool? visible; |
| 138 | + |
| 139 | + if (widget is EditableText) { |
| 140 | + focused = widget.focusNode.hasFocus; |
| 141 | + } else if (widget is TextField) { |
| 142 | + enabled = widget.enabled ?? true; |
| 143 | + focused = widget.focusNode?.hasFocus ?? false; |
| 144 | + } else if (widget is Visibility) { |
| 145 | + visible = widget.visible; |
| 146 | + } |
| 147 | + |
| 148 | + return { |
| 149 | + if (enabled != null) 'enabled': enabled, |
| 150 | + if (focused != null) 'focused': focused, |
| 151 | + if (visible != null) 'visible': visible, |
| 152 | + }; |
| 153 | + } |
| 154 | +} |
0 commit comments