Skip to content

Commit

Permalink
add comments and fix dir check
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshshanmugam committed May 27, 2021
1 parent 69affc1 commit c6e5089
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
3 changes: 0 additions & 3 deletions __tests__/reporters/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ describe('json reporter', () => {
journey: j1,
status: 'succeeded',
step: step('s1', () => {}),
screenshot: 'dummy',
url: 'dummy',
start: 0,
end: 10,
Expand Down Expand Up @@ -157,7 +156,6 @@ describe('json reporter', () => {
journey: j1,
status: 'failed',
step: step('s2', () => {}),
screenshot: 'dummy2',
url: 'dummy2',
start: 11,
end: 20,
Expand Down Expand Up @@ -201,7 +199,6 @@ describe('json reporter', () => {
journey: j1,
status: 'failed',
step: step('s2', () => {}),
screenshot: data,
start: 11,
end: 20,
});
Expand Down
5 changes: 5 additions & 0 deletions src/core/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ export default class Runner {
const buffer = await driver.page.screenshot({
type: 'png',
});
/**
* Write the screenshot image buffer with additional details (step
* information) which could be extracted at the end of
* each journey without impacting the step timing information
*/
const fileName = now().toString() + '.json';
writeFileSync(
join(this.screenshotPath, fileName),
Expand Down
31 changes: 22 additions & 9 deletions src/reporters/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
*/

import { readFileSync } from 'fs';
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';
import sharp from 'sharp';
import { createHash } from 'crypto';
Expand Down Expand Up @@ -287,6 +287,12 @@ async function processScreenshot(screenshot: Buffer) {
blob: buf.toString('base64'),
id: hash,
});
/**
* We dont write the width, height of individual blocks on the
* reference as we use similar sized blocks for each extraction,
* we would need to send the width and height here if we decide to
* go with dynamic block extraction.
*/
reference.blocks.push({
hash,
top,
Expand All @@ -298,17 +304,24 @@ async function processScreenshot(screenshot: Buffer) {
return { blocks, reference, blob_mime: 'image/webp' };
}

/**
* Get all the screenshots from the cached screenshot location
* at the end of each journey and construct equally sized blocks out
* of the individual screenshot image.
*/
async function gatherScreenshots(screenshotsPath: string) {
const screenshots: Array<ScreenshotOutput> = [];
await totalist(screenshotsPath, async (_, absPath) => {
const content = readFileSync(absPath, 'utf8');
const { step, data } = JSON.parse(content);
const result = await processScreenshot(Buffer.from(data, 'base64'));
screenshots.push({
step,
...result,
if (existsSync(CACHE_PATH)) {
await totalist(screenshotsPath, async (_, absPath) => {
const content = readFileSync(absPath, 'utf8');
const { step, data } = JSON.parse(content);
const result = await processScreenshot(Buffer.from(data, 'base64'));
screenshots.push({
step,
...result,
});
});
});
}
return screenshots;
}

Expand Down

0 comments on commit c6e5089

Please sign in to comment.