Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mobile-stale-transition-ref.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions docs/reference/mobile.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions packages/mobile/src/surface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NodeBinding> {
const raw = await this.snapshotOrEmpty(operation, false);
const projected = this.project(raw);
Expand All @@ -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);
}

/**
Expand Down
35 changes: 32 additions & 3 deletions packages/mobile/tests/unit/engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading