|
7 | 7 | import type {ElementHandle} from 'puppeteer-core'; |
8 | 8 | import z from 'zod'; |
9 | 9 |
|
10 | | -import type {McpContext} from '../McpContext.js'; |
| 10 | +import type {McpContext, TextSnapshotNode} from '../McpContext.js'; |
11 | 11 |
|
12 | 12 | import {ToolCategories} from './categories.js'; |
13 | 13 | import {defineTool} from './ToolDefinition.js'; |
@@ -80,32 +80,53 @@ export const hover = defineTool({ |
80 | 80 | }, |
81 | 81 | }); |
82 | 82 |
|
83 | | -async function fillFormElement( |
84 | | - uid: string, |
| 83 | +// The AXNode for an option doesn't contain its `value`. We set text content of the option as value. |
| 84 | +// If the form is a combobox, we need to find the correct option by its text value. |
| 85 | +// To do that, loop through the children while checking which child's text matches the requested value (requested value is actually the text content). |
| 86 | +// When the correct option is found, use the element handle to get the real value. |
| 87 | +async function selectOption( |
| 88 | + handle: ElementHandle, |
| 89 | + aXNode: TextSnapshotNode, |
85 | 90 | value: string, |
86 | | - context: McpContext, |
87 | 91 | ) { |
88 | | - const handle = await context.getElementByUid(uid); |
89 | | - try { |
90 | | - // The AXNode for an option doesn't contain its `value`. We set text content of the option as value. |
91 | | - // If the form is a combobox, we need to find the correct option by its text value. |
92 | | - // To do that, loop through the children while checking which child's text matches the requested value (requested value is actually the text content). |
93 | | - // When the correct option is found, use the element handle to get the real value. |
94 | | - const aXNode = context.getAXNodeByUid(uid); |
95 | | - if (aXNode && aXNode.role === 'combobox' && aXNode.children) { |
96 | | - for (const child of aXNode.children) { |
97 | | - if (child.role === 'option' && child.name === value && child.value) { |
98 | | - const childHandle = await child.elementHandle(); |
99 | | - if (childHandle) { |
100 | | - const childValueHandle = await childHandle.getProperty('value'); |
| 92 | + let optionFound = false; |
| 93 | + for (const child of aXNode.children) { |
| 94 | + if (child.role === 'option' && child.name === value && child.value) { |
| 95 | + optionFound = true; |
| 96 | + const childHandle = await child.elementHandle(); |
| 97 | + if (childHandle) { |
| 98 | + try { |
| 99 | + const childValueHandle = await childHandle.getProperty('value'); |
| 100 | + try { |
101 | 101 | const childValue = await childValueHandle.jsonValue(); |
102 | 102 | if (childValue) { |
103 | 103 | await handle.asLocator().fill(childValue.toString()); |
104 | 104 | } |
105 | | - break; |
| 105 | + } finally { |
| 106 | + void childValueHandle.dispose(); |
106 | 107 | } |
| 108 | + break; |
| 109 | + } finally { |
| 110 | + void childHandle.dispose(); |
107 | 111 | } |
108 | 112 | } |
| 113 | + } |
| 114 | + } |
| 115 | + if (!optionFound) { |
| 116 | + throw new Error(`Could not find option with text "${value}"`); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +async function fillFormElement( |
| 121 | + uid: string, |
| 122 | + value: string, |
| 123 | + context: McpContext, |
| 124 | +) { |
| 125 | + const handle = await context.getElementByUid(uid); |
| 126 | + try { |
| 127 | + const aXNode = context.getAXNodeByUid(uid); |
| 128 | + if (aXNode && aXNode.role === 'combobox') { |
| 129 | + await selectOption(handle, aXNode, value); |
109 | 130 | } else { |
110 | 131 | await handle.asLocator().fill(value); |
111 | 132 | } |
|
0 commit comments