-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetNetworkLogs.js
More file actions
63 lines (54 loc) · 1.82 KB
/
Copy pathgetNetworkLogs.js
File metadata and controls
63 lines (54 loc) · 1.82 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
import { browserStackFetch } from './lib/auth.js';
import { isMainModule, requireArg } from './lib/cli.js';
const IGNORED_EXTENSIONS = /\.(png|jpg|jpeg|gif|svg|webp|ico|css|js|woff|woff2|ttf|otf)$/i;
/**
* Filter HAR entries down to main HTML page navigations.
* @param {object[]} entries
* @returns {object[]}
*/
export function extractMainPages(entries) {
return entries.filter((entry) => {
const url = entry.request?.url || '';
const mimeType = entry.response?.content?.mimeType || '';
if (IGNORED_EXTENSIONS.test(url)) return false;
if (mimeType && !mimeType.includes('text/html')) return false;
return true;
});
}
/**
* Fetch network logs for a session and return main page entries.
* @param {string} sessionId
* @returns {Promise<object[]>}
*/
export async function getNetworkLogs(sessionId) {
const data = await browserStackFetch(
`https://api.browserstack.com/automate/sessions/${sessionId}/networklogs`
);
const entries = data.log?.entries || [];
return extractMainPages(entries);
}
/**
* Convenience: return only page URLs from a session's network logs.
* @param {string} sessionId
* @returns {Promise<string[]>}
*/
export async function getPageUrls(sessionId) {
const pages = await getNetworkLogs(sessionId);
return pages.map((entry) => entry.request?.url).filter(Boolean);
}
async function main() {
const sessionId = requireArg(
'sessionId',
process.argv[2] || process.env.BROWSERSTACK_SESSION_ID,
'node getNetworkLogs.js <sessionId>'
);
const urls = await getPageUrls(sessionId);
console.log(`Retrieved ${urls.length} page URL(s) for session ${sessionId}:`);
console.log(JSON.stringify(urls, null, 2));
}
if (isMainModule(import.meta.url)) {
main().catch((error) => {
console.error('Error fetching network logs:', error.message);
process.exit(1);
});
}