Skip to content

Commit b732652

Browse files
committed
enhance self-heal selector extraction with context information
1 parent 4cebd1a commit b732652

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

src/tools/selfheal-utils/selfheal.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import config from "../../config.js";
44
interface SelectorMapping {
55
originalSelector: string;
66
healedSelector: string;
7+
context: {
8+
before: string;
9+
after: string;
10+
};
711
}
812

913
export async function getSelfHealSelectors(sessionId: string) {
@@ -24,20 +28,40 @@ export async function getSelfHealSelectors(sessionId: string) {
2428
}
2529

2630
function extractHealedSelectors(logText: string): SelectorMapping[] {
27-
// Pattern to match SELFHEAL entries with healed selectors
31+
// Split log text into lines for easier context handling
32+
const logLines = logText.split("\n");
33+
34+
// Pattern to match successful SELFHEAL entries only
2835
const selfhealPattern =
29-
/SELFHEAL\s*{\s*"status":"true",\s*"data":\s*{\s*"using":"css selector",\s*"value":"(.*?)"}/g;
36+
/SELFHEAL\s*{\s*"status":"true",\s*"data":\s*{\s*"using":"css selector",\s*"value":"(.*?)"}/;
3037

3138
// Pattern to match preceding selector requests
3239
const requestPattern =
3340
/POST \/session\/[^/]+\/element.*?"using":"css selector","value":"(.*?)"/g;
3441

35-
// Find all healed selectors
36-
const healedSelectors: string[] = [];
37-
let healedMatch;
38-
while ((healedMatch = selfhealPattern.exec(logText)) !== null) {
39-
healedSelectors.push(healedMatch[1]);
40-
}
42+
// Find all successful healed selectors with their line numbers and context
43+
const healedSelectors: Array<{
44+
selector: string;
45+
lineNumber: number;
46+
context: { before: string; after: string };
47+
}> = [];
48+
49+
logLines.forEach((line, index) => {
50+
const match = line.match(selfhealPattern);
51+
if (match) {
52+
const beforeLine = index > 0 ? logLines[index - 1] : "";
53+
const afterLine = index < logLines.length - 1 ? logLines[index + 1] : "";
54+
55+
healedSelectors.push({
56+
selector: match[1],
57+
lineNumber: index,
58+
context: {
59+
before: beforeLine,
60+
after: afterLine,
61+
},
62+
});
63+
}
64+
});
4165

4266
// Find all selector requests
4367
const selectorRequests: string[] = [];
@@ -53,7 +77,8 @@ function extractHealedSelectors(logText: string): SelectorMapping[] {
5377
for (let i = 0; i < minLength; i++) {
5478
healedMappings.push({
5579
originalSelector: selectorRequests[i],
56-
healedSelector: healedSelectors[i],
80+
healedSelector: healedSelectors[i].selector,
81+
context: healedSelectors[i].context,
5782
});
5883
}
5984

0 commit comments

Comments
 (0)