-
Notifications
You must be signed in to change notification settings - Fork 9
feat: add automate self-heal tools integration #54
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4cebd1a
feat: add automate self-heal tools integration
tech-sushant b732652
enhance self-heal selector extraction with context information
tech-sushant 1067b84
Refactor selector extraction logic to improve mapping
tech-sushant 60eb5f5
Change tool description
tech-sushant fa304fe
Update src/tools/selfheal.ts
tech-sushant 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { assertOkResponse } from "../../lib/utils.js"; | ||
import config from "../../config.js"; | ||
|
||
interface SelectorMapping { | ||
originalSelector: string; | ||
healedSelector: string; | ||
context: { | ||
before: string; | ||
after: string; | ||
}; | ||
} | ||
|
||
export async function getSelfHealSelectors(sessionId: string) { | ||
const credentials = `${config.browserstackUsername}:${config.browserstackAccessKey}`; | ||
const auth = Buffer.from(credentials).toString("base64"); | ||
const url = `https://api.browserstack.com/automate/sessions/${sessionId}/logs`; | ||
|
||
const response = await fetch(url, { | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Basic ${auth}`, | ||
}, | ||
}); | ||
|
||
await assertOkResponse(response, "session logs"); | ||
const logText = await response.text(); | ||
return extractHealedSelectors(logText); | ||
} | ||
|
||
function extractHealedSelectors(logText: string): SelectorMapping[] { | ||
// Split log text into lines for easier context handling | ||
const logLines = logText.split("\n"); | ||
|
||
// Pattern to match successful SELFHEAL entries only | ||
const selfhealPattern = | ||
/SELFHEAL\s*{\s*"status":"true",\s*"data":\s*{\s*"using":"css selector",\s*"value":"(.*?)"}/; | ||
|
||
// Pattern to match preceding selector requests | ||
const requestPattern = | ||
/POST \/session\/[^/]+\/element.*?"using":"css selector","value":"(.*?)"/g; | ||
|
||
// Find all successful healed selectors with their line numbers and context | ||
const healedSelectors: Array<{ | ||
selector: string; | ||
lineNumber: number; | ||
context: { before: string; after: string }; | ||
}> = []; | ||
|
||
logLines.forEach((line, index) => { | ||
const match = line.match(selfhealPattern); | ||
if (match) { | ||
const beforeLine = index > 0 ? logLines[index - 1] : ""; | ||
const afterLine = index < logLines.length - 1 ? logLines[index + 1] : ""; | ||
|
||
healedSelectors.push({ | ||
selector: match[1], | ||
lineNumber: index, | ||
context: { | ||
before: beforeLine, | ||
after: afterLine, | ||
}, | ||
}); | ||
} | ||
}); | ||
|
||
// Find all selector requests | ||
const selectorRequests: string[] = []; | ||
let requestMatch; | ||
while ((requestMatch = requestPattern.exec(logText)) !== null) { | ||
selectorRequests.push(requestMatch[1]); | ||
} | ||
|
||
// Pair each healed selector with its corresponding original selector | ||
const healedMappings: SelectorMapping[] = []; | ||
const minLength = Math.min(selectorRequests.length, healedSelectors.length); | ||
|
||
for (let i = 0; i < minLength; i++) { | ||
healedMappings.push({ | ||
originalSelector: selectorRequests[i], | ||
healedSelector: healedSelectors[i].selector, | ||
context: healedSelectors[i].context, | ||
}); | ||
} | ||
|
||
return healedMappings; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
import { z } from "zod"; | ||
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; | ||
import { getSelfHealSelectors } from "./selfheal-utils/selfheal.js"; | ||
import logger from "../logger.js"; | ||
|
||
// Tool function that fetches self-healing selectors | ||
export async function fetchSelfHealSelectorTool(args: { | ||
sessionId: string; | ||
}): Promise<CallToolResult> { | ||
try { | ||
const selectors = await getSelfHealSelectors(args.sessionId); | ||
return { | ||
content: [ | ||
{ | ||
type: "text", | ||
text: | ||
"Self-heal selectors fetched successfully" + | ||
JSON.stringify(selectors), | ||
}, | ||
], | ||
}; | ||
} catch (error) { | ||
logger.error("Error fetching self-heal selector suggestions", error); | ||
throw error; | ||
} | ||
} | ||
|
||
// Registers the fetchSelfHealSelector tool with the MCP server | ||
export default function addSelfHealTools(server: McpServer) { | ||
server.tool( | ||
"fetchSelfHealSelector", | ||
"Fetch self-healing selector suggestions for a broken selector", | ||
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. update |
||
{ | ||
sessionId: z.string().describe("The session ID of the test run"), | ||
}, | ||
async (args) => { | ||
try { | ||
return await fetchSelfHealSelectorTool(args); | ||
} catch (error) { | ||
const errorMessage = | ||
error instanceof Error ? error.message : "Unknown error"; | ||
return { | ||
content: [ | ||
{ | ||
type: "text", | ||
text: `Error during fetching self-heal suggestions: ${errorMessage}`, | ||
}, | ||
], | ||
}; | ||
} | ||
}, | ||
); | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.