@@ -12,6 +12,162 @@ const ALLOWED_STATUSES = [200, 201];
1212const REQUEST_TIMEOUT = 1800000 ;
1313const MIN_VIEWPORT_HEIGHT = 1080 ;
1414
15+ export async function prepareSnapshot ( snapshot : Snapshot , ctx : Context ) : Promise < Record < string , any > > {
16+ let processedOptions : Record < string , any > = { } ;
17+ processedOptions . cliEnableJavascript = ctx . config . cliEnableJavaScript ;
18+ processedOptions . ignoreHTTPSErrors = ctx . config . ignoreHTTPSErrors ;
19+ if ( ctx . config . basicAuthorization ) {
20+ processedOptions . basicAuthorization = ctx . config . basicAuthorization ;
21+ }
22+ ctx . config . allowedHostnames . push ( new URL ( snapshot . url ) . hostname ) ;
23+ processedOptions . allowedHostnames = ctx . config . allowedHostnames ;
24+ processedOptions . skipCapturedCookies = ctx . env . SMARTUI_DO_NOT_USE_CAPTURED_COOKIES ;
25+
26+ if ( ctx . env . HTTP_PROXY || ctx . env . HTTPS_PROXY ) processedOptions . proxy = { server : ctx . env . HTTP_PROXY || ctx . env . HTTPS_PROXY } ;
27+ if ( ctx . env . SMARTUI_HTTP_PROXY || ctx . env . SMARTUI_HTTPS_PROXY ) processedOptions . proxy = { server : ctx . env . SMARTUI_HTTP_PROXY || ctx . env . SMARTUI_HTTPS_PROXY } ;
28+
29+ let options = snapshot . options ;
30+ let optionWarnings : Set < string > = new Set ( ) ;
31+ let selectors : Array < string > = [ ] ;
32+ let ignoreOrSelectDOM : string ;
33+ let ignoreOrSelectBoxes : string ;
34+
35+ if ( options && Object . keys ( options ) . length ) {
36+ ctx . log . debug ( `Snapshot options: ${ JSON . stringify ( options ) } ` ) ;
37+
38+ const isNotAllEmpty = ( obj : Record < string , Array < string > > ) : boolean => {
39+ for ( let key in obj ) if ( obj [ key ] ?. length ) return true ;
40+ return false ;
41+ }
42+
43+ if ( options . loadDomContent ) {
44+ processedOptions . loadDomContent = true ;
45+ }
46+
47+ if ( options . sessionId ) {
48+ const sessionId = options . sessionId ;
49+ processedOptions . sessionId = sessionId
50+ if ( ctx . sessionCapabilitiesMap && ctx . sessionCapabilitiesMap . has ( sessionId ) ) {
51+ const sessionCapabilities = ctx . sessionCapabilitiesMap . get ( sessionId ) ;
52+ if ( sessionCapabilities && sessionCapabilities . id ) {
53+ processedOptions . testId = sessionCapabilities . id ;
54+ }
55+ }
56+ }
57+
58+ if ( options . web && Object . keys ( options . web ) . length ) {
59+ processedOptions . web = { } ;
60+
61+ // Check and process viewports in web
62+ if ( options . web . viewports && options . web . viewports . length > 0 ) {
63+ processedOptions . web . viewports = options . web . viewports . filter ( viewport =>
64+ Array . isArray ( viewport ) && viewport . length > 0
65+ ) ;
66+ }
67+
68+ // Check and process browsers in web
69+ if ( options . web . browsers && options . web . browsers . length > 0 ) {
70+ processedOptions . web . browsers = options . web . browsers ;
71+ }
72+ }
73+
74+ if ( options . mobile && Object . keys ( options . mobile ) . length ) {
75+ processedOptions . mobile = { } ;
76+
77+ // Check and process devices in mobile
78+ if ( options . mobile . devices && options . mobile . devices . length > 0 ) {
79+ processedOptions . mobile . devices = options . mobile . devices ;
80+ }
81+
82+ // Check if 'fullPage' is provided and is a boolean, otherwise set default to true
83+ if ( options . mobile . hasOwnProperty ( 'fullPage' ) && typeof options . mobile . fullPage === 'boolean' ) {
84+ processedOptions . mobile . fullPage = options . mobile . fullPage ;
85+ } else {
86+ processedOptions . mobile . fullPage = true ; // Default value for fullPage
87+ }
88+
89+ // Check if 'orientation' is provided and is valid, otherwise set default to 'portrait'
90+ if ( options . mobile . hasOwnProperty ( 'orientation' ) && ( options . mobile . orientation === constants . MOBILE_ORIENTATION_PORTRAIT || options . mobile . orientation === constants . MOBILE_ORIENTATION_LANDSCAPE ) ) {
91+ processedOptions . mobile . orientation = options . mobile . orientation ;
92+ } else {
93+ processedOptions . mobile . orientation = constants . MOBILE_ORIENTATION_PORTRAIT ; // Default value for orientation
94+ }
95+ }
96+
97+ if ( options . element && Object . keys ( options . element ) . length ) {
98+ if ( options . element . id ) processedOptions . element = '#' + options . element . id ;
99+ else if ( options . element . class ) processedOptions . element = '.' + options . element . class ;
100+ else if ( options . element . cssSelector ) processedOptions . element = options . element . cssSelector ;
101+ else if ( options . element . xpath ) processedOptions . element = 'xpath=' + options . element . xpath ;
102+ } else if ( options . ignoreDOM && Object . keys ( options . ignoreDOM ) . length && isNotAllEmpty ( options . ignoreDOM ) ) {
103+ processedOptions . ignoreBoxes = { } ;
104+ ignoreOrSelectDOM = 'ignoreDOM' ;
105+ ignoreOrSelectBoxes = 'ignoreBoxes' ;
106+ } else if ( options . selectDOM && Object . keys ( options . selectDOM ) . length && isNotAllEmpty ( options . selectDOM ) ) {
107+ processedOptions . selectBoxes = { } ;
108+ ignoreOrSelectDOM = 'selectDOM' ;
109+ ignoreOrSelectBoxes = 'selectBoxes' ;
110+ }
111+ if ( ignoreOrSelectDOM ) {
112+ for ( const [ key , value ] of Object . entries ( options [ ignoreOrSelectDOM ] ) ) {
113+ switch ( key ) {
114+ case 'id' :
115+ selectors . push ( ...value . map ( e => '#' + e ) ) ;
116+ break ;
117+ case 'class' :
118+ selectors . push ( ...value . map ( e => '.' + e ) ) ;
119+ break ;
120+ case 'xpath' :
121+ selectors . push ( ...value . map ( e => 'xpath=' + e ) ) ;
122+ break ;
123+ case 'cssSelector' :
124+ selectors . push ( ...value ) ;
125+ break ;
126+ }
127+ }
128+ }
129+ if ( options . ignoreType ) {
130+ processedOptions . ignoreType = options . ignoreType ;
131+ }
132+ }
133+
134+ if ( ctx . config . tunnel ) {
135+ if ( ctx . tunnelDetails && ctx . tunnelDetails . tunnelPort != - 1 && ctx . tunnelDetails . tunnelHost != '' ) {
136+ const tunnelAddress = `http://${ ctx . tunnelDetails . tunnelHost } :${ ctx . tunnelDetails . tunnelPort } ` ;
137+ processedOptions . tunnelAddress = tunnelAddress ;
138+ ctx . log . debug ( `Tunnel address added to processedOptions: ${ tunnelAddress } ` ) ;
139+ }
140+ }
141+
142+ processedOptions . allowedAssets = ctx . config . allowedAssets ;
143+ processedOptions . selectors = selectors ;
144+
145+ processedOptions . ignoreDOM = options ?. ignoreDOM ;
146+ processedOptions . selectDOM = options ?. selectDOM ;
147+ ctx . log . debug ( `Processed options: ${ JSON . stringify ( processedOptions ) } ` ) ;
148+
149+ let renderViewports ;
150+ if ( ( snapshot . options && snapshot . options . web ) || ( snapshot . options && snapshot . options . mobile ) ) {
151+ renderViewports = getRenderViewportsForOptions ( snapshot . options )
152+ } else {
153+ renderViewports = getRenderViewports ( ctx ) ;
154+ }
155+
156+ processedOptions . doRemoteDiscovery = true ;
157+ return {
158+ processedSnapshot : {
159+ name : snapshot . name ,
160+ url : snapshot . url ,
161+ dom : Buffer . from ( snapshot . dom . html ) . toString ( 'base64' ) ,
162+ resources : { } ,
163+ options : processedOptions ,
164+ cookies : Buffer . from ( snapshot . dom . cookies ) . toString ( 'base64' ) ,
165+ renderViewports : renderViewports ,
166+ } ,
167+ warnings : [ ...optionWarnings , ...snapshot . dom . warnings ] ,
168+ }
169+ }
170+
15171export default async function processSnapshot ( snapshot : Snapshot , ctx : Context ) : Promise < Record < string , any > > {
16172 updateLogContext ( { task : 'discovery' } ) ;
17173 ctx . log . debug ( `Processing snapshot ${ snapshot . name } ${ snapshot . url } ` ) ;
@@ -24,6 +180,8 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
24180 browsers : { }
25181 } ;
26182
183+ let processedOptions : Record < string , any > = { } ;
184+
27185 let globalViewport = ""
28186 let globalBrowser = constants . CHROME
29187 let launchOptions : Record < string , any > = {
@@ -235,7 +393,6 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
235393
236394 let options = snapshot . options ;
237395 let optionWarnings : Set < string > = new Set ( ) ;
238- let processedOptions : Record < string , any > = { } ;
239396 let selectors : Array < string > = [ ] ;
240397 let ignoreOrSelectDOM : string ;
241398 let ignoreOrSelectBoxes : string ;
@@ -419,6 +576,7 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
419576 ctx . log . debug ( `Network idle failed due to ${ error } ` ) ;
420577 }
421578
579+
422580 if ( ctx . config . allowedAssets && ctx . config . allowedAssets . length ) {
423581 for ( let assetUrl of ctx . config . allowedAssets ) {
424582 if ( ! cache [ assetUrl ] ) {
0 commit comments