Skip to content

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 5 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import addTestManagementTools from "./tools/testmanagement.js";
import addAppAutomationTools from "./tools/appautomate.js";
import addFailureLogsTools from "./tools/getFailureLogs.js";
import addAutomateTools from "./tools/automate.js";
import addSelfHealTools from "./tools/selfheal.js";
import { setupOnInitialized } from "./oninitialized.js";

function registerTools(server: McpServer) {
Expand All @@ -26,6 +27,7 @@ function registerTools(server: McpServer) {
addAppAutomationTools(server);
addFailureLogsTools(server);
addAutomateTools(server);
addSelfHealTools(server);
}

// Create an MCP server
Expand Down
86 changes: 86 additions & 0 deletions src/tools/selfheal-utils/selfheal.ts
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;
}
54 changes: 54 additions & 0 deletions src/tools/selfheal.ts
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",
Copy link
Collaborator

Choose a reason for hiding this comment

The 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}`,
},
],
};
}
},
);
}