Skip to content

Commit 64da8e7

Browse files
authored
Merge pull request #133 from parthlambdatest/Dot-3915
feat: add support for web and mobile configurations in options
2 parents 5d6435c + 9e29543 commit 64da8e7

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

src/types.ts

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

0 commit comments

Comments
 (0)