Skip to content

Commit

Permalink
[journeys] Change page screeshots on steps (elastic#188332)
Browse files Browse the repository at this point in the history
## Summary

We have seen some random browser crashes during screenshot taken on CI
and hopefully these changes will help to minimize it:

- no screenshots on step success by default: we don't use the artifact
at the moment, so we don't need screenshots for all steps
- catch errors on screenshot take and log error, but don't throw error;
it will be thrown for step failure explicitly
  • Loading branch information
dmlemeshko authored Jul 17, 2024
1 parent 4e24f15 commit e1933c5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
9 changes: 9 additions & 0 deletions packages/kbn-journeys/journey/journey_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ export interface JourneyConfigOptions<CtxExt extends { '@timestamp'?: number | u
* },
*/
synthtrace?: JourneySynthtrace<CtxExt>;

/**
* Take browser page screenshot on every step complete
*/
takeScreenshotOnSuccess?: boolean;
}

export class JourneyConfig<CtxExt extends object> {
Expand Down Expand Up @@ -219,4 +224,8 @@ export class JourneyConfig<CtxExt extends object> {
getSynthtraceConfig() {
return this.#opts.synthtrace;
}

takeScreenshotOnSuccess() {
return !!this.#opts.takeScreenshotOnSuccess;
}
}
42 changes: 30 additions & 12 deletions packages/kbn-journeys/journey/journey_ftr_harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,37 @@ export class JourneyFtrHarness {
]);
}

private async takeScreenshots(page: Page) {
let screenshot;
let fs;
// screenshots taking might crash the browser
try {
screenshot = await page.screenshot({ animations: 'disabled' });
fs = await page.screenshot({ animations: 'disabled', fullPage: true });
} catch (e) {
if (!screenshot) {
this.log.error(`Failed to take screenshot of the visible viewport: ${e.message}`);
} else if (screenshot && !fs) {
this.log.error(`Failed to take screenshot of the full scrollable page: ${e.message}`);
} else {
this.log.error(`Unknown error on taking screenshots`);
}
}

return { screenshot, fs };
}

private async onStepSuccess(step: AnyStep) {
if (!this.page) {
return;
}

const [screenshot, fs] = await Promise.all([
this.page.screenshot(),
this.page.screenshot({ fullPage: true }),
]);

await this.screenshots.addSuccess(step, screenshot, fs);
if (this.journeyConfig.takeScreenshotOnSuccess()) {
const { screenshot, fs } = await this.takeScreenshots(this.page);
if (screenshot && fs) {
await this.screenshots.addSuccess(step, screenshot, fs);
}
}
}

private async onStepError(step: AnyStep, err: Error) {
Expand All @@ -325,12 +345,10 @@ export class JourneyFtrHarness {
}

if (this.page) {
const [screenshot, fs] = await Promise.all([
this.page.screenshot(),
this.page.screenshot({ fullPage: true }),
]);

await this.screenshots.addError(step, screenshot, fs);
const { screenshot, fs } = await this.takeScreenshots(this.page);
if (screenshot && fs) {
await this.screenshots.addError(step, screenshot, fs);
}
}
}

Expand Down

0 comments on commit e1933c5

Please sign in to comment.