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