diff --git a/package.json b/package.json index 1863ea5..2801539 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lambdatest/smartui-cli", - "version": "4.0.3", + "version": "4.0.4", "description": "A command line interface (CLI) to run SmartUI tests on LambdaTest", "files": [ "dist/**/*" 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..b7ba433 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,8 +77,8 @@ 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 contextOptions: Record = { @@ -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