Skip to content

Commit 6107e48

Browse files
authored
Merge pull request #118 from parthlambdatest/Dot_3790
feat: addition of cookies and basic auth
2 parents 93919fb + 078f271 commit 6107e48

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

src/lib/ctx.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Context, Env, WebConfig, MobileConfig } from '../types.js'
1+
import { Context, Env, WebConfig, MobileConfig, basicAuth } from '../types.js'
22
import constants from './constants.js'
33
import { version } from '../../package.json'
44
import { validateConfig } from './schemaValidation.js'
@@ -11,6 +11,7 @@ export default (options: Record<string, string>): Context => {
1111
let env: Env = getEnv();
1212
let webConfig: WebConfig;
1313
let mobileConfig: MobileConfig;
14+
let basicAuthObj: basicAuth
1415
let config = constants.DEFAULT_CONFIG;
1516
let port: number;
1617
let resolutionOff: boolean;
@@ -52,10 +53,13 @@ export default (options: Record<string, string>): Context => {
5253
if (config.mobile) {
5354
mobileConfig = {
5455
devices: config.mobile.devices,
55-
fullPage: config.mobile.fullPage || true,
56+
fullPage: config.mobile.fullPage ?? true,
5657
orientation: config.mobile.orientation || constants.MOBILE_ORIENTATION_PORTRAIT,
5758
}
5859
}
60+
if (config.basicAuthorization){
61+
basicAuthObj = config.basicAuthorization
62+
}
5963

6064
return {
6165
env: env,
@@ -69,7 +73,8 @@ export default (options: Record<string, string>): Context => {
6973
enableJavaScript: config.enableJavaScript || false,
7074
cliEnableJavaScript: config.cliEnableJavaScript || true,
7175
scrollTime: config.scrollTime || constants.DEFAULT_SCROLL_TIME,
72-
allowedHostnames: config.allowedHostnames || []
76+
allowedHostnames: config.allowedHostnames || [],
77+
basicAuthorization: basicAuthObj
7378
},
7479
uploadFilePath: '',
7580
webStaticConfig: [],

src/lib/processSnapshot.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise<Record
9292
}
9393
const context = await ctx.browser.newContext(contextOptions);
9494
ctx.log.debug(`Browser context created with options ${JSON.stringify(contextOptions)}`);
95+
96+
// Setting the cookies in playwright context
97+
const domainName = new URL(snapshot.url).hostname;
98+
ctx.log.debug('Setting cookies in context for domain:', domainName);
99+
const cookieArray = snapshot.dom.cookies.split('; ').map(cookie => {
100+
const [name, value] = cookie.split('=');
101+
return {
102+
name: name.trim(),
103+
value: value.trim(),
104+
domain: domainName,
105+
path: '/'
106+
};
107+
});
108+
await context.addCookies(cookieArray);
109+
95110
const page = await context.newPage();
96111
let cache: Record<string, any> = {};
97112

@@ -110,11 +125,24 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise<Record
110125
ctx.config.allowedHostnames.push(new URL(snapshot.url).hostname);
111126
if (ctx.config.enableJavaScript) ALLOWED_RESOURCES.push('script');
112127

128+
let requestOptions: Record<string, any> = {
129+
timeout: REQUEST_TIMEOUT
130+
}
131+
if (requestUrl === snapshot.url && ctx.config.basicAuthorization ) {
132+
ctx.log.debug(`Adding basic authorization to the headers for root url`);
133+
let token = Buffer.from(`${ctx.config.basicAuthorization.username}:${ctx.config.basicAuthorization.password}`).toString('base64');
134+
requestOptions.headers = {
135+
...request.headers(),
136+
Authorization: `Basic ${token}`
137+
};
138+
}
139+
113140
const response = await page.request.fetch(request, { timeout: REQUEST_TIMEOUT });
114141
const body = await response.body();
142+
115143
if (!body) {
116144
ctx.log.debug(`Handling request ${requestUrl}\n - skipping no response`);
117-
} else if (!body.length) {
145+
} else if (!body.length) {
118146
ctx.log.debug(`Handling request ${requestUrl}\n - skipping empty response`);
119147
} else if (requestUrl === snapshot.url) {
120148
ctx.log.debug(`Handling request ${requestUrl}\n - skipping root resource`);

src/lib/schemaValidation.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ const ConfigSchema = {
133133
uniqueItems: "Invalid config; duplicates in allowedHostnames"
134134
}
135135

136+
},
137+
basicAuthorization: {
138+
type: "object",
139+
properties: {
140+
username: {
141+
type: "string",
142+
errorMessage: "Invalid config; username is mandatory"
143+
},
144+
password: {
145+
type: "string",
146+
errorMessage: "Invalid config; password is mandatory"
147+
},
148+
}
136149
}
137150
},
138151
anyOf: [

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface Context {
2323
cliEnableJavaScript: boolean;
2424
scrollTime: number;
2525
allowedHostnames: Array<string>;
26+
basicAuthorization: basicAuth | undefined
2627
};
2728
uploadFilePath: string;
2829
webStaticConfig: WebStaticConfig;
@@ -141,3 +142,8 @@ export type FigmaDesignConfig = {
141142
depth: number;
142143
figma_config: FigmaConfigItem[];
143144
};
145+
146+
export interface basicAuth {
147+
username: string;
148+
password: string;
149+
}

0 commit comments

Comments
 (0)