From 0908441f4eafb4fa53667ff4e578cb125d3d3c55 Mon Sep 17 00:00:00 2001 From: Anton Kolomiiets Date: Sun, 26 Feb 2023 18:11:10 +0200 Subject: [PATCH] add function to split har per page (#12) * add function to split har per page * update readme --- README.md | 2 +- client/src/utils/constants.ts | 8 ---- helper/index-config.js | 25 ++++++------ service/lambdaFunction/attachTrace.js | 5 ++- service/lambdaFunction/convertHarToJSON.js | 3 +- service/lambdaFunction/getHarFile.js | 45 ++++++++++++++++++++++ service/lambdaFunction/handlerHar.js | 15 ++++++++ service/lambdaFunction/index.js | 14 +++---- 8 files changed, 86 insertions(+), 31 deletions(-) create mode 100644 service/lambdaFunction/getHarFile.js create mode 100644 service/lambdaFunction/handlerHar.js diff --git a/README.md b/README.md index 3581cde..d33a17f 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ and then open [Logz.io](https://app.logz.io/#/dashboard/metrics). 1.1.9: -- Add support to run a tests on different mobile devices +- Split har reports between pages what will be open during the test 1.1.8: diff --git a/client/src/utils/constants.ts b/client/src/utils/constants.ts index a4f5a68..038f8d6 100644 --- a/client/src/utils/constants.ts +++ b/client/src/utils/constants.ts @@ -1,5 +1,4 @@ export const DEFAULT_CODE = `const playwright = require('playwright-aws-lambda'); - const readSendData = require('./rsData'); const handler = async () => { let context = null; @@ -7,11 +6,6 @@ export const DEFAULT_CODE = `const playwright = require('playwright-aws-lambda') try { browser = await playwright.launchChromium(false); context = await browser.newContext({ - recordHar: { - path: './capture-hars/page.har', - mode: 'full', - content: 'omit', - }, }); const page = await context.newPage(); ////////////////////////////////// @@ -30,7 +24,5 @@ export const DEFAULT_CODE = `const playwright = require('playwright-aws-lambda') await browser.close(); } } - - readSendData(); return true; };` diff --git a/helper/index-config.js b/helper/index-config.js index 7b59910..25bfc8b 100644 --- a/helper/index-config.js +++ b/helper/index-config.js @@ -4,6 +4,7 @@ module.exports = { const path = require('path'); const readSendData = require('./rsData'); const cfnResponse = require('cfn-response-async'); + const pageHandler = require('./handlerHar'); const firstRun = async (event, context) => { await regularRun(); @@ -28,23 +29,24 @@ module.exports = { try { browser = await playwright.launchChromium(false); context = await browser.newContext({ - recordHar: { - path: path.join(__dirname, '..', '..', 'tmp', 'page.har'), - mode: 'full', - content: 'omit', - }, ...mobileDevice }); await context.tracing.start({ screenshots: false, snapshots: false }); page = await context.newPage(); + let count = 0; + page.on('load', async (data) => { + count++; + await pageHandler(data, count); + }); `, startFile: `const playwright = require('playwright-aws-lambda'); const path = require('path'); const readSendData = require('./rsData'); const cfnResponse = require('cfn-response-async'); - + const pageHandler = require('./handlerHar'); + const firstRun = async (event, context) => { await regularRun(); @@ -61,18 +63,19 @@ module.exports = { let context = null; let err = null; let page = null; + let browser; try { browser = await playwright.launchChromium(false); context = await browser.newContext({ - recordHar: { - path: path.join(__dirname, '..', '..', 'tmp', 'page.har'), - mode: 'full', - content: 'omit', - }, }); await context.tracing.start({ screenshots: false, snapshots: false }); page = await context.newPage(); + let count = 0; + page.on('load', async (data) => { + count++; + await pageHandler(data, count); + }); `, endFile: ` diff --git a/service/lambdaFunction/attachTrace.js b/service/lambdaFunction/attachTrace.js index d44b0d2..6a82029 100644 --- a/service/lambdaFunction/attachTrace.js +++ b/service/lambdaFunction/attachTrace.js @@ -35,8 +35,9 @@ const parsingLogSynthetic = (trace, name, sessionId, startTime) => { if (trace.metadata.params) { Object.keys(trace.metadata.params).forEach((key, idx) => { parseObject[`actionParameter${idx}_key`] = key; - parseObject[`actionParameter${idx}_value`] = - trace.metadata.params[key].toString(); + parseObject[`actionParameter${idx}_value`] = JSON.stringify( + trace.metadata.params[key], + ); }); } diff --git a/service/lambdaFunction/convertHarToJSON.js b/service/lambdaFunction/convertHarToJSON.js index f60c3d4..e6909df 100644 --- a/service/lambdaFunction/convertHarToJSON.js +++ b/service/lambdaFunction/convertHarToJSON.js @@ -1,7 +1,7 @@ const match = require('@menadevs/objectron'); const entryPattern = { - pageref: /(?.*)/, + // pageref: /(?.*)/, startedDateTime: /(?.*)/, request: { method: /(?GET|POST)/, @@ -55,6 +55,7 @@ const convertHarToJSON = (harFile) => { line.forEach((box, index) => { obj[headers[index]] = box; }); + obj.pageref = harFile.log.pages[0].title; result.push(obj); }); diff --git a/service/lambdaFunction/getHarFile.js b/service/lambdaFunction/getHarFile.js new file mode 100644 index 0000000..8b70ce2 --- /dev/null +++ b/service/lambdaFunction/getHarFile.js @@ -0,0 +1,45 @@ +const playwright = require('playwright-aws-lambda'); +const { harFromMessages } = require('chrome-har'); + +module.exports = { + getHarFile: async function (pageUrl) { + // list of events for converting to HAR + const events = []; + + // event types to observe + const observe = [ + 'Page.loadEventFired', + 'Page.domContentEventFired', + 'Page.frameStartedLoading', + 'Page.frameAttached', + 'Network.requestWillBeSent', + 'Network.requestServedFromCache', + 'Network.dataReceived', + 'Network.responseReceived', + 'Network.resourceChangedPriority', + 'Network.loadingFinished', + 'Network.loadingFailed', + ]; + // Create a URL object for use later + const url = new URL(pageUrl); + + const browser = await playwright.launchChromium(false); + const page = await browser.newPage(); + + // register events listeners + const client = await page.context().newCDPSession(page); + await client.send('Page.enable'); + await client.send('Network.enable'); + observe.forEach((method) => { + client.on(method, (params) => { + events.push({ method, params }); + }); + }); + + await page.goto(url.href); + await browser.close(); + + // convert events to HAR file + return harFromMessages(events); + }, +}; diff --git a/service/lambdaFunction/handlerHar.js b/service/lambdaFunction/handlerHar.js new file mode 100644 index 0000000..cfee765 --- /dev/null +++ b/service/lambdaFunction/handlerHar.js @@ -0,0 +1,15 @@ +const { getHarFile } = require('./getHarFile'); +const { writeFileSync } = require('fs'); +const path = require('path'); + +module.exports = pageHandler = async (page, count) => { + const result = await getHarFile(page.url()); + if (count) { + writeFileSync( + path.join(__dirname, '..', '..', 'tmp', `${count}.har`), + JSON.stringify(result), + ); + } else { + return harObject; + } +}; diff --git a/service/lambdaFunction/index.js b/service/lambdaFunction/index.js index 2c42e72..339b33f 100644 --- a/service/lambdaFunction/index.js +++ b/service/lambdaFunction/index.js @@ -23,18 +23,16 @@ const regularRun = async () => { let browser; try { browser = await playwright.launchChromium(false); - context = await browser.newContext({ - recordHar: { - path: path.join(__dirname, '..', '..', 'tmp', 'page.har'), - mode: 'full', - content: 'omit', - }, - }); + context = await browser.newContext({}); await context.tracing.start({ screenshots: false, snapshots: false }); page = await context.newPage(); + let count = 0; + page.on('load', async (data) => { + count++; + await pageHandler(data, count); + }); } catch (error) { - console.log(error); err = error.message; } finally { if (browser) {