4
4
HarFile ,
5
5
filterLinesByKeywords ,
6
6
validateLogResponse ,
7
- LogResponse ,
8
7
} from "./utils.js" ;
9
8
10
9
const auth = Buffer . from (
@@ -14,7 +13,7 @@ const auth = Buffer.from(
14
13
// NETWORK LOGS
15
14
export async function retrieveNetworkFailures (
16
15
sessionId : string ,
17
- ) : Promise < LogResponse > {
16
+ ) : Promise < string > {
18
17
const url = `https://api.browserstack.com/automate/sessions/${ sessionId } /networklogs` ;
19
18
20
19
const response = await fetch ( url , {
@@ -25,45 +24,40 @@ export async function retrieveNetworkFailures(
25
24
} ,
26
25
} ) ;
27
26
28
- const validationResult = validateLogResponse ( response , "network logs" ) ;
29
- if ( validationResult ) return validationResult ;
27
+ const validationError = validateLogResponse ( response , "network logs" ) ;
28
+ if ( validationError ) return validationError . message ! ;
30
29
31
30
const networklogs : HarFile = await response . json ( ) ;
32
-
33
- // Filter for failure logs
34
31
const failureEntries : HarEntry [ ] = networklogs . log . entries . filter (
35
- ( entry : HarEntry ) => {
36
- return (
37
- entry . response . status === 0 ||
38
- entry . response . status >= 400 ||
39
- entry . response . _error !== undefined
40
- ) ;
41
- } ,
32
+ ( entry : HarEntry ) =>
33
+ entry . response . status === 0 ||
34
+ entry . response . status >= 400 ||
35
+ entry . response . _error !== undefined
42
36
) ;
43
-
44
- return {
45
- logs : failureEntries . map ( ( entry : any ) => ( {
46
- startedDateTime : entry . startedDateTime ,
47
- request : {
48
- method : entry . request ?. method ,
49
- url : entry . request ?. url ,
50
- queryString : entry . request ?. queryString ,
51
- } ,
52
- response : {
53
- status : entry . response ?. status ,
54
- statusText : entry . response ?. statusText ,
55
- _error : entry . response ?. _error ,
56
- } ,
57
- serverIPAddress : entry . serverIPAddress ,
58
- time : entry . time ,
59
- } ) ) ,
60
- } ;
37
+
38
+ return failureEntries . length > 0
39
+ ? `Network Failures ( ${ failureEntries . length } found):\n ${ JSON . stringify ( failureEntries . map ( ( entry : any ) => ( {
40
+ startedDateTime : entry . startedDateTime ,
41
+ request : {
42
+ method : entry . request ?. method ,
43
+ url : entry . request ?. url ,
44
+ queryString : entry . request ?. queryString ,
45
+ } ,
46
+ response : {
47
+ status : entry . response ?. status ,
48
+ statusText : entry . response ?. statusText ,
49
+ _error : entry . response ?. _error ,
50
+ } ,
51
+ serverIPAddress : entry . serverIPAddress ,
52
+ time : entry . time ,
53
+ } ) ) , null , 2 ) } `
54
+ : "No network failures found" ;
61
55
}
62
56
63
57
// SESSION LOGS
64
58
export async function retrieveSessionFailures (
65
59
sessionId : string ,
66
- ) : Promise < LogResponse > {
60
+ ) : Promise < string > {
67
61
const url = `https://api.browserstack.com/automate/sessions/${ sessionId } /logs` ;
68
62
69
63
const response = await fetch ( url , {
@@ -73,17 +67,20 @@ export async function retrieveSessionFailures(
73
67
} ,
74
68
} ) ;
75
69
76
- const validationResult = validateLogResponse ( response , "session logs" ) ;
77
- if ( validationResult ) return validationResult ;
70
+ const validationError = validateLogResponse ( response , "session logs" ) ;
71
+ if ( validationError ) return validationError . message ! ;
78
72
79
73
const logText = await response . text ( ) ;
80
- return { logs : filterSessionFailures ( logText ) } ;
74
+ const logs = filterSessionFailures ( logText ) ;
75
+ return logs . length > 0
76
+ ? `Session Failures (${ logs . length } found):\n${ JSON . stringify ( logs , null , 2 ) } `
77
+ : "No session failures found" ;
81
78
}
82
79
83
80
// CONSOLE LOGS
84
81
export async function retrieveConsoleFailures (
85
82
sessionId : string ,
86
- ) : Promise < LogResponse > {
83
+ ) : Promise < string > {
87
84
const url = `https://api.browserstack.com/automate/sessions/${ sessionId } /consolelogs` ;
88
85
89
86
const response = await fetch ( url , {
@@ -93,11 +90,14 @@ export async function retrieveConsoleFailures(
93
90
} ,
94
91
} ) ;
95
92
96
- const validationResult = validateLogResponse ( response , "console logs" ) ;
97
- if ( validationResult ) return validationResult ;
93
+ const validationError = validateLogResponse ( response , "console logs" ) ;
94
+ if ( validationError ) return validationError . message ! ;
98
95
99
96
const logText = await response . text ( ) ;
100
- return { logs : filterConsoleFailures ( logText ) } ;
97
+ const logs = filterConsoleFailures ( logText ) ;
98
+ return logs . length > 0
99
+ ? `Console Failures (${ logs . length } found):\n${ JSON . stringify ( logs , null , 2 ) } `
100
+ : "No console failures found" ;
101
101
}
102
102
103
103
// FILTER: session logs
0 commit comments