Skip to content

Commit 3b4e186

Browse files
committed
fix(provider-webdriver): leave a down keyboard alone, keep echo probes from failing a landed key press
Appium refuses hide-keyboard when no soft keyboard shows, so dismiss now skips the route when the probe says the keyboard is down and answers dismissed:false, as native dismiss does. The visibility probe after enter or dismiss is an echo, so its failure leaves visible unknown instead of failing a mutation that landed. Enter is declared on Android only until the iOS newline route has run against XCUITest.
1 parent ef27ad8 commit 3b4e186

4 files changed

Lines changed: 92 additions & 32 deletions

File tree

‎packages/provider-webdriver/src/platform-runtime.test.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ test('an Apple WebDriver dump bounded to one entry reports unnamed traffic witho
661661
expect(result.dump).not.toHaveProperty('unnamedRequestIds');
662662
});
663663

664-
test('an iOS WebDriver device serves keyboard enter and dismiss but not the Android IME probe', async () => {
664+
test('an iOS WebDriver device serves keyboard dismiss, and neither the unproven enter nor the Android IME probe', async () => {
665665
const iosDevice: DeviceInfo = {
666666
...device,
667667
platform: 'apple',
@@ -677,12 +677,13 @@ test('an iOS WebDriver device serves keyboard enter and dismiss but not the Andr
677677
getInteractor: vi.fn(() => ({}) as unknown as Interactor),
678678
});
679679
const facts = await owner.inspectFacts(iosDevice);
680-
expect(facts.operations.keyboardEnter).toEqual({ available: true });
681680
expect(facts.operations.keyboardDismiss).toEqual({ available: true });
682-
expect(facts.operations.keyboardStatus).toMatchObject({
683-
available: false,
684-
reason: 'unsupported-provider-mode',
685-
});
681+
for (const operation of ['keyboardEnter', 'keyboardStatus'] as const) {
682+
expect(facts.operations[operation]).toMatchObject({
683+
available: false,
684+
reason: 'unsupported-provider-mode',
685+
});
686+
}
686687
});
687688

688689
test('a provider that declares no keyboard capability closes the keyboard family', async () => {

‎packages/provider-webdriver/src/platform-runtime.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -661,15 +661,19 @@ function webDriverFacts(
661661
orientation: declared('orientation', orientationUnavailable),
662662
}),
663663
...tvRemoteRuntimeOperationFacts({ tvRemote: tvRemoteUnavailable }),
664-
// Enter and dismiss ride the same reachable interactor `back`/`home` do, over the driver's
665-
// key press and hide-keyboard routes. Status is the Appium keyboard probe, whose result
666-
// shape is Android's IME probe, so only an Android device serves it.
664+
// Dismiss rides the same reachable interactor `back`/`home` do, over the driver's
665+
// hide-keyboard route. Status is the Appium keyboard probe, whose result shape is Android's
666+
// IME probe, so only an Android device serves it.
667667
...keyboardRuntimeOperationFacts({
668668
unsupported: keyboardUnavailable,
669-
enter: declared('keyboard', keyboardUnavailable),
670669
dismiss: declared('keyboard', keyboardUnavailable),
670+
// Enter is proven on Android only: the newline-through-`/keys` route is written for
671+
// XCUITest but has not run against it, so an iOS device refuses until it has.
671672
...(device.platform === 'android'
672-
? { status: declared('keyboard', keyboardUnavailable) }
673+
? {
674+
enter: declared('keyboard', keyboardUnavailable),
675+
status: declared('keyboard', keyboardUnavailable),
676+
}
673677
: {}),
674678
}),
675679
// Clipboard rides the same reachable interactor `back`/`home` do; the declared-capability

‎packages/provider-webdriver/src/webdriver-interactor.test.ts‎

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,9 @@ test('endpoint plans become one timed W3C pointer move', async () => {
529529
});
530530

531531
/** A client that records the keyboard routes the interactor drives, answering the probe from `shown`. */
532-
function createKeyboardWorld(shown: Array<boolean | 'unsupported'>) {
532+
function createKeyboardWorld(shown: Array<boolean | 'unsupported' | 'failing'>) {
533533
const transcript: string[] = [];
534+
const upAtStart = shown[0] !== false;
534535
const client = {
535536
executeScript: async (script: string, args: unknown[]) => {
536537
transcript.push(`${script} ${JSON.stringify(args)}`);
@@ -540,10 +541,18 @@ function createKeyboardWorld(shown: Array<boolean | 'unsupported'>) {
540541
},
541542
hideKeyboard: async () => {
542543
transcript.push('hide');
544+
// The driver refuses the route when no soft keyboard shows.
545+
if (!upAtStart) {
546+
throw new AppError('COMMAND_FAILED', 'Soft keyboard not present', { status: 500 });
547+
}
543548
},
544549
isKeyboardShown: async () => {
545550
transcript.push('probe');
546-
return shown.length > 1 ? shown.shift()! : shown[0]!;
551+
const next = shown.length > 1 ? shown.shift()! : shown[0]!;
552+
if (next === 'failing') {
553+
throw new AppError('COMMAND_FAILED', 'is_keyboard_shown timed out', { status: 504 });
554+
}
555+
return next;
547556
},
548557
} as unknown as WebDriverClient;
549558
return { transcript, client };
@@ -616,21 +625,6 @@ test('keyboard dismiss names the Appium route as the mechanism on iOS', async ()
616625
});
617626
});
618627

619-
// `dismissed` is the transition, as the Android IME probe defines it: a keyboard that was never
620-
// up was not dismissed by this call.
621-
test('keyboard dismiss does not claim a dismissal when the keyboard was already down', async () => {
622-
const world = createKeyboardWorld([false, false]);
623-
624-
const result = await keyboardInteractor(world, 'android').keyboardDismiss!();
625-
626-
assert.deepEqual(result, {
627-
kind: 'ime-probe',
628-
wasVisible: false,
629-
visible: false,
630-
dismissed: false,
631-
});
632-
});
633-
634628
test('keyboard status reads the Appium probe, and refuses a driver without one', async () => {
635629
const shown = createKeyboardWorld([false]);
636630
assert.deepEqual(await keyboardInteractor(shown, 'android').keyboardStatus!(), {
@@ -674,3 +668,38 @@ test('a provider that declares no keyboard capability refuses every keyboard act
674668
}
675669
assert.deepEqual(world.transcript, []);
676670
});
671+
672+
// Appium refuses hide-keyboard when nothing is up; native dismiss answers "already hidden" with
673+
// success, so a script's idempotent dismiss must not break on a cloud device. The fake driver
674+
// throws on that call, so this test fails if the call is made.
675+
test('keyboard dismiss leaves a keyboard that is already down alone', async () => {
676+
const world = createKeyboardWorld([false]);
677+
678+
const result = await keyboardInteractor(world, 'android').keyboardDismiss!();
679+
680+
assert.deepEqual(result, {
681+
kind: 'ime-probe',
682+
wasVisible: false,
683+
visible: false,
684+
dismissed: false,
685+
});
686+
assert.deepEqual(world.transcript, ['probe']);
687+
});
688+
689+
// The probe after a landed mutation is an echo: a gateway that times it out must not make the
690+
// caller retry a key press that already happened.
691+
test('a failing echo probe after enter or dismiss leaves the visibility unknown instead of failing', async () => {
692+
const entered = createKeyboardWorld([true, 'failing']);
693+
assert.deepEqual(await keyboardInteractor(entered, 'ios').keyboardEnter!(), {
694+
kind: 'visibility-echo',
695+
wasVisible: true,
696+
});
697+
assert.deepEqual(entered.transcript, ['probe', String.raw`keys "\n"`, 'probe']);
698+
699+
const dismissed = createKeyboardWorld([true, 'failing']);
700+
assert.deepEqual(await keyboardInteractor(dismissed, 'android').keyboardDismiss!(), {
701+
kind: 'ime-probe',
702+
wasVisible: true,
703+
});
704+
assert.deepEqual(dismissed.transcript, ['probe', 'hide', 'probe']);
705+
});

‎packages/provider-webdriver/src/webdriver-interactor.ts‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,15 +375,29 @@ class WebDriverInteractor implements Interactor {
375375
return { kind: 'ime-probe', visible: await this.keyboardVisible() };
376376
}
377377

378+
/**
379+
* A keyboard that is not up is left alone: Appium refuses the hide-keyboard route when no soft
380+
* keyboard shows, and native dismiss answers an already-hidden keyboard with success. Only a
381+
* keyboard seen up, or one the driver cannot report, is asked down.
382+
*/
378383
async keyboardDismiss(): Promise<KeyboardDismissResult> {
379384
this.requireSupport('keyboard');
380385
const wasVisible = await this.keyboardVisibility();
386+
if (wasVisible === false)
387+
return this.dismissResult({ wasVisible, visible: false, dismissed: false });
381388
await this.client.hideKeyboard();
382-
const visible = await this.keyboardVisibility();
383-
const echo = {
389+
const visible = await this.keyboardVisibilityAfterMutation();
390+
return this.dismissResult({
384391
...(wasVisible === undefined ? {} : { wasVisible }),
385392
...(visible === undefined ? {} : { visible, dismissed: wasVisible === true && !visible }),
386-
};
393+
});
394+
}
395+
396+
private dismissResult(echo: {
397+
wasVisible?: boolean;
398+
visible?: boolean;
399+
dismissed?: boolean;
400+
}): KeyboardDismissResult {
387401
return this.backend === 'android'
388402
? { kind: 'ime-probe', ...echo }
389403
: { kind: 'mechanism', mechanism: WEBDRIVER_DISMISS_MECHANISM, ...echo };
@@ -403,14 +417,26 @@ class WebDriverInteractor implements Interactor {
403417
}
404418
const wasVisible = await this.keyboardVisibility();
405419
await this.client.sendKeys('\n');
406-
const visible = await this.keyboardVisibility();
420+
const visible = await this.keyboardVisibilityAfterMutation();
407421
return {
408422
kind: 'visibility-echo',
409423
...(wasVisible === undefined ? {} : { wasVisible }),
410424
...(visible === undefined ? {} : { visible }),
411425
};
412426
}
413427

428+
/**
429+
* The echo read after a key press or dismiss landed. A probe that fails there must not turn
430+
* the landed mutation into a failure a caller would retry, so it reads as no echo.
431+
*/
432+
private async keyboardVisibilityAfterMutation(): Promise<boolean | undefined> {
433+
try {
434+
return await this.keyboardVisibility();
435+
} catch {
436+
return undefined;
437+
}
438+
}
439+
414440
/** The keyboard probe's answer, or `undefined` when this driver has no such route. */
415441
private async keyboardVisibility(): Promise<boolean | undefined> {
416442
const shown = await this.client.isKeyboardShown(TEXT_ENTRY_PROBE_TIMEOUT_MS);

0 commit comments

Comments
 (0)