diff --git a/.changeset/mobile-ios-tab-in-wrapper.md b/.changeset/mobile-ios-tab-in-wrapper.md new file mode 100644 index 000000000..3578c230c --- /dev/null +++ b/.changeset/mobile-ios-tab-in-wrapper.md @@ -0,0 +1,5 @@ +--- +'@e2e-dev/mobile': patch +--- + +On iOS, a tab bar or segmented control button that XCTest nests in an `Other` wrapper reads as a `tab`, as it does without the wrapper. XCTest reports the app that way once a document picker has closed, so `getByRole('tab', 'Search')` stopped finding a tab bar item for the rest of the session and the tree showed it as a `button`. diff --git a/docs/reference/mobile.mdx b/docs/reference/mobile.mdx index bc842fcdc..9211d77f8 100644 --- a/docs/reference/mobile.mdx +++ b/docs/reference/mobile.mdx @@ -1276,7 +1276,7 @@ to `combobox`. | `listitem` | `Cell`, `ListItem`, `TableCell`, `CollectionCell` | | | `row` | `TableRow` | | | `tablist` | `TabBar`, `TabGroup`, `SegmentedControl` | `TabLayout`, `TabWidget`, `BottomNavigationView` | -| `tab` | `Tab`, `TabBarItem`, `TabBarButton`, and a `Button` inside a `TabBar` or `SegmentedControl` | `TabLayout$TabView`, `BottomNavigationItemView` | +| `tab` | `Tab`, `TabBarItem`, `TabBarButton`, and a `Button` inside a `TabBar` or `SegmentedControl`, through any `Other` wrappers between them | `TabLayout$TabView`, `BottomNavigationItemView` | | `menu` | `Menu` | | | `menubar` | `MenuBar` | | | `menuitem` | `MenuItem`, `MenuBarItem`, `MenuButton` | | diff --git a/packages/mobile/src/nodes.ts b/packages/mobile/src/nodes.ts index 04377c4d6..f9bc4036b 100644 --- a/packages/mobile/src/nodes.ts +++ b/packages/mobile/src/nodes.ts @@ -399,6 +399,17 @@ function roleOf(kind: string, android: boolean, parentKind: string | undefined): return ROLE_MAP[kind] ?? kind; } +/** + * The kind of the nearest ancestor that is not an `Other` wrapper. XCTest can + * nest a tab bar's buttons in one, as it does for the whole app after a + * document picker closes, and the tab bar still says what they are. + */ +function containerKind(parent: ProjectedNode | undefined): string | undefined { + let node = parent; + while (node?.kind === 'other') node = node.parent; + return node?.kind; +} + /** * The role an app declared beside the platform's class: Android's heading * flag (React Native's `accessibilityRole="header"`), else the localized role @@ -701,7 +712,7 @@ export function projectSnapshot( // `Other`: it is the role the app declared, and iOS had no trait for it. // Only an `Other` carries that encoding; a native control's value is its own. const spelled = android || kind !== 'other' ? undefined : reactNativeValue(source.value); - const role = editableRole(spelled?.role ?? describedRole(source, echoedTexts.has(position)) ?? roleOf(kind, android, parent?.kind), source.editable); + const role = editableRole(spelled?.role ?? describedRole(source, echoedTexts.has(position)) ?? roleOf(kind, android, containerKind(parent)), source.editable); const editable = source.editable === true || (role !== undefined && TEXT_INPUT_ROLES.has(role)); const value = showsHint(source, android, editable) ? '' : spelled === undefined ? source.value : spelled.value; // iOS names a secure field by class; UIAutomator flags a password diff --git a/packages/mobile/tests/unit/nodes.test.ts b/packages/mobile/tests/unit/nodes.test.ts index 7d7a14dce..ba5048a41 100644 --- a/packages/mobile/tests/unit/nodes.test.ts +++ b/packages/mobile/tests/unit/nodes.test.ts @@ -226,6 +226,33 @@ describe('snapshot projection', () => { expect(project(webDriver).viewport).toEqual({ width: 390, height: 844 }); }); + it('reads a tab bar or segmented control button nested in an Other wrapper as a tab', () => { + const projected = project([ + { ref: 'e1', index: 0, depth: 0, type: 'Application', label: 'Rechka' }, + { ref: 'e2', index: 1, parentIndex: 0, depth: 1, type: 'TabBar', label: 'Tab Bar' }, + { ref: 'e3', index: 2, parentIndex: 1, depth: 2, type: 'Other', label: 'Recordings' }, + { ref: 'e4', index: 3, parentIndex: 2, depth: 3, type: 'Button', label: 'Recordings', selected: true }, + { ref: 'e5', index: 4, parentIndex: 2, depth: 3, type: 'Button', label: 'Search' }, + { ref: 'e6', index: 5, parentIndex: 0, depth: 1, type: 'SegmentedControl' }, + { ref: 'e7', index: 6, parentIndex: 5, depth: 2, type: 'Other' }, + { ref: 'e8', index: 7, parentIndex: 6, depth: 3, type: 'Button', label: 'Weekly' }, + { ref: 'e9', index: 8, parentIndex: 0, depth: 1, type: 'Other' }, + { ref: 'e10', index: 9, parentIndex: 8, depth: 2, type: 'Button', label: 'Save' }, + ]); + expect(projected.index.map((entry) => entry.node.role)).toEqual([ + 'application', + 'tablist', + 'other', + 'tab', + 'tab', + 'tablist', + 'other', + 'tab', + 'other', + 'button', + ]); + }); + it('maps iOS composite widgets onto the vocabulary roles a browser reports for them', () => { const projected = project([ { ref: 'e1', index: 0, depth: 0, type: 'Application', label: 'Shop' },