-
Notifications
You must be signed in to change notification settings - Fork 570
fix: add an option value to the snapshot #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlinaVarkki
wants to merge
8
commits into
main
Choose a base branch
from
fix-select
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a728c04
format
AlinaVarkki ee14198
format
AlinaVarkki 56ab321
option handle seperately
AlinaVarkki b1506ed
add test
AlinaVarkki 047c403
make value string
AlinaVarkki c680b3c
remove value
AlinaVarkki dcced0f
use option text as value
AlinaVarkki e873cd9
check if element is selector through ax node and extract method
AlinaVarkki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
import type {ElementHandle} from 'puppeteer-core'; | ||
import z from 'zod'; | ||
|
||
import type {McpContext} from '../McpContext.js'; | ||
|
||
import {ToolCategories} from './categories.js'; | ||
import {defineTool} from './ToolDefinition.js'; | ||
|
||
|
@@ -78,6 +80,40 @@ export const hover = defineTool({ | |
}, | ||
}); | ||
|
||
async function fillFormElement( | ||
uid: string, | ||
value: string, | ||
context: McpContext, | ||
) { | ||
const handle = await context.getElementByUid(uid); | ||
try { | ||
// The AXNode for an option doesn't contain its `value`. We set text content of the option as value. | ||
// If the form is a combobox, we need to find the correct option by its text value. | ||
// To do that, loop through the children while checking which child's text matches the requested value (requested value is actually the text content). | ||
// When the correct option is found, use the element handle to get the real value. | ||
const aXNode = context.getAXNodeByUid(uid); | ||
if (aXNode && aXNode.role === 'combobox' && aXNode.children) { | ||
for (const child of aXNode.children) { | ||
if (child.role === 'option' && child.name === value && child.value) { | ||
const childHandle = await child.elementHandle(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to dispose the child handle after we use it |
||
if (childHandle) { | ||
const childValueHandle = await childHandle.getProperty('value'); | ||
const childValue = await childValueHandle.jsonValue(); | ||
if (childValue) { | ||
await handle.asLocator().fill(childValue.toString()); | ||
} | ||
break; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should throw an error if no option was found |
||
} | ||
} else { | ||
await handle.asLocator().fill(value); | ||
} | ||
} finally { | ||
void handle.dispose(); | ||
} | ||
} | ||
|
||
export const fill = defineTool({ | ||
name: 'fill', | ||
description: `Type text into a input, text area or select an option from a <select> element.`, | ||
|
@@ -94,16 +130,15 @@ export const fill = defineTool({ | |
value: z.string().describe('The value to fill in'), | ||
}, | ||
handler: async (request, response, context) => { | ||
const handle = await context.getElementByUid(request.params.uid); | ||
try { | ||
await context.waitForEventsAfterAction(async () => { | ||
await handle.asLocator().fill(request.params.value); | ||
}); | ||
response.appendResponseLine(`Successfully filled out the element`); | ||
response.setIncludeSnapshot(true); | ||
} finally { | ||
void handle.dispose(); | ||
} | ||
await context.waitForEventsAfterAction(async () => { | ||
await fillFormElement( | ||
request.params.uid, | ||
request.params.value, | ||
context as McpContext, | ||
); | ||
}); | ||
response.appendResponseLine(`Successfully filled out the element`); | ||
response.setIncludeSnapshot(true); | ||
}, | ||
}); | ||
|
||
|
@@ -155,14 +190,13 @@ export const fillForm = defineTool({ | |
}, | ||
handler: async (request, response, context) => { | ||
for (const element of request.params.elements) { | ||
const handle = await context.getElementByUid(element.uid); | ||
try { | ||
await context.waitForEventsAfterAction(async () => { | ||
await handle.asLocator().fill(element.value); | ||
}); | ||
} finally { | ||
void handle.dispose(); | ||
} | ||
await context.waitForEventsAfterAction(async () => { | ||
await fillFormElement( | ||
element.uid, | ||
element.value, | ||
context as McpContext, | ||
); | ||
}); | ||
} | ||
response.appendResponseLine(`Successfully filled out the form`); | ||
response.setIncludeSnapshot(true); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we could extract the logic for finding an option in a combobox into a helper function?