Skip to content

Commit 3500517

Browse files
authored
fix(alert): read W3C element id for Android custom button clicks (#491)
* fix(alert): read W3C element id for Android custom button clicks * fix(alert): read W3C element id for Android custom button clicks
1 parent 5fb770a commit 3500517

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

src/tests/tools/interactions/handle-alert.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('appium_alert Android custom button', () => {
8181
expect(mockElementClick).not.toHaveBeenCalled();
8282
});
8383

84-
test('clicks the resolved element when findElement succeeds', async () => {
84+
test('clicks the resolved W3C element id when findElement succeeds', async () => {
8585
mockFindElement.mockResolvedValue({
8686
'element-6066-11e4-a52e-4f735466cecf': 'el-1',
8787
});
@@ -90,6 +90,16 @@ describe('appium_alert Android custom button', () => {
9090
const result = await tool.execute({action: 'accept', buttonLabel: 'OK'}, undefined);
9191

9292
expect(result.isError).toBeFalsy();
93-
expect(mockElementClick).toHaveBeenCalledTimes(1);
93+
expect(mockElementClick).toHaveBeenCalledWith(expect.anything(), 'el-1');
94+
});
95+
96+
test('clicks the legacy ELEMENT id when findElement returns it', async () => {
97+
mockFindElement.mockResolvedValue({ELEMENT: 'legacy-el-1'});
98+
99+
const tool = await loadTool();
100+
const result = await tool.execute({action: 'accept', buttonLabel: 'OK'}, undefined);
101+
102+
expect(result.isError).toBeFalsy();
103+
expect(mockElementClick).toHaveBeenCalledWith(expect.anything(), 'legacy-el-1');
94104
});
95105
});

src/tools/interactions/handle-alert.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {elementClick, execute, findElement, getPageSource} from '../../command.j
55
import {generateAllElementLocators} from '../../locators/generate-all-locators.js';
66
import {getPlatformName, PLATFORM} from '../../session-store.js';
77
import type {DriverInstance} from '../../session-store.js';
8-
import {resolveDriver, textResult, errorResult, toolErrorMessage} from '../tool-response.js';
8+
import {readWebElementId, resolveDriver, textResult, errorResult, toolErrorMessage} from '../tool-response.js';
99

1010
const ANDROID_LOCATOR_STRATEGY_ORDER = ['accessibility id', 'id', 'xpath', '-android uiautomator', 'class name'];
1111

@@ -99,8 +99,11 @@ async function handleAndroidAlert(driver: DriverInstance, action: string, button
9999
if (!button) {
100100
throw new Error('Could not find element with any generated locator; it may have disappeared');
101101
}
102-
const buttonUUID = button.ELEMENT || button;
103-
await elementClick(driver, buttonUUID);
102+
const buttonId = readWebElementId(button);
103+
if (!buttonId) {
104+
throw new Error('Element was returned without a valid element ID');
105+
}
106+
await elementClick(driver, buttonId);
104107
} else {
105108
if (action === 'accept') {
106109
await execute(driver, 'mobile: acceptAlert', {});

0 commit comments

Comments
 (0)