-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathselfheal.ts
More file actions
73 lines (62 loc) · 2.23 KB
/
selfheal.ts
File metadata and controls
73 lines (62 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { assertOkResponse } from "../../lib/utils.js";
import config from "../../config.js";
import { DOMAINS } from "../../lib/domains.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 = `${DOMAINS.API_CLOUD}/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;
}