Skip to content

Commit

Permalink
add function to split har per page (#12)
Browse files Browse the repository at this point in the history
* add function to split har per page

* update readme
  • Loading branch information
resdenia authored Feb 26, 2023
1 parent 3326c48 commit 0908441
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
8 changes: 0 additions & 8 deletions client/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
export const DEFAULT_CODE = `const playwright = require('playwright-aws-lambda');
const readSendData = require('./rsData');
const handler = async () => {
let context = null;
let browser = null;
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();
//////////////////////////////////
Expand All @@ -30,7 +24,5 @@ export const DEFAULT_CODE = `const playwright = require('playwright-aws-lambda')
await browser.close();
}
}
readSendData();
return true;
};`
25 changes: 14 additions & 11 deletions helper/index-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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: `
Expand Down
5 changes: 3 additions & 2 deletions service/lambdaFunction/attachTrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
);
});
}

Expand Down
3 changes: 2 additions & 1 deletion service/lambdaFunction/convertHarToJSON.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const match = require('@menadevs/objectron');

const entryPattern = {
pageref: /(?<pageRef>.*)/,
// pageref: /(?<pageRef>.*)/,
startedDateTime: /(?<startedDateTime>.*)/,
request: {
method: /(?<requestMethod>GET|POST)/,
Expand Down Expand Up @@ -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);
});

Expand Down
45 changes: 45 additions & 0 deletions service/lambdaFunction/getHarFile.js
Original file line number Diff line number Diff line change
@@ -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);
},
};
15 changes: 15 additions & 0 deletions service/lambdaFunction/handlerHar.js
Original file line number Diff line number Diff line change
@@ -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;
}
};
14 changes: 6 additions & 8 deletions service/lambdaFunction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0908441

Please sign in to comment.