|
| 1 | +import {util} from '@appium/support'; |
| 2 | + |
| 3 | +import {AX_OBJECT_TYPE} from './ax-deserialize.js'; |
| 4 | + |
| 5 | +/** |
| 6 | + * A handle to one element in the device's accessibility tree. |
| 7 | + * |
| 8 | + * `platformElement` is the daemon's opaque 20-byte identifier and is what makes |
| 9 | + * the handle usable in later calls — it has to be sent back verbatim. |
| 10 | + */ |
| 11 | +export interface AxElement { |
| 12 | + /** The daemon's opaque element identifier. */ |
| 13 | + platformElement: Buffer; |
| 14 | + /** The element's `accessibilityIdentifier`, when it has one. */ |
| 15 | + accessibilityIdentifier?: string; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * One attribute the daemon exposes for an element, e.g. `Label` or `Traits`. |
| 20 | + * |
| 21 | + * These are descriptors only — they carry no value. Reading a value takes a |
| 22 | + * second call (`deviceElement:valueForAttribute:`) passing the element and the |
| 23 | + * descriptor back, which is exactly what Xcode's Inspector does to fill each row |
| 24 | + * of its panel. |
| 25 | + */ |
| 26 | +export interface AxElementAttribute { |
| 27 | + /** Wire name, e.g. `TraitsHumanReadable`. Pass this back to read a value. */ |
| 28 | + name: string; |
| 29 | + /** Display name, e.g. `Traits`. */ |
| 30 | + humanReadableName: string; |
| 31 | + /** Whether the value can be written back. */ |
| 32 | + settable: boolean; |
| 33 | + /** Whether reading it performs an action rather than returning data. */ |
| 34 | + performsAction: boolean; |
| 35 | + /** Whether the daemon considers this internal/debug-only. */ |
| 36 | + isInternal: boolean; |
| 37 | + /** The daemon's value-type discriminator. */ |
| 38 | + valueType?: number; |
| 39 | + /** The raw descriptor, needed verbatim when asking for the value. */ |
| 40 | + raw: Record<string, unknown>; |
| 41 | +} |
| 42 | + |
| 43 | +/** A titled group of attributes — `Basic`, `Actions`, `Element`, `Hierarchy`. */ |
| 44 | +export interface AxInspectorSection { |
| 45 | + /** Stable identifier, e.g. `Basic_v1`. */ |
| 46 | + identifier: string; |
| 47 | + /** Display title, e.g. `Basic`. */ |
| 48 | + title: string; |
| 49 | + /** The attributes in this section. */ |
| 50 | + attributes: AxElementAttribute[]; |
| 51 | +} |
| 52 | + |
| 53 | +/** The inspector panel the device pushes when the focused element changes. */ |
| 54 | +export interface AxInspectedElement { |
| 55 | + /** What VoiceOver would announce, when the daemon provides it. */ |
| 56 | + spokenDescription?: string; |
| 57 | + /** The caption shown above the panel, when present. */ |
| 58 | + caption?: string; |
| 59 | + /** The panel's sections, in the order the device sent them. */ |
| 60 | + sections: AxInspectorSection[]; |
| 61 | +} |
| 62 | + |
| 63 | +/** Recovers a `Buffer` from a decoded `NS.data` blob. */ |
| 64 | +function toBuffer(value: unknown): Buffer | undefined { |
| 65 | + if (Buffer.isBuffer(value)) { |
| 66 | + return value; |
| 67 | + } |
| 68 | + if (util.isPlainObject(value)) { |
| 69 | + // The archiver decodes NSData into an index-keyed object. |
| 70 | + const bytes = Object.values(value as Record<string, unknown>).filter((b): b is number => typeof b === 'number'); |
| 71 | + if (bytes.length > 0) { |
| 72 | + return Buffer.from(bytes); |
| 73 | + } |
| 74 | + } |
| 75 | + return undefined; |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Parses a deserialized `AXAuditElement_v1`. |
| 80 | + * |
| 81 | + * The `_v1` suffixes are the daemon's own wire keys, not our assumption. A |
| 82 | + * future shape would carry different keys, so this returns `undefined` rather |
| 83 | + * than misreading one. |
| 84 | + */ |
| 85 | +export function toAxElement(value: unknown): AxElement | undefined { |
| 86 | + if (!util.isPlainObject(value)) { |
| 87 | + return undefined; |
| 88 | + } |
| 89 | + const fields = value as Record<string, unknown>; |
| 90 | + const platformValue = fields.PlatformElementValue_v1; |
| 91 | + const container = util.isPlainObject(platformValue) |
| 92 | + ? ((platformValue as Record<string, unknown>)['NS.data'] ?? platformValue) |
| 93 | + : undefined; |
| 94 | + const platformElement = toBuffer(container); |
| 95 | + if (!platformElement) { |
| 96 | + return undefined; |
| 97 | + } |
| 98 | + return { |
| 99 | + platformElement, |
| 100 | + accessibilityIdentifier: |
| 101 | + typeof fields.AccessibilityIdentifier_v1 === 'string' ? fields.AccessibilityIdentifier_v1 : undefined, |
| 102 | + }; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Rebuilds the serialized form the daemon expects when an element is passed |
| 107 | + * back, matching what Xcode's Inspector sends. |
| 108 | + */ |
| 109 | +export function serializeAxElement(element: AxElement): Record<string, unknown> { |
| 110 | + const value: Record<string, unknown> = { |
| 111 | + PlatformElementValue_v1: {ObjectType: 'passthrough', Value: element.platformElement}, |
| 112 | + }; |
| 113 | + if (element.accessibilityIdentifier !== undefined) { |
| 114 | + value.AccessibilityIdentifier_v1 = {ObjectType: 'passthrough', Value: element.accessibilityIdentifier}; |
| 115 | + } |
| 116 | + return { |
| 117 | + ObjectType: 'AXAuditElement_v1', |
| 118 | + Value: {ObjectType: 'passthrough', Value: value}, |
| 119 | + }; |
| 120 | +} |
| 121 | + |
| 122 | +function toAttribute(value: unknown): AxElementAttribute | undefined { |
| 123 | + if (!util.isPlainObject(value)) { |
| 124 | + return undefined; |
| 125 | + } |
| 126 | + const fields = value as Record<string, unknown>; |
| 127 | + const name = fields.AttributeNameValue_v1; |
| 128 | + if (typeof name !== 'string') { |
| 129 | + return undefined; |
| 130 | + } |
| 131 | + return { |
| 132 | + name, |
| 133 | + humanReadableName: typeof fields.HumanReadableNameValue_v1 === 'string' ? fields.HumanReadableNameValue_v1 : name, |
| 134 | + settable: fields.SettableValue_v1 === true, |
| 135 | + performsAction: fields.PerformsActionValue_v1 === true, |
| 136 | + isInternal: fields.IsInternal_v1 === true, |
| 137 | + valueType: typeof fields.ValueTypeValue_v1 === 'number' ? fields.ValueTypeValue_v1 : undefined, |
| 138 | + raw: stripTag(fields), |
| 139 | + }; |
| 140 | +} |
| 141 | + |
| 142 | +/** Drops the decoder's type tag so the object round-trips as the daemon sent it. */ |
| 143 | +function stripTag(fields: Record<string, unknown>): Record<string, unknown> { |
| 144 | + return Object.fromEntries(Object.entries(fields).filter(([key]) => key !== AX_OBJECT_TYPE)); |
| 145 | +} |
| 146 | + |
| 147 | +/** Rebuilds an attribute descriptor for the wire. */ |
| 148 | +export function serializeAxAttribute(attribute: AxElementAttribute): Record<string, unknown> { |
| 149 | + const value = Object.fromEntries( |
| 150 | + Object.entries(attribute.raw).map(([key, inner]) => [key, {ObjectType: 'passthrough', Value: inner}]), |
| 151 | + ); |
| 152 | + return { |
| 153 | + ObjectType: 'AXAuditElementAttribute_v1', |
| 154 | + Value: {ObjectType: 'passthrough', Value: value}, |
| 155 | + }; |
| 156 | +} |
| 157 | + |
| 158 | +/** Parses the payload of an inbound `hostInspectorCurrentElementChanged:`. */ |
| 159 | +export function toInspectedElement(value: unknown): AxInspectedElement { |
| 160 | + const fields = (util.isPlainObject(value) ? value : {}) as Record<string, unknown>; |
| 161 | + const rawSections = Array.isArray(fields.InspectorSectionsValue_v1) ? fields.InspectorSectionsValue_v1 : []; |
| 162 | + const sections: AxInspectorSection[] = []; |
| 163 | + for (const rawSection of rawSections) { |
| 164 | + if (!util.isPlainObject(rawSection)) { |
| 165 | + continue; |
| 166 | + } |
| 167 | + const section = rawSection as Record<string, unknown>; |
| 168 | + const rawAttributes = Array.isArray(section.ElementAttributesValue_v1) ? section.ElementAttributesValue_v1 : []; |
| 169 | + sections.push({ |
| 170 | + identifier: typeof section.IdentifierValue_v1 === 'string' ? section.IdentifierValue_v1 : '', |
| 171 | + title: typeof section.TitleValue_v1 === 'string' ? section.TitleValue_v1 : '', |
| 172 | + attributes: rawAttributes |
| 173 | + .map(toAttribute) |
| 174 | + .filter((attribute): attribute is AxElementAttribute => attribute !== undefined), |
| 175 | + }); |
| 176 | + } |
| 177 | + return { |
| 178 | + spokenDescription: |
| 179 | + typeof fields.SpokenDescriptionValue_v1 === 'string' ? fields.SpokenDescriptionValue_v1 : undefined, |
| 180 | + caption: typeof fields.CaptionTextValue_v1 === 'string' ? fields.CaptionTextValue_v1 : undefined, |
| 181 | + sections, |
| 182 | + }; |
| 183 | +} |
0 commit comments