diff --git a/.changeset/mobile-stale-transition-ref.md b/.changeset/mobile-stale-transition-ref.md new file mode 100644 index 0000000000..c9f9ac3a03 --- /dev/null +++ b/.changeset/mobile-stale-transition-ref.md @@ -0,0 +1,5 @@ +--- +"@e2e-dev/mobile": patch +--- + +Re-resolve a locator when its target changes during the transition wait, instead of tapping a stale native reference that may now identify another control. diff --git a/docs/reference/mobile.mdx b/docs/reference/mobile.mdx index 0d5fd73f91..486a1ce2e9 100644 --- a/docs/reference/mobile.mdx +++ b/docs/reference/mobile.mdx @@ -234,6 +234,8 @@ readonly transition?: number | undefined; mobile({ platform: 'ios', transition: 800 }); // default: 500 ``` +If the control changes during the wait, the action finds it again within its timeout before acting. + `back`, `home`, `alert`, `dismissKeyboard`, `setOrientation`, and `fold` on [`device`](#device) change the screen, so they count as actions for this budget the way a tap does. A control that arrives with the screen they reveal diff --git a/packages/mobile/src/surface.ts b/packages/mobile/src/surface.ts index 1263f02df5..6537df0048 100644 --- a/packages/mobile/src/surface.ts +++ b/packages/mobile/src/surface.ts @@ -1221,7 +1221,11 @@ export class AgentDeviceSurface { return entry; } - /** The same control in a fresh snapshot; input mutations refuse a missing target or changed security facts. */ + /** + * The same control in a fresh snapshot. A snapshot that no longer lists it + * is a retryable NODE_STALE, so the locator resolves again; input mutations + * also refuse a truncated snapshot or changed security facts. + */ private async relocated(entry: NodeBinding, operation: OperationContext, input = false): Promise { const raw = await this.snapshotOrEmpty(operation, false); const projected = this.project(raw); @@ -1233,7 +1237,10 @@ export class AgentDeviceSurface { entry.node.states?.hidden !== found.node.states?.hidden)) { throw new EngineError('NODE_STALE', `input node ${entry.id} changed before mutation; observe and authorize it again`, { retryable: true }); } - return found === undefined ? entry : this.bind(found, projected); + if (found === undefined) { + throw new EngineError('NODE_STALE', `node ${entry.id} changed during transition; re-resolve`, { retryable: true }); + } + return this.bind(found, projected); } /** diff --git a/packages/mobile/tests/unit/engine.test.ts b/packages/mobile/tests/unit/engine.test.ts index 0a0e4b7033..194ea40118 100644 --- a/packages/mobile/tests/unit/engine.test.ts +++ b/packages/mobile/tests/unit/engine.test.ts @@ -2705,13 +2705,42 @@ describe('deterministic actions', () => { await h.engine.perform!(inFlight.ref, { kind: 'tap' }, test()); expect(h.fake.lastArgs('interactions.press')).toEqual({ ref: '@e42' }); - // A control the fresh snapshot no longer lists is acted on as it was. + // A missing control must be resolved again before any input is dispatched. + h.fake.respond('capture.snapshot', () => SETTINGS_SNAPSHOT); await h.engine.perform!((await observed(h, 'Back')).ref, { kind: 'tap' }, test()); h.fake.respond('capture.snapshot', () => submit(600, '@e42')); const landed = await observed(h, 'Submit'); h.fake.respond('capture.snapshot', () => SETTINGS_SNAPSHOT); - await h.engine.perform!(landed.ref, { kind: 'tap' }, test()); - expect(h.fake.lastArgs('interactions.press')).toEqual({ ref: '@e42' }); + const presses = h.fake.methods().filter((method) => method === 'interactions.press').length; + await expect(h.engine.perform!(landed.ref, { kind: 'tap' }, test())).rejects.toMatchObject({ code: 'NODE_STALE', retryable: true }); + expect(h.fake.methods().filter((method) => method === 'interactions.press')).toHaveLength(presses); + }); + + it('does not tap a recycled ref when a native button becomes a tab during relocation', async () => { + autoAdvanceTimers(); + const h = harness({ transition: 120 }); + await openAttempt(h); + await h.engine.perform!((await observed(h, 'Back')).ref, { kind: 'tap' }, test()); + h.fake.respond('capture.snapshot', () => ({ + ...SETTINGS_SNAPSHOT, + nodes: [...SETTINGS_NODES, { ref: '@e11', index: 10, parentIndex: 0, depth: 1, type: 'button', label: 'Chat' }], + })); + const chat = await observed(h, 'Chat'); + h.fake.respond('capture.snapshot', () => ({ + ...SETTINGS_SNAPSHOT, + nodes: [ + ...SETTINGS_NODES, + { ref: '@e10', index: 10, parentIndex: 0, depth: 1, type: 'tab-bar' }, + { ref: '@e12', index: 11, parentIndex: 10, depth: 2, type: 'button', label: 'Chat' }, + { ref: '@e11', index: 12, parentIndex: 10, depth: 2, type: 'button', label: 'Orders' }, + ], + })); + const presses = h.fake.methods().filter((method) => method === 'interactions.press').length; + await expect(h.engine.perform!(chat.ref, { kind: 'tap' }, test())).rejects.toMatchObject({ code: 'NODE_STALE', retryable: true }); + expect(h.fake.methods().filter((method) => method === 'interactions.press')).toHaveLength(presses); + + await h.engine.perform!((await observed(h, 'Chat')).ref, { kind: 'tap' }, test()); + expect(h.fake.lastArgs('interactions.press')).toEqual({ ref: '@e12' }); }); it.each(