Skip to content

Commit 1067b84

Browse files
committed
Refactor selector extraction logic to improve mapping
1 parent b732652 commit 1067b84

File tree

1 file changed

+19
-32
lines changed

1 file changed

+19
-32
lines changed

src/tools/selfheal-utils/selfheal.ts

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,49 +37,36 @@ function extractHealedSelectors(logText: string): SelectorMapping[] {
3737

3838
// Pattern to match preceding selector requests
3939
const requestPattern =
40-
/POST \/session\/[^/]+\/element.*?"using":"css selector","value":"(.*?)"/g;
40+
/POST \/session\/[^/]+\/element.*?"using":"css selector","value":"(.*?)"/;
4141

4242
// 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-
}> = [];
43+
const healedMappings: SelectorMapping[] = [];
4844

49-
logLines.forEach((line, index) => {
50-
const match = line.match(selfhealPattern);
45+
for (let i = 0; i < logLines.length; i++) {
46+
const match = logLines[i].match(selfhealPattern);
5147
if (match) {
52-
const beforeLine = index > 0 ? logLines[index - 1] : "";
53-
const afterLine = index < logLines.length - 1 ? logLines[index + 1] : "";
48+
const beforeLine = i > 0 ? logLines[i - 1] : "";
49+
const afterLine = i < logLines.length - 1 ? logLines[i + 1] : "";
50+
51+
// Look backwards to find the most recent original selector request
52+
let originalSelector = "UNKNOWN";
53+
for (let j = i - 1; j >= 0; j--) {
54+
const requestMatch = logLines[j].match(requestPattern);
55+
if (requestMatch) {
56+
originalSelector = requestMatch[1];
57+
break;
58+
}
59+
}
5460

55-
healedSelectors.push({
56-
selector: match[1],
57-
lineNumber: index,
61+
healedMappings.push({
62+
originalSelector,
63+
healedSelector: match[1],
5864
context: {
5965
before: beforeLine,
6066
after: afterLine,
6167
},
6268
});
6369
}
64-
});
65-
66-
// Find all selector requests
67-
const selectorRequests: string[] = [];
68-
let requestMatch;
69-
while ((requestMatch = requestPattern.exec(logText)) !== null) {
70-
selectorRequests.push(requestMatch[1]);
71-
}
72-
73-
// Pair each healed selector with its corresponding original selector
74-
const healedMappings: SelectorMapping[] = [];
75-
const minLength = Math.min(selectorRequests.length, healedSelectors.length);
76-
77-
for (let i = 0; i < minLength; i++) {
78-
healedMappings.push({
79-
originalSelector: selectorRequests[i],
80-
healedSelector: healedSelectors[i].selector,
81-
context: healedSelectors[i].context,
82-
});
8370
}
8471

8572
return healedMappings;

0 commit comments

Comments
 (0)