buildSelectorChainForNode function

List<String> buildSelectorChainForNode(
  1. SnapshotNode node,
  2. String platform, {
  3. String? action,
})

Build selector chain alternatives for a node.

Implementation

List<String> buildSelectorChainForNode(
  SnapshotNode node,
  String platform, {
  String? action,
}) {
  final chain = <String>[];
  final role = _normalizeType(node.type ?? '');
  final id = _normalizeSelectorText(node.identifier);
  final label = _normalizeSelectorText(node.label);
  final value = _normalizeSelectorText(node.value);
  final text = _normalizeSelectorText(extractNodeText(node));
  final requireEditable = action == 'fill';

  if (id != null) {
    chain.add('id=${_quoteSelectorValue(id)}');
  }
  if (role.isNotEmpty && label != null) {
    if (requireEditable) {
      chain.add(
        'role=${_quoteSelectorValue(role)} label=${_quoteSelectorValue(label)} editable=true',
      );
    } else {
      chain.add(
        'role=${_quoteSelectorValue(role)} label=${_quoteSelectorValue(label)}',
      );
    }
  }
  if (label != null) {
    if (requireEditable) {
      chain.add('label=${_quoteSelectorValue(label)} editable=true');
    } else {
      chain.add('label=${_quoteSelectorValue(label)}');
    }
  }
  if (value != null) {
    if (requireEditable) {
      chain.add('value=${_quoteSelectorValue(value)} editable=true');
    } else {
      chain.add('value=${_quoteSelectorValue(value)}');
    }
  }
  if (text != null && text != label && text != value) {
    if (requireEditable) {
      chain.add('text=${_quoteSelectorValue(text)} editable=true');
    } else {
      chain.add('text=${_quoteSelectorValue(text)}');
    }
  }
  if (role.isNotEmpty &&
      requireEditable &&
      !chain.any((entry) => entry.contains('editable=true'))) {
    chain.add('role=${_quoteSelectorValue(role)} editable=true');
  }

  final deduped = _uniqueStrings(chain);
  if (deduped.isEmpty && role.isNotEmpty) {
    if (requireEditable) {
      deduped.add('role=${_quoteSelectorValue(role)} editable=true');
    } else {
      deduped.add('role=${_quoteSelectorValue(role)}');
    }
  }
  if (deduped.isEmpty) {
    final visible = isNodeVisible(node);
    if (visible) deduped.add('visible=true');
  }
  return deduped;
}