Skip to content

Commit aa9ef73

Browse files
Merge pull request #93 from pinanks/DOT-3399
Release v3.0.9
2 parents d130b1b + 6aecf1f commit aa9ef73

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lambdatest/smartui-cli",
3-
"version": "3.0.8",
3+
"version": "3.0.9",
44
"description": "A command line interface (CLI) to run SmartUI tests on LambdaTest",
55
"files": [
66
"dist/**/*"

src/lib/processSnapshot.ts

+21-13
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@ const MIN_VIEWPORT_HEIGHT = 1080;
1212
export default async (snapshot: Snapshot, ctx: Context): Promise<Record<string, any>> => {
1313
ctx.log.debug(`Processing snapshot ${snapshot.name}`);
1414

15+
let launchOptions: Record<string, any> = { headless: true }
16+
let contextOptions: Record<string, any> = {
17+
javaScriptEnabled: ctx.config.enableJavaScript,
18+
userAgent: constants.CHROME_USER_AGENT,
19+
}
1520
if (!ctx.browser) {
16-
let launchOptions: Record<string, any> = { headless: true }
1721
if (ctx.env.HTTP_PROXY || ctx.env.HTTPS_PROXY) launchOptions.proxy = { server: ctx.env.HTTP_PROXY || ctx.env.HTTPS_PROXY };
1822
ctx.browser = await chromium.launch(launchOptions);
1923
ctx.log.debug(`Chromium launched with options ${JSON.stringify(launchOptions)}`);
2024
}
21-
const context = await ctx.browser.newContext({userAgent: constants.CHROME_USER_AGENT})
25+
const context = await ctx.browser.newContext(contextOptions);
26+
ctx.log.debug(`Browser context created with options ${JSON.stringify(contextOptions)}`);
2227
const page = await context.newPage();
2328
let cache: Record<string, any> = {};
2429

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

133138
// navigate to snapshot url once
134139
if (!navigated) {
135-
// domcontentloaded event is more reliable than load event
136-
await page.goto(snapshot.url, { waitUntil: "domcontentloaded"});
137-
// adding extra timeout since domcontentloaded event is fired pretty quickly
138-
await new Promise(r => setTimeout(r, 1250));
139-
if (ctx.config.waitForTimeout) await page.waitForTimeout(ctx.config.waitForTimeout);
140-
navigated = true;
141-
ctx.log.debug(`Navigated to ${snapshot.url}`);
140+
try {
141+
// domcontentloaded event is more reliable than load event
142+
await page.goto(snapshot.url, { waitUntil: "domcontentloaded"});
143+
// adding extra timeout since domcontentloaded event is fired pretty quickly
144+
await new Promise(r => setTimeout(r, 1250));
145+
if (ctx.config.waitForTimeout) await page.waitForTimeout(ctx.config.waitForTimeout);
146+
navigated = true;
147+
ctx.log.debug(`Navigated to ${snapshot.url}`);
148+
} catch (error: any) {
149+
ctx.log.debug(`Navigation to discovery page failed; ${error}`)
150+
throw new Error(error.message)
151+
}
152+
142153
}
143-
if (fullPage) await page.evaluate(scrollToBottomAndBackToTop);
154+
if (ctx.config.enableJavaScript && fullPage) await page.evaluate(scrollToBottomAndBackToTop);
144155

145156
try {
146157
await page.waitForLoadState('networkidle', { timeout: 5000 });
@@ -181,9 +192,6 @@ export default async (snapshot: Snapshot, ctx: Context): Promise<Record<string,
181192
}
182193
}
183194

184-
await page.close();
185-
await context.close();
186-
187195
// add dom resources to cache
188196
if (snapshot.dom.resources.length) {
189197
for (let resource of snapshot.dom.resources) {

src/lib/server.ts

+23-4
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,36 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
2525

2626
// upload snpashot
2727
server.post('/snapshot', opts, async (request, reply) => {
28+
let replyCode: number;
29+
let replyBody: Record<string, any>;
30+
2831
try {
2932
let { snapshot, testType } = request.body;
3033
if (!validateSnapshot(snapshot)) throw new Error(validateSnapshot.errors[0].message);
3134
let { processedSnapshot, warnings } = await processSnapshot(snapshot, ctx);
3235
await ctx.client.uploadSnapshot(ctx.build.id, processedSnapshot, testType, ctx.log);
33-
34-
ctx.totalSnapshots++
35-
reply.code(200).send({data: { message: "success", warnings }});
36+
ctx.totalSnapshots++;
37+
replyCode = 200;
38+
replyBody = { data: { message: "success", warnings }};
3639
} catch (error: any) {
37-
return reply.code(500).send({ error: { message: error.message}});
40+
ctx.log.debug(`snapshot failed; ${error}`)
41+
replyCode = 500;
42+
replyBody = { error: { message: error.message }}
43+
}
44+
45+
// Close open browser contexts and pages
46+
if (ctx.browser) {
47+
for (let context of ctx.browser.contexts()) {
48+
for (let page of context.pages()) {
49+
await page.close();
50+
ctx.log.debug(`Closed browser page`);
51+
}
52+
await context.close();
53+
ctx.log.debug(`Closed browser context`);
54+
}
3855
}
56+
57+
return reply.code(replyCode).send(replyBody);
3958
});
4059

4160

0 commit comments

Comments
 (0)