From 31632e3d4b95003417368e06d0141f0d48cfb2ed Mon Sep 17 00:00:00 2001 From: Pinank Solanki Date: Wed, 29 May 2024 19:26:21 +0530 Subject: [PATCH 1/2] Use enableJS config also for discovery page --- src/lib/processSnapshot.ts | 34 +++++++++++++++++++++------------- src/lib/server.ts | 27 +++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/lib/processSnapshot.ts b/src/lib/processSnapshot.ts index 772e358..1df9d3b 100644 --- a/src/lib/processSnapshot.ts +++ b/src/lib/processSnapshot.ts @@ -12,13 +12,18 @@ const MIN_VIEWPORT_HEIGHT = 1080; export default async (snapshot: Snapshot, ctx: Context): Promise> => { ctx.log.debug(`Processing snapshot ${snapshot.name}`); + let launchOptions: Record = { headless: true } + let contextOptions: Record = { + javaScriptEnabled: ctx.config.enableJavaScript, + userAgent: constants.CHROME_USER_AGENT, + } if (!ctx.browser) { - let launchOptions: Record = { headless: true } if (ctx.env.HTTP_PROXY || ctx.env.HTTPS_PROXY) launchOptions.proxy = { server: ctx.env.HTTP_PROXY || ctx.env.HTTPS_PROXY }; ctx.browser = await chromium.launch(launchOptions); ctx.log.debug(`Chromium launched with options ${JSON.stringify(launchOptions)}`); } - const context = await ctx.browser.newContext({userAgent: constants.CHROME_USER_AGENT}) + const context = await ctx.browser.newContext(contextOptions); + ctx.log.debug(`Browser context created with options ${JSON.stringify(contextOptions)}`); const page = await context.newPage(); let cache: Record = {}; @@ -132,15 +137,21 @@ export default async (snapshot: Snapshot, ctx: Context): Promise setTimeout(r, 1250)); - if (ctx.config.waitForTimeout) await page.waitForTimeout(ctx.config.waitForTimeout); - navigated = true; - ctx.log.debug(`Navigated to ${snapshot.url}`); + try { + // domcontentloaded event is more reliable than load event + await page.goto(snapshot.url, { waitUntil: "domcontentloaded"}); + // 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); + navigated = true; + ctx.log.debug(`Navigated to ${snapshot.url}`); + } catch (error: any) { + ctx.log.debug(`Navigation to discovery page failed; ${error}`) + throw new Error(error.message) + } + } - if (fullPage) await page.evaluate(scrollToBottomAndBackToTop); + if (ctx.config.enableJavaScript && fullPage) await page.evaluate(scrollToBottomAndBackToTop); try { await page.waitForLoadState('networkidle', { timeout: 5000 }); @@ -181,9 +192,6 @@ export default async (snapshot: Snapshot, ctx: Context): Promise { + let replyCode: number; + let replyBody: Record; + try { let { snapshot, testType } = request.body; if (!validateSnapshot(snapshot)) throw new Error(validateSnapshot.errors[0].message); let { processedSnapshot, warnings } = await processSnapshot(snapshot, ctx); await ctx.client.uploadSnapshot(ctx.build.id, processedSnapshot, testType, ctx.log); - - ctx.totalSnapshots++ - reply.code(200).send({data: { message: "success", warnings }}); + ctx.totalSnapshots++; + replyCode = 200; + replyBody = { data: { message: "success", warnings }}; } catch (error: any) { - return reply.code(500).send({ error: { message: error.message}}); + ctx.log.debug(`snapshot failed; ${error}`) + replyCode = 500; + replyBody = { error: { message: error.message }} + } + + // Close open browser contexts and pages + if (ctx.browser) { + for (let context of ctx.browser.contexts()) { + for (let page of context.pages()) { + await page.close(); + ctx.log.debug(`Closed browser page`); + } + await context.close(); + ctx.log.debug(`Closed browser context`); + } } + + return reply.code(replyCode).send(replyBody); }); From 6aecf1f9e905c490601ad541b83ca685b8d2159f Mon Sep 17 00:00:00 2001 From: Pinank Solanki Date: Wed, 29 May 2024 19:26:29 +0530 Subject: [PATCH 2/2] 3.0.9 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e300080..ed84e16 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lambdatest/smartui-cli", - "version": "3.0.8", + "version": "3.0.9", "description": "A command line interface (CLI) to run SmartUI tests on LambdaTest", "files": [ "dist/**/*"