Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for screenshot toggle #311

Merged
merged 4 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/common_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ export type PluginOutput = {
metrics?: PerfMetrics;
};

export type ScreenshotOptions = 'on' | 'off' | 'only-on-failure';

export type CliArgs = {
capability?: Array<string>;
config?: string;
environment?: string;
outfd?: number;
headless?: boolean;
screenshots?: boolean;
screenshots?: ScreenshotOptions;
ssblocks?: boolean;
metrics?: boolean;
filmstrips?: boolean;
Expand Down
36 changes: 20 additions & 16 deletions src/core/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ interface Events {
JourneyResult &
PluginOutput & {
journey: Journey;
ssblocks?: boolean;
options: RunOptions;
};
'journey:end:reported': unknown;
'step:start': { journey: Journey; step: Step };
Expand Down Expand Up @@ -207,25 +207,29 @@ export default class Runner extends EventEmitter {
data.error = error;
} finally {
data.url ??= driver.page.url();
if (screenshots) {
if (screenshots !== 'off') {
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
await driver.page.waitForLoadState('load');
const buffer = await driver.page.screenshot({
type: 'jpeg',
quality: 80,
});
const buffer = await driver.page
.screenshot({
type: 'jpeg',
quality: 80,
})
.catch(() => {});
/**
* 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),
JSON.stringify({
step,
data: buffer.toString('base64'),
})
);
if (buffer) {
const fileName = now().toString() + '.json';
writeFileSync(
join(this.screenshotPath, fileName),
JSON.stringify({
step,
data: buffer.toString('base64'),
})
);
}
}
}
log(`Runner: end step (${step.name})`);
Expand Down Expand Up @@ -292,15 +296,15 @@ export default class Runner extends EventEmitter {
params,
start,
end: monotonicTimeInSeconds(),
ssblocks: options.ssblocks,
options,
...pluginOutput,
browserconsole: status == 'failed' ? pluginOutput.browserconsole : [],
});
/**
* Wait for the all the reported events to be consumed aschronously
* by reporter.
*/
if (options.reporter == 'json') {
if (options.reporter === 'json') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird that the linter missed this

await once(this, 'journey:end:reported');
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/parse_args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ program
'--capability <features...>',
'Enable capabilities through feature flags'
)
.option('--screenshots', 'take screenshot for each step')
.addOption(
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
new Option('--screenshots [flag]', 'take screenshots at end of each step')
.choices(['on', 'off', 'only-on-failure'])
.default('on')
)
.option('--screenshots [flag]', 'take screenshot for each step')
.option('--network', 'capture network information for all journeys')
.option(
'--dry-run',
Expand Down Expand Up @@ -98,7 +103,7 @@ const options = command.opts() as CliArgs;
*/
if (options.richEvents) {
options.reporter = options.reporter ?? 'json';
options.screenshots = true;
options.screenshots = 'on';
options.network = true;
}

Expand Down
70 changes: 38 additions & 32 deletions src/reporters/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,42 +415,48 @@ export default class JSONReporter extends BaseReporter {
metrics,
status,
error,
ssblocks,
options,
}) => {
await gatherScreenshots(
join(CACHE_PATH, 'screenshots'),
async (step, data) => {
if (ssblocks) {
const { blob_mime, blocks, reference } =
await getScreenshotBlocks(Buffer.from(data, 'base64'));
for (let i = 0; i < blocks.length; i++) {
const block = blocks[i];
const { ssblocks, screenshots } = options;
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
const writeScreenshots =
screenshots === 'on' ||
(screenshots === 'only-on-failure' && status === 'failed');
if (writeScreenshots) {
await gatherScreenshots(
join(CACHE_PATH, 'screenshots'),
async (step, data) => {
if (ssblocks) {
const { blob_mime, blocks, reference } =
await getScreenshotBlocks(Buffer.from(data, 'base64'));
for (let i = 0; i < blocks.length; i++) {
const block = blocks[i];
this.writeJSON({
type: 'screenshot/block',
_id: block.id,
blob: block.blob,
blob_mime,
});
}
this.writeJSON({
type: 'screenshot/block',
_id: block.id,
blob: block.blob,
blob_mime,
type: 'step/screenshot_ref',
journey,
step,
root_fields: {
screenshot_ref: reference,
},
});
} else {
this.writeJSON({
type: 'step/screenshot',
journey,
step,
blob: data,
blob_mime: 'image/jpeg',
});
}
this.writeJSON({
type: 'step/screenshot_ref',
journey,
step,
root_fields: {
screenshot_ref: reference,
},
});
} else {
this.writeJSON({
type: 'step/screenshot',
journey,
step,
blob: data,
blob_mime: 'image/jpeg',
});
}
}
);
);
}

if (networkinfo) {
networkinfo.forEach(ni => {
Expand Down Expand Up @@ -504,7 +510,7 @@ export default class JSONReporter extends BaseReporter {
status,
},
});
this.runner.emit('journey:end:reported', {});
process.nextTick(() => this.runner.emit('journey:end:reported', {}));
}
);
}
Expand Down