Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions src/lib/processSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,47 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
ctx.log.debug('No valid cookies to add');
}
}

let options = snapshot.options;

// Custom cookies include those which cannot be captured by javascript function `document.cookie` like httpOnly, secure, sameSite etc.
// These custom cookies will be captured by the user in their automation browser and sent to CLI through the snapshot options using `customCookies` field.
if (options?.customCookies && Array.isArray(options.customCookies) && options.customCookies.length > 0) {
ctx.log.debug(`Setting ${options.customCookies.length} custom cookies`);

const validCustomCookies = options.customCookies.filter(cookie => {
if (!cookie.name || !cookie.value || !cookie.domain) {
ctx.log.debug(`Skipping invalid custom cookie: missing required fields (name, value, or domain)`);
return false;
}

if (cookie.sameSite && !['Strict', 'Lax', 'None'].includes(cookie.sameSite)) {
ctx.log.debug(`Skipping invalid custom cookie: invalid sameSite value '${cookie.sameSite}'`);
return false;
}

return true;
}).map(cookie => ({
name: cookie.name,
value: cookie.value,
domain: cookie.domain,
path: cookie.path || '/',
httpOnly: cookie.httpOnly || false,
secure: cookie.secure || false,
sameSite: cookie.sameSite || 'Lax'
}));

if (validCustomCookies.length > 0) {
try {
await context.addCookies(validCustomCookies);
ctx.log.debug(`Successfully added ${validCustomCookies.length} custom cookies`);
} catch (error) {
ctx.log.debug(`Failed to add custom cookies: ${error}`);
}
} else {
ctx.log.debug('No valid custom cookies to add');
}
}
const page = await context.newPage();

// populate cache with already captured resources
Expand Down Expand Up @@ -415,8 +456,6 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
route.abort();
}
});

let options = snapshot.options;
let optionWarnings: Set<string> = new Set();
let selectors: Array<string> = [];
let ignoreOrSelectDOM: string;
Expand Down Expand Up @@ -582,6 +621,7 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
// adding extra timeout since domcontentloaded event is fired pretty quickly
await new Promise(r => setTimeout(r, 1250));
if (ctx.config.waitForTimeout) await page.waitForTimeout(ctx.config.waitForTimeout);
await page.waitForLoadState("networkidle", { timeout: 10000 }).catch(() => { ctx.log.debug('networkidle event failed to fire within 10s') });
navigated = true;
ctx.log.debug(`Navigated to ${snapshot.url}`);
} catch (error: any) {
Expand Down Expand Up @@ -815,7 +855,6 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):

if (hasBrowserErrors) {
discoveryErrors.timestamp = new Date().toISOString();
// ctx.log.warn(discoveryErrors);
}

if (ctx.config.useGlobalCache) {
Expand Down
8 changes: 8 additions & 0 deletions src/lib/schemaValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,14 @@ const SnapshotSchema: JSONSchemaType<Snapshot> = {
minimum: 0,
maximum: 100,
errorMessage: "Invalid snapshot options; rejectionThreshold must be a number between 0 and 100"
},
customCookies: {
type: "array",
items: {
type: "object",
minProperties: 1,
},
errorMessage: "Invalid snapshot options; customCookies must be an array of objects with string properties"
}
},
additionalProperties: false
Expand Down
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export interface Snapshot {
useExtendedViewport?: boolean;
approvalThreshold?: number;
rejectionThreshold?: number;
customCookies?: CustomCookie[];
}
}

Expand Down Expand Up @@ -251,6 +252,16 @@ export interface FigmaWebConfig {
configs: Array<{ figma_file_token: string, figma_ids: Array<string>, screenshot_names:Array<string> }>;
}

export interface CustomCookie {
name: string;
value: string;
domain: string;
path: string;
httpOnly: boolean;
secure: boolean;
sameSite: 'Strict' | 'Lax' | 'None';
}


export interface ViewportErrors {
statusCode: "aborted" | "404" | string;
Expand Down