-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathautomate.ts
More file actions
141 lines (125 loc) · 3.78 KB
/
Copy pathautomate.ts
File metadata and controls
141 lines (125 loc) · 3.78 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import config from "../../config.js";
import {
HarEntry,
HarFile,
filterLinesByKeywords,
validateLogResponse,
} from "./utils.js";
import { DOMAINS } from "../../lib/domains.js";
const auth = Buffer.from(
`${config.browserstackUsername}:${config.browserstackAccessKey}`,
).toString("base64");
// NETWORK LOGS
export async function retrieveNetworkFailures(
sessionId: string,
): Promise<string> {
const url = `${DOMAINS.API}/automate/sessions/${sessionId}/networklogs`;
const response = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${auth}`,
},
});
const validationError = validateLogResponse(response, "network logs");
if (validationError) return validationError.message!;
const networklogs: HarFile = await response.json();
const failureEntries: HarEntry[] = networklogs.log.entries.filter(
(entry: HarEntry) =>
entry.response.status === 0 ||
entry.response.status >= 400 ||
entry.response._error !== undefined,
);
return failureEntries.length > 0
? `Network Failures (${failureEntries.length} found):\n${JSON.stringify(
failureEntries.map((entry: any) => ({
startedDateTime: entry.startedDateTime,
request: {
method: entry.request?.method,
url: entry.request?.url,
queryString: entry.request?.queryString,
},
response: {
status: entry.response?.status,
statusText: entry.response?.statusText,
_error: entry.response?._error,
},
serverIPAddress: entry.serverIPAddress,
time: entry.time,
})),
null,
2,
)}`
: "No network failures found";
}
// SESSION LOGS
export async function retrieveSessionFailures(
sessionId: string,
): Promise<string> {
const url = `${DOMAINS.API}/automate/sessions/${sessionId}/logs`;
const response = await fetch(url, {
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${auth}`,
},
});
const validationError = validateLogResponse(response, "session logs");
if (validationError) return validationError.message!;
const logText = await response.text();
const logs = filterSessionFailures(logText);
return logs.length > 0
? `Session Failures (${logs.length} found):\n${JSON.stringify(logs, null, 2)}`
: "No session failures found";
}
// CONSOLE LOGS
export async function retrieveConsoleFailures(
sessionId: string,
): Promise<string> {
const url = `${DOMAINS.API}/automate/sessions/${sessionId}/consolelogs`;
const response = await fetch(url, {
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${auth}`,
},
});
const validationError = validateLogResponse(response, "console logs");
if (validationError) return validationError.message!;
const logText = await response.text();
const logs = filterConsoleFailures(logText);
return logs.length > 0
? `Console Failures (${logs.length} found):\n${JSON.stringify(logs, null, 2)}`
: "No console failures found";
}
// FILTER: session logs
export function filterSessionFailures(logText: string): string[] {
const keywords = [
"error",
"fail",
"exception",
"fatal",
"unable to",
"not found",
'"success":false',
'"success": false',
'"msg":',
"console.error",
"stderr",
];
return filterLinesByKeywords(logText, keywords);
}
// FILTER: console logs
export function filterConsoleFailures(logText: string): string[] {
const keywords = [
"failed to load resource",
"uncaught",
"typeerror",
"referenceerror",
"scanner is not ready",
"status of 4",
"status of 5",
"not found",
"undefined",
"error:",
];
return filterLinesByKeywords(logText, keywords);
}