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 all 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
73 changes: 73 additions & 0 deletions src/tools/selfheal-utils/selfheal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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":"(.*?)"/;

// Find all successful healed selectors with their line numbers and context
const healedMappings: SelectorMapping[] = [];

for (let i = 0; i < logLines.length; i++) {
const match = logLines[i].match(selfhealPattern);
if (match) {
const beforeLine = i > 0 ? logLines[i - 1] : "";
const afterLine = i < logLines.length - 1 ? logLines[i + 1] : "";

// Look backwards to find the most recent original selector request
let originalSelector = "UNKNOWN";
for (let j = i - 1; j >= 0; j--) {
const requestMatch = logLines[j].match(requestPattern);
if (requestMatch) {
originalSelector = requestMatch[1];
break;
}
}

healedMappings.push({
originalSelector,
healedSelector: match[1],
context: {
before: beforeLine,
after: afterLine,
},
});
}
}

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(
"fetchSelfHealedSelectors",
"Retrieves AI-generated, self-healed selectors for a BrowserStack Automate session to resolve flaky tests caused by dynamic DOM changes.",
{
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}`,
},
],
};
}
},
);
}