describeElement function

UiElement describeElement({
  1. required Element element,
  2. required String elementId,
  3. required String? testId,
  4. required AssertionEngine assertions,
  5. required WidgetTester tester,
  6. required bool includeBounds,
  7. bool useSemantics = true,
  8. bool registerRouteDependency = true,
})

Builds a semantic UiElement snapshot from a live Flutter Element.

Shared by FlutterUiObserver and live fingerprint revalidation so observe and act agree on observable state.

When useSemantics is false (diagnostic / report snapshots), skips WidgetTester.getSemantics label reads. When registerRouteDependency is false, visibility uses a dependency-free current-route check so hot-path dumps do not subscribe every element to _ModalScopeStatus.

Implementation

UiElement describeElement({
  required Element element,
  required String elementId,
  required String? testId,
  required AssertionEngine assertions,
  required WidgetTester tester,
  required bool includeBounds,
  bool useSemantics = true,
  bool registerRouteDependency = true,
}) {
  return RunnerBenchmark.sync('observer', 'describeElement', () {
    final type = resolveObservedWidgetType(element, testId: testId);
    final primary = type != 'widget' && testId != null && testId.isNotEmpty
        ? findPrimaryControlDescendant(element)
        : null;
    final semanticsSource = primary ?? element;
    final secure = looksSecure(semanticsSource);
    final bounds = boundsFor(element);
    final visible = registerRouteDependency
        ? assertions.isElementVisuallyActionable(element)
        : isElementGeometricallyVisible(element, tester);
    final offscreen = bounds != null && !inViewport(tester, bounds);
    // Icons: only report enabled for real icon buttons / compact chrome —
    // do not inherit `onTap` from a parent card/list-row InkWell.
    // Compact Ensemble Icon(onTap) → InkWell is the host: use readEnabled.
    // Leaf glyph under a compact Row (WifiCard eye-show) inherits that host.
    final bool? enabled;
    if (type == 'icon') {
      final iconEnabled = readIconButtonEnabled(semanticsSource);
      if (iconEnabled != null) {
        enabled = iconEnabled;
      } else if (_isGenericTapTarget(semanticsSource.widget)) {
        enabled = readEnabled(semanticsSource);
      } else {
        enabled = readEnabledFromCompactTapAncestor(semanticsSource);
      }
    } else if (type == 'toast') {
      enabled = null;
    } else if (type == 'card') {
      enabled = readCardEnabled(semanticsSource);
    } else {
      enabled = readEnabled(semanticsSource);
    }
    final checked = readChecked(semanticsSource);
    final selected =
        useSemantics ? readSelected(tester, semanticsSource) : null;
    var text = readText(semanticsSource);
    var label = useSemantics
        ? readControlLabel(semanticsSource, tester)
        : readControlLabelWithoutSemantics(semanticsSource);
    final hint = readHint(semanticsSource);
    // Icons rarely have Text; fall back to tooltip / semanticLabel only.
    if (type == 'icon') {
      if ((text == null || text.isEmpty) && (label == null || label.isEmpty)) {
        final iconName = readIconName(semanticsSource);
        if (iconName != null && iconName.isNotEmpty) {
          text = iconName;
        }
      }
      // Nested icons inherit a parent's merged semantics — drop those. Keep
      // labels on compact tappable chrome
      // (CloseAppButton) and on Semantics authored for that chrome.
      final trimmedLabel = label?.trim();
      if (trimmedLabel != null &&
          trimmedLabel.isNotEmpty &&
          (text == null || trimmedLabel != text.trim()) &&
          !_shouldKeepIconSemanticsLabel(semanticsSource, trimmedLabel)) {
        label = null;
      }
    }
    // Standalone text: never keep a merged semantics label (e.g. "Network name Guest_Network"
    // shared by both the label and value Text nodes).
    if (type == 'text') {
      final trimmedText = text?.trim();
      final trimmedLabel = label?.trim();
      if (trimmedLabel != null &&
          trimmedText != null &&
          trimmedLabel != trimmedText) {
        label = null;
      }
    }
    // Images / SVG / GIF / Lottie: surface source basename when there is no
    // semantic label (common for decorative Ensemble Image widgets).
    if (_isMediaObserveType(type)) {
      final desc = readMediaDescription(semanticsSource);
      // Nested AppIcon / SVG under a button inherits the parent row's a11y
      // label (WifiCard embeds `${addSpaces(password)}`). Never publish that
      // as the image title — prefer source basename, else nothing.
      if (hasPrimaryControlAncestor(semanticsSource)) {
        text = (desc != null && desc.isNotEmpty) ? desc : null;
        label = null;
      } else if ((text == null || text.isEmpty) &&
          desc != null &&
          desc.isNotEmpty) {
        text = desc;
      }
    }
    // Switches/checkboxes shouldn't inherit nearby label Text as "value".
    if ((type == 'switch' || type == 'toggle' || type == 'checkbox') &&
        checked != null) {
      text = null;
    }
    // textInput value is editable content only — never hint/label Text.
    if (type == 'textInput' &&
        hint != null &&
        text != null &&
        text.trim() == hint.trim()) {
      text = null;
    }
    final options = type == 'dropdown'
        ? readDropdownOptions(semanticsSource)
        : const <String>[];
    // Buttons / cards: surface the visible caption as [label] when semantics did
    // not provide one — agents author `target: { label, role: button }`.
    final effectiveLabel = _effectiveControlLabel(
      type: type,
      label: label,
      text: text,
    );
    final actions = actionsFor(
      type,
      secure: secure,
      enabled: enabled,
      testId: testId,
      text: text,
      label: effectiveLabel,
      offscreen: offscreen,
      hasBounds: bounds != null,
      hasScrollableAncestor: _hasScrollableAncestor(element),
    );
    // Interactable = can run a gesture/edit step on this node right now.
    final interactable = visible &&
        !offscreen &&
        enabled != false &&
        actions.any(isInteractionStep);

    return UiElement(
      elementId: elementId,
      testId: testId,
      type: type,
      role: inferSemanticRole(semanticsSource, type),
      label: effectiveLabel,
      text: text,
      hint: hint,
      options: options,
      state: UiElementState(
        exists: true,
        visible: visible,
        interactable: interactable,
        enabled: enabled,
        secure: secure,
        offscreen: offscreen,
        // Hit testing against overlays is not verified by the observer.
        obscured: null,
        selected: selected,
        checked: checked,
      ),
      bounds: includeBounds ? bounds : null,
      actions: actions,
    );
  });
}