Skip to content

Commit e3d304b

Browse files
add support for web and mobile in options
1 parent 17f8186 commit e3d304b

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

src/lib/processSnapshot.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,45 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise<Record
225225
return false;
226226
}
227227

228+
if (options.web && Object.keys(options.web).length) {
229+
processedOptions.web = {};
230+
231+
// Check and process viewports in web
232+
if (options.web.viewports && Array.isArray(options.web.viewports) && options.web.viewports.length > 0) {
233+
processedOptions.web.viewports = options.web.viewports.filter(viewport =>
234+
Array.isArray(viewport) && viewport.length > 0
235+
);
236+
}
237+
238+
// Check and process browsers in web
239+
if (options.web.browsers && Array.isArray(options.web.browsers) && options.web.browsers.length > 0) {
240+
processedOptions.web.browsers = options.web.browsers;
241+
}
242+
}
243+
244+
if (options.mobile && Object.keys(options.mobile).length) {
245+
processedOptions.mobile = {};
246+
247+
// Check and process devices in mobile
248+
if (options.mobile.devices && Array.isArray(options.mobile.devices) && options.mobile.devices.length > 0) {
249+
processedOptions.mobile.devices = options.mobile.devices;
250+
}
251+
252+
// Check if 'fullPage' is provided and is a boolean, otherwise set default to true
253+
if ('fullPage' in options.mobile && typeof options.mobile.fullPage === 'boolean') {
254+
processedOptions.mobile.fullPage = options.mobile.fullPage;
255+
} else {
256+
processedOptions.mobile.fullPage = true; // Default value for fullPage
257+
}
258+
259+
// Check if 'orientation' is provided and is valid, otherwise set default to 'portrait'
260+
if ('orientation' in options.mobile && (options.mobile.orientation === 'portrait' || options.mobile.orientation === 'landscape')) {
261+
processedOptions.mobile.orientation = options.mobile.orientation;
262+
} else {
263+
processedOptions.mobile.orientation = 'portrait'; // Default value for orientation
264+
}
265+
}
266+
228267
if (options.element && Object.keys(options.element).length) {
229268
if (options.element.id) processedOptions.element = '#' + options.element.id;
230269
else if (options.element.class) processedOptions.element = '.' + options.element.class;
@@ -334,6 +373,7 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise<Record
334373
});
335374
}
336375
}
376+
ctx.log.debug(`Processed options: ${JSON.stringify(processedOptions)}`);
337377
}
338378

339379
return {

src/lib/schemaValidation.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,64 @@ const SnapshotSchema: JSONSchemaType<Snapshot> = {
285285
errorMessage: "Invalid snapshot options; selectDOM xpath array must have unique and non-empty items"
286286
},
287287
}
288+
},
289+
web: {
290+
type: "object",
291+
properties: {
292+
browsers: {
293+
type: "array",
294+
items: {
295+
type: "string",
296+
enum: [constants.CHROME, constants.FIREFOX, constants.SAFARI, constants.EDGE],
297+
minLength: 1
298+
},
299+
uniqueItems: true,
300+
errorMessage: `Invalid snapshot options; allowed browsers - ${constants.CHROME}, ${constants.FIREFOX}, ${constants.SAFARI}, ${constants.EDGE}`
301+
},
302+
viewports: {
303+
type: "array",
304+
items: {
305+
type: "array",
306+
items: {
307+
type: "number",
308+
minimum: 1
309+
},
310+
minItems: 1,
311+
maxItems: 2,
312+
errorMessage: "Invalid snapshot options; each viewport array must contain either a single width or a width and height tuple with positive values."
313+
},
314+
uniqueItems: true,
315+
errorMessage: "Invalid snapshot options; viewports must be an array of unique arrays."
316+
}
317+
},
318+
required: ["browsers", "viewports"],
319+
errorMessage: "Invalid snapshot options; web must include both browsers and viewports properties."
320+
},
321+
mobile: {
322+
type: "object",
323+
properties: {
324+
devices: {
325+
type: "array",
326+
items: {
327+
type: "string",
328+
enum: Object.keys(constants.SUPPORTED_MOBILE_DEVICES),
329+
minLength: 1
330+
},
331+
uniqueItems: true,
332+
errorMessage: "Invalid snapshot options; devices must be an array of unique supported mobile devices."
333+
},
334+
fullPage: {
335+
type: "boolean",
336+
errorMessage: "Invalid snapshot options; fullPage must be a boolean."
337+
},
338+
orientation: {
339+
type: "string",
340+
enum: [constants.MOBILE_ORIENTATION_PORTRAIT, constants.MOBILE_ORIENTATION_LANDSCAPE],
341+
errorMessage: "Invalid snapshot options; orientation must be either 'portrait' or 'landscape'."
342+
}
343+
},
344+
required: ["devices"],
345+
errorMessage: "Invalid snapshot options; mobile must include devices property."
288346
}
289347
},
290348
additionalProperties: false

src/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ export interface Snapshot {
8585
class?: string,
8686
cssSelector?: string,
8787
xpath?: string
88+
},
89+
web?: {
90+
browsers: string[],
91+
viewports: ([number] | [number, number])[]
92+
},
93+
mobile?: {
94+
devices: string[],
95+
fullPage?: boolean,
96+
orientation?: string
8897
}
8998
}
9099
}

0 commit comments

Comments
 (0)