Skip to content

Commit 543af16

Browse files
committed
fix: address PR review comments for 0317-fix_input_clear_and_fill
1 parent 1eb96eb commit 543af16

3 files changed

Lines changed: 93 additions & 11 deletions

File tree

packages/browseros-agent/apps/server/src/browser/browser.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -818,14 +818,24 @@ export class Browser {
818818
}
819819

820820
if (clear) {
821-
const cleared = await elements.clearEditableElement(session, element)
822-
if (!cleared || (await elements.getInputValue(session, element))) {
823-
await keyboard.clearField(session)
824-
if (coords) {
825-
const value = await elements.getInputValue(session, element)
826-
if (value) {
827-
await mouse.dispatchClick(session, coords.x, coords.y, 'left', 3, 0)
828-
if (!text) await keyboard.pressCombo(session, 'Backspace')
821+
const existingValue = await elements.getInputValue(session, element)
822+
if (existingValue) {
823+
const cleared = await elements.clearEditableElement(session, element)
824+
if (!cleared || (await elements.getInputValue(session, element))) {
825+
await keyboard.clearField(session)
826+
if (coords) {
827+
const value = await elements.getInputValue(session, element)
828+
if (value) {
829+
await mouse.dispatchClick(
830+
session,
831+
coords.x,
832+
coords.y,
833+
'left',
834+
3,
835+
0,
836+
)
837+
if (!text) await keyboard.pressCombo(session, 'Backspace')
838+
}
829839
}
830840
}
831841
}

packages/browseros-agent/apps/server/src/browser/keyboard.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { platform } from 'node:os'
22
import type { ProtocolApi } from '@browseros/cdp-protocol/protocol-api'
33

4+
const PLATFORM_MODIFIER = platform() === 'darwin' ? 4 : 2
5+
46
type KeyInfo = { code: string; keyCode: number | undefined }
57

68
const KEY_MAP: Record<string, KeyInfo> = {
@@ -181,9 +183,32 @@ export async function typeText(
181183
}
182184

183185
export async function clearField(session: ProtocolApi): Promise<void> {
184-
const selectAllCombo = platform() === 'darwin' ? 'Meta+A' : 'Control+A'
185-
await pressCombo(session, selectAllCombo)
186-
await pressCombo(session, 'Backspace')
186+
await session.Input.dispatchKeyEvent({
187+
type: 'keyDown',
188+
key: 'a',
189+
code: 'KeyA',
190+
modifiers: PLATFORM_MODIFIER,
191+
windowsVirtualKeyCode: 65,
192+
})
193+
await session.Input.dispatchKeyEvent({
194+
type: 'keyUp',
195+
key: 'a',
196+
code: 'KeyA',
197+
modifiers: PLATFORM_MODIFIER,
198+
windowsVirtualKeyCode: 65,
199+
})
200+
await session.Input.dispatchKeyEvent({
201+
type: 'keyDown',
202+
key: 'Backspace',
203+
code: 'Backspace',
204+
windowsVirtualKeyCode: 8,
205+
})
206+
await session.Input.dispatchKeyEvent({
207+
type: 'keyUp',
208+
key: 'Backspace',
209+
code: 'Backspace',
210+
windowsVirtualKeyCode: 8,
211+
})
187212
}
188213

189214
function parseKeyCombo(input: string): {

packages/browseros-agent/apps/server/tests/tools/keyboard.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { describe, expect, it } from 'bun:test'
2+
import { platform } from 'node:os'
23
import {
4+
clearField,
35
getKeyInfo,
46
modifierBitmask,
57
normalizeKey,
@@ -158,3 +160,48 @@ describe('pressCombo validation', () => {
158160
)
159161
})
160162
})
163+
164+
describe('clearField', () => {
165+
it('uses lowercase select-all without standalone modifier events', async () => {
166+
const events: Array<Record<string, unknown>> = []
167+
const fakeSession = {
168+
Input: {
169+
dispatchKeyEvent: async (event: Record<string, unknown>) => {
170+
events.push(event)
171+
},
172+
},
173+
} as unknown as Parameters<typeof clearField>[0]
174+
175+
await clearField(fakeSession)
176+
177+
const modifier = platform() === 'darwin' ? 4 : 2
178+
expect(events).toEqual([
179+
{
180+
type: 'keyDown',
181+
key: 'a',
182+
code: 'KeyA',
183+
modifiers: modifier,
184+
windowsVirtualKeyCode: 65,
185+
},
186+
{
187+
type: 'keyUp',
188+
key: 'a',
189+
code: 'KeyA',
190+
modifiers: modifier,
191+
windowsVirtualKeyCode: 65,
192+
},
193+
{
194+
type: 'keyDown',
195+
key: 'Backspace',
196+
code: 'Backspace',
197+
windowsVirtualKeyCode: 8,
198+
},
199+
{
200+
type: 'keyUp',
201+
key: 'Backspace',
202+
code: 'Backspace',
203+
windowsVirtualKeyCode: 8,
204+
},
205+
])
206+
})
207+
})

0 commit comments

Comments
 (0)