From 6a544ac1524176b9ef819fdc12a8387adb53134f Mon Sep 17 00:00:00 2001 From: Pinank Solanki Date: Tue, 17 Sep 2024 13:21:58 +0530 Subject: [PATCH] Fixes * Add null check while setting cookies * Response from cache for already captured resources while discovery * Remove latest version additional description --- src/index.ts | 4 +- src/lib/processSnapshot.ts | 115 +++++++++++++++++++------------------ 2 files changed, 59 insertions(+), 60 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9b41b20..aaf40b7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,16 +12,14 @@ import pkgJSON from './../package.json' let log = logger; try { - let { data: { latestVersion, deprecated, additionalDescription, additionalDescriptionLatestVersion } } = await client.checkUpdate(log); + let { data: { latestVersion, deprecated, additionalDescription } } = await client.checkUpdate(log); log.info(`\nLambdaTest SmartUI CLI v${pkgJSON.version}`); log.info(chalk.yellow(`${additionalDescription}`)); if (deprecated){ log.warn(`This version is deprecated. A new version ${latestVersion} is available!`); - log.warn(`${additionalDescriptionLatestVersion}\n`); } else if (pkgJSON.version !== latestVersion){ log.info(chalk.green(`A new version ${latestVersion} is available!`)); - log.info(chalk.red(`${additionalDescriptionLatestVersion}\n`)); } else log.info(chalk.gray('https://www.npmjs.com/package/@lambdatest/smartui-cli\n')); } catch (error) { diff --git a/src/lib/processSnapshot.ts b/src/lib/processSnapshot.ts index 338851f..225ad1e 100644 --- a/src/lib/processSnapshot.ts +++ b/src/lib/processSnapshot.ts @@ -16,7 +16,7 @@ export default class Queue { private processing: boolean = false; private processingSnapshot: string = ''; private ctx: Context; - + constructor(ctx: Context) { this.ctx = ctx; } @@ -28,7 +28,7 @@ export default class Queue { this.processNext(); } } - + private async processNext(): Promise { if (!this.isEmpty()) { const snapshot = this.snapshots.shift(); @@ -37,10 +37,10 @@ export default class Queue { let { processedSnapshot, warnings } = await processSnapshot(snapshot, this.ctx); await this.ctx.client.uploadSnapshot(this.ctx, processedSnapshot); this.ctx.totalSnapshots++; - this.processedSnapshots.push({name: snapshot.name, warnings}); + this.processedSnapshots.push({ name: snapshot.name, warnings }); } catch (error: any) { this.ctx.log.debug(`snapshot failed; ${error}`); - this.processedSnapshots.push({name: snapshot.name, error: error.message}); + this.processedSnapshots.push({ name: snapshot.name, error: error.message }); } // Close open browser contexts and pages if (this.ctx.browser) { @@ -77,10 +77,10 @@ export default class Queue { } async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise> { - updateLogContext({task: 'discovery'}); - ctx.log.debug(`Processing snapshot ${snapshot.name}`); + updateLogContext({ task: 'discovery' }); + ctx.log.debug(`Processing snapshot ${snapshot.name} ${snapshot.url}`); - let launchOptions: Record = { headless: true } + let launchOptions: Record = { headless: false } let contextOptions: Record = { javaScriptEnabled: ctx.config.cliEnableJavaScript, userAgent: constants.CHROME_USER_AGENT, @@ -92,35 +92,44 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise { - if (!cookie) return null; - const [name, value] = cookie.split('='); - if (!name || !value) return null; - - return { - name: name.trim(), - value: value.trim(), - domain: domainName, - path: '/' - }; - }).filter(Boolean); - - - if (cookieArray && Array.isArray(cookieArray) && cookieArray.length > 0) { - await context.addCookies(cookieArray); - ctx.log.debug('Cookies added'); - } else { - ctx.log.debug('No valid cookies to add'); + if (snapshot.dom.cookies) { + const domainName = new URL(snapshot.url).hostname; + ctx.log.debug(`Setting cookies for domain: ${domainName}`); + + const cookieArray = snapshot.dom.cookies.split('; ').map(cookie => { + if (!cookie) return null; + const [name, value] = cookie.split('='); + if (!name || !value) return null; + + return { + name: name.trim(), + value: value.trim(), + domain: domainName, + path: '/' + }; + }).filter(Boolean); + + if (cookieArray.length > 0) { + await context.addCookies(cookieArray); + } else { + ctx.log.debug('No valid cookies to add'); + } } - const page = await context.newPage(); + + // populate cache with already captured resources let cache: Record = {}; + if (snapshot.dom.resources.length) { + for (let resource of snapshot.dom.resources) { + // convert text/css content to base64 + let body = resource.mimetype == 'text/css' ? Buffer.from(resource.content).toString('base64') : resource.content; + cache[resource.url] = { + body: body, + type: resource.mimetype + } + } + } // Use route to intercept network requests and discover resources await page.route('**/*', async (route, request) => { @@ -138,7 +147,7 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise 200, - headers: () => ({ 'content-type': 'text/html' }) + headers: () => ({ 'content-type': 'text/html' }) + } + body = snapshot.dom.html; + } else if (cache[requestUrl]) { + response = { + status: () => 200, + headers: () => ({ 'content-type': cache[requestUrl].mimetype }) } - body = snapshot.dom.html + body = cache[requestUrl].body; } else { response = await page.request.fetch(request, requestOptions); body = await response.body(); @@ -162,7 +177,7 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise>): boolean => { for (let key in obj) if (obj[key]?.length) return true; return false; @@ -240,7 +255,7 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise setTimeout(r, 1250)); if (ctx.config.waitForTimeout) await page.waitForTimeout(ctx.config.waitForTimeout); @@ -278,7 +293,7 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise setTimeout(r, 1000)); - // snapshot options if (processedOptions.element) { let l = await page.locator(processedOptions.element).all() @@ -323,18 +336,6 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise