Skip to content

Commit

Permalink
Merge pull request #93 from pinanks/DOT-3399
Browse files Browse the repository at this point in the history
Release v3.0.9
  • Loading branch information
arushsaxena1998 authored May 29, 2024
2 parents d130b1b + 6aecf1f commit aa9ef73
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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/**/*"
Expand Down
34 changes: 21 additions & 13 deletions src/lib/processSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ const MIN_VIEWPORT_HEIGHT = 1080;
export default async (snapshot: Snapshot, ctx: Context): Promise<Record<string, any>> => {
ctx.log.debug(`Processing snapshot ${snapshot.name}`);

let launchOptions: Record<string, any> = { headless: true }
let contextOptions: Record<string, any> = {
javaScriptEnabled: ctx.config.enableJavaScript,
userAgent: constants.CHROME_USER_AGENT,
}
if (!ctx.browser) {
let launchOptions: Record<string, any> = { 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<string, any> = {};

Expand Down Expand Up @@ -132,15 +137,21 @@ export default async (snapshot: Snapshot, ctx: Context): Promise<Record<string,

// navigate to snapshot url once
if (!navigated) {
// 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}`);
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 });
Expand Down Expand Up @@ -181,9 +192,6 @@ export default async (snapshot: Snapshot, ctx: Context): Promise<Record<string,
}
}

await page.close();
await context.close();

// add dom resources to cache
if (snapshot.dom.resources.length) {
for (let resource of snapshot.dom.resources) {
Expand Down
27 changes: 23 additions & 4 deletions src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,36 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes

// upload snpashot
server.post('/snapshot', opts, async (request, reply) => {
let replyCode: number;
let replyBody: Record<string, any>;

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);
});


Expand Down

0 comments on commit aa9ef73

Please sign in to comment.