11import { Snapshot , Context , ProcessedSnapshot } from "../types.js" ;
2- import { scrollToBottomAndBackToTop , getRenderViewports } from "./utils.js"
2+ import { scrollToBottomAndBackToTop , getRenderViewports , getRenderViewportsForOptions } from "./utils.js"
33import { chromium , Locator } from "@playwright/test"
44import constants from "./constants.js" ;
55import { updateLogContext } from '../lib/logger.js'
@@ -10,73 +10,7 @@ const ALLOWED_STATUSES = [200, 201];
1010const REQUEST_TIMEOUT = 10000 ;
1111const MIN_VIEWPORT_HEIGHT = 1080 ;
1212
13- export default class Queue {
14- private snapshots : Array < Snapshot > = [ ] ;
15- private processedSnapshots : Array < Record < string , any > > = [ ] ;
16- private processing : boolean = false ;
17- private processingSnapshot : string = '' ;
18- private ctx : Context ;
19-
20- constructor ( ctx : Context ) {
21- this . ctx = ctx ;
22- }
23-
24- enqueue ( item : Snapshot ) : void {
25- this . snapshots . push ( item ) ;
26- if ( ! this . processing ) {
27- this . processing = true ;
28- this . processNext ( ) ;
29- }
30- }
31-
32- private async processNext ( ) : Promise < void > {
33- if ( ! this . isEmpty ( ) ) {
34- const snapshot = this . snapshots . shift ( ) ;
35- try {
36- this . processingSnapshot = snapshot ?. name ;
37- let { processedSnapshot, warnings } = await processSnapshot ( snapshot , this . ctx ) ;
38- await this . ctx . client . uploadSnapshot ( this . ctx , processedSnapshot ) ;
39- this . ctx . totalSnapshots ++ ;
40- this . processedSnapshots . push ( { name : snapshot . name , warnings } ) ;
41- } catch ( error : any ) {
42- this . ctx . log . debug ( `snapshot failed; ${ error } ` ) ;
43- this . processedSnapshots . push ( { name : snapshot . name , error : error . message } ) ;
44- }
45- // Close open browser contexts and pages
46- if ( this . ctx . browser ) {
47- for ( let context of this . ctx . browser . contexts ( ) ) {
48- for ( let page of context . pages ( ) ) {
49- await page . close ( ) ;
50- this . ctx . log . debug ( `Closed browser page for snapshot ${ snapshot . name } ` ) ;
51- }
52- await context . close ( ) ;
53- this . ctx . log . debug ( `Closed browser context for snapshot ${ snapshot . name } ` ) ;
54- }
55- }
56- this . processNext ( ) ;
57- } else {
58- this . processing = false ;
59- }
60- }
61-
62- isProcessing ( ) : boolean {
63- return this . processing ;
64- }
65-
66- getProcessingSnapshot ( ) : string {
67- return this . processingSnapshot ;
68- }
69-
70- getProcessedSnapshots ( ) : Array < Record < string , any > > {
71- return this . processedSnapshots ;
72- }
73-
74- isEmpty ( ) : boolean {
75- return this . snapshots && this . snapshots . length ? false : true ;
76- }
77- }
78-
79- async function processSnapshot ( snapshot : Snapshot , ctx : Context ) : Promise < Record < string , any > > {
13+ export default async function processSnapshot ( snapshot : Snapshot , ctx : Context ) : Promise < Record < string , any > > {
8014 updateLogContext ( { task : 'discovery' } ) ;
8115 ctx . log . debug ( `Processing snapshot ${ snapshot . name } ${ snapshot . url } ` ) ;
8216
@@ -231,6 +165,45 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise<Record
231165 return false ;
232166 }
233167
168+ if ( options . web && Object . keys ( options . web ) . length ) {
169+ processedOptions . web = { } ;
170+
171+ // Check and process viewports in web
172+ if ( options . web . viewports && options . web . viewports . length > 0 ) {
173+ processedOptions . web . viewports = options . web . viewports . filter ( viewport =>
174+ Array . isArray ( viewport ) && viewport . length > 0
175+ ) ;
176+ }
177+
178+ // Check and process browsers in web
179+ if ( options . web . browsers && options . web . browsers . length > 0 ) {
180+ processedOptions . web . browsers = options . web . browsers ;
181+ }
182+ }
183+
184+ if ( options . mobile && Object . keys ( options . mobile ) . length ) {
185+ processedOptions . mobile = { } ;
186+
187+ // Check and process devices in mobile
188+ if ( options . mobile . devices && options . mobile . devices . length > 0 ) {
189+ processedOptions . mobile . devices = options . mobile . devices ;
190+ }
191+
192+ // Check if 'fullPage' is provided and is a boolean, otherwise set default to true
193+ if ( options . mobile . hasOwnProperty ( 'fullPage' ) && typeof options . mobile . fullPage === 'boolean' ) {
194+ processedOptions . mobile . fullPage = options . mobile . fullPage ;
195+ } else {
196+ processedOptions . mobile . fullPage = true ; // Default value for fullPage
197+ }
198+
199+ // Check if 'orientation' is provided and is valid, otherwise set default to 'portrait'
200+ if ( options . mobile . hasOwnProperty ( 'orientation' ) && ( options . mobile . orientation === constants . MOBILE_ORIENTATION_PORTRAIT || options . mobile . orientation === constants . MOBILE_ORIENTATION_LANDSCAPE ) ) {
201+ processedOptions . mobile . orientation = options . mobile . orientation ;
202+ } else {
203+ processedOptions . mobile . orientation = constants . MOBILE_ORIENTATION_PORTRAIT ; // Default value for orientation
204+ }
205+ }
206+
234207 if ( options . element && Object . keys ( options . element ) . length ) {
235208 if ( options . element . id ) processedOptions . element = '#' + options . element . id ;
236209 else if ( options . element . class ) processedOptions . element = '.' + options . element . class ;
@@ -268,7 +241,14 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise<Record
268241 // process for every viewport
269242 let navigated : boolean = false ;
270243 let previousDeviceType : string | null = null ;
271- let renderViewports = getRenderViewports ( ctx ) ;
244+
245+ let renderViewports ;
246+
247+ if ( ( snapshot . options && snapshot . options . web ) || ( snapshot . options && snapshot . options . mobile ) ) {
248+ renderViewports = getRenderViewportsForOptions ( snapshot . options )
249+ } else {
250+ renderViewports = getRenderViewports ( ctx ) ;
251+ }
272252
273253 for ( const { viewport, viewportString, fullPage, device } of renderViewports ) {
274254
@@ -340,6 +320,7 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise<Record
340320 } ) ;
341321 }
342322 }
323+ ctx . log . debug ( `Processed options: ${ JSON . stringify ( processedOptions ) } ` ) ;
343324 }
344325
345326 return {
0 commit comments