Skip to content

Commit b6fdb00

Browse files
committed
Add an option to disabled attaching screenshots to reports
This fixes #1306 [1]. [1] #1306
1 parent 6db26a7 commit b6fdb00

File tree

10 files changed

+141
-23
lines changed

10 files changed

+141
-23
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ All notable changes to this project will be documented in this file.
66

77
- Add TypeScript support for custom world in hooks, fixes [#1304](https://github.com/badeball/cypress-cucumber-preprocessor/issues/1304).
88

9+
- Add an [option](docs/json-report.md#screenshots) to disable attaching screenshots to reports, fixes [#1306](https://github.com/badeball/cypress-cucumber-preprocessor/issues/1306).
10+
11+
- This option defaults to `true`, retaining existing behavior for other users.
12+
913
## v22.1.0
1014

1115
- Gracefully handle sourcemaps when running component tests with webpack and chunks enabled (default behavior), fixes [#1296](https://github.com/badeball/cypress-cucumber-preprocessor/issues/1296).

docs/configuration.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,23 @@ $ CYPRESS_filterSpecs=true cypress run
7171

7272
Every configuration option has a similar key which can be use to override it, shown in the table below.
7373

74-
| JSON path | Environment key | Example(s) |
75-
|------------------------|------------------------|------------------------------------------|
76-
| `stepDefinitions` | `stepDefinitions` | `[filepath].{js,ts}` |
77-
| `messages.enabled` | `messagesEnabled` | `true`, `false` |
78-
| `messages.output` | `messagesOutput` | `cucumber-messages.ndjson` |
79-
| `json.enabled` | `jsonEnabled` | `true`, `false` |
80-
| `json.output` | `jsonOutput` | `cucumber-report.json` |
81-
| `html.enabled` | `htmlEnabled` | `true`, `false` |
82-
| `html.output` | `htmlOutput` | `cucumber-report.html` |
83-
| `usage.enabled` | `usageEnabled` | `true`, `false` |
84-
| `usage.output` | `usageOutput` | `stdout` |
85-
| `pretty.enabled` | `prettyEnabled` | `true`, `false` |
86-
| `filterSpecsMixedMode` | `filterSpecsMixedMode` | `hide`, `show`, `empty-set` |
87-
| `filterSpecs` | `filterSpecs` | `true`, `false` |
88-
| `omitFiltered` | `omitFiltered` | `true`, `false` |
89-
| `dryRun` | `dryRun` | `true`, `false` |
74+
| JSON path | Environment key | Example(s) |
75+
|------------------------------|-----------------------------|------------------------------------------|
76+
| `stepDefinitions` | `stepDefinitions` | `[filepath].{js,ts}` |
77+
| `messages.enabled` | `messagesEnabled` | `true`, `false` |
78+
| `messages.output` | `messagesOutput` | `cucumber-messages.ndjson` |
79+
| `json.enabled` | `jsonEnabled` | `true`, `false` |
80+
| `json.output` | `jsonOutput` | `cucumber-report.json` |
81+
| `html.enabled` | `htmlEnabled` | `true`, `false` |
82+
| `html.output` | `htmlOutput` | `cucumber-report.html` |
83+
| `usage.enabled` | `usageEnabled` | `true`, `false` |
84+
| `usage.output` | `usageOutput` | `stdout` |
85+
| `pretty.enabled` | `prettyEnabled` | `true`, `false` |
86+
| `filterSpecsMixedMode` | `filterSpecsMixedMode` | `hide`, `show`, `empty-set` |
87+
| `filterSpecs` | `filterSpecs` | `true`, `false` |
88+
| `omitFiltered` | `omitFiltered` | `true`, `false` |
89+
| `dryRun` | `dryRun` | `true`, `false` |
90+
| `attachments.addScreenshots` | `attachmentsAddScreenshots` | `true`, `false` |
9091

9192
## Test configuration
9293

docs/html-report.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The report is outputted to `cucumber-report.html` in the project directory, but
1616

1717
## Screenshots
1818

19-
Screenshots are automatically added to HTML reports, including that of failed tests (unless you have disabled `screenshotOnRunFailure`).
19+
Screenshots are automatically added to HTML reports, including that of failed tests (unless you have disabled `screenshotOnRunFailure`). This can be turned off using the `attachments.addScreenshots` property, which defaults to `true`.
2020

2121
## Attachments
2222

docs/json-report.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The report is outputted to `cucumber-report.json` in the project directory, but
1616

1717
## Screenshots
1818

19-
Screenshots are automatically added to JSON reports, including that of failed tests (unless you have disabled `screenshotOnRunFailure`).
19+
Screenshots are automatically added to JSON reports, including that of failed tests (unless you have disabled `screenshotOnRunFailure`). This can be turned off using the `attachments.addScreenshots` property, which defaults to `true`.
2020

2121
## Attachments (browser environment)
2222

docs/messages-report.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The report is outputted to `cucumber-messages.ndjson` in the project directory,
1818

1919
## Screenshots
2020

21-
Screenshots are automatically added to messages reports, including that of failed tests (unless you have disabled `screenshotOnRunFailure`).
21+
Screenshots are automatically added to messages reports, including that of failed tests (unless you have disabled `screenshotOnRunFailure`). This can be turned off using the `attachments.addScreenshots` property, which defaults to `true`.
2222

2323
## Attachments
2424

features/reporters/json.feature

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ Feature: JSON formatter
337337
Then it fails
338338
And there should be a JSON output similar to "fixtures/failing-step.json"
339339

340-
Rule: it should contain screenshots captured during a test
340+
Rule: it should by default contain screenshots captured during a test
341341
Scenario: explicit screenshot
342342
Given a file named "cypress/e2e/a.feature" with:
343343
"""
@@ -387,6 +387,24 @@ Feature: JSON formatter
387387
Then it fails
388388
And the JSON report should contain an image attachment for what appears to be a screenshot
389389

390+
Scenario: with screenshots disabled
391+
Given a file named "cypress/e2e/a.feature" with:
392+
"""
393+
Feature: a feature
394+
Scenario: a scenario
395+
Given a step
396+
"""
397+
And a file named "cypress/support/step_definitions/steps.js" with:
398+
"""
399+
const { Given } = require("@badeball/cypress-cucumber-preprocessor");
400+
Given("a step", function() {
401+
cy.screenshot();
402+
});
403+
"""
404+
When I run cypress with "-e attachmentsAddScreenshots=false"
405+
Then it passes
406+
And there should be a JSON output similar to "fixtures/passed-example.json"
407+
390408
Rule: it should include results of skipped (not omitted) tests
391409
Scenario: first scenario skipped
392410
Given a file named "cypress/e2e/a.feature" with:

features/reporters/messages.feature

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ Feature: messages report
338338
Then it fails
339339
And there should be a messages similar to "fixtures/failing-step.ndjson"
340340

341-
Rule: it should contain screenshots captured during a test
341+
Rule: it should by default contain screenshots captured during a test
342342
Scenario: explicit screenshot
343343
Given a file named "cypress/e2e/a.feature" with:
344344
"""
@@ -388,6 +388,24 @@ Feature: messages report
388388
Then it fails
389389
And the messages report should contain an image attachment for what appears to be a screenshot
390390

391+
Scenario: with screenshots disabled
392+
Given a file named "cypress/e2e/a.feature" with:
393+
"""
394+
Feature: a feature
395+
Scenario: a scenario
396+
Given a step
397+
"""
398+
And a file named "cypress/support/step_definitions/steps.js" with:
399+
"""
400+
const { Given } = require("@badeball/cypress-cucumber-preprocessor");
401+
Given("a step", function() {
402+
cy.screenshot();
403+
});
404+
"""
405+
When I run cypress with "-e attachmentsAddScreenshots=false"
406+
Then it passes
407+
And there should be a messages similar to "fixtures/passed-example.ndjson"
408+
391409
Rule: it should include results of skipped (not omitted) tests
392410
Scenario: first scenario skipped
393411
Given a file named "cypress/e2e/a.feature" with:

lib/plugin-event-handlers.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,10 @@ export async function afterScreenshotHandler(
662662

663663
const preprocessor = await resolve(config, config.env, "/");
664664

665-
if (!preprocessor.isTrackingState) {
665+
if (
666+
!preprocessor.isTrackingState ||
667+
!preprocessor.attachments.addScreenshots
668+
) {
666669
return details;
667670
}
668671

lib/preprocessor-configuration.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ function basicBooleanExample(options: {
178178
getValueFn,
179179
environment: { [environmentKey]: "" },
180180
configuration: {},
181-
expectedValue: false,
181+
expectedValue: options.default,
182182
}));
183183
});
184184
}
@@ -958,6 +958,27 @@ describe("resolve()", () => {
958958
}));
959959
});
960960
});
961+
962+
describe("attachments", () => {
963+
describe("addScreenshots", () => {
964+
const getValueFn = (
965+
configuration: IPreprocessorConfiguration,
966+
): boolean => configuration.attachments.addScreenshots;
967+
968+
const setValueFn = (
969+
configuration: IBaseUserConfiguration,
970+
value: boolean,
971+
) => (configuration.attachments = { addScreenshots: value });
972+
973+
basicBooleanExample({
974+
testingType,
975+
default: true,
976+
environmentKey: "attachmentsAddScreenshots",
977+
getValueFn,
978+
setValueFn,
979+
});
980+
});
981+
});
961982
});
962983
}
963984
});

lib/preprocessor-configuration.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,27 @@ function validateUserConfigurationEntry(
234234
}
235235
return { [key]: value };
236236
}
237+
case "attachments": {
238+
if (typeof value !== "object" || value == null) {
239+
throw new Error(
240+
`Expected an object (json), but got ${util.inspect(value)}`,
241+
);
242+
}
243+
if (
244+
!hasOwnProperty(value, "addScreenshots") ||
245+
typeof value.addScreenshots !== "boolean"
246+
) {
247+
throw new Error(
248+
`Expected a boolean (attachments.addScreenshots), but got ${util.inspect(
249+
value.addScreenshots,
250+
)}`,
251+
);
252+
}
253+
const attachmentsConfig = {
254+
addScreenshots: value.addScreenshots,
255+
};
256+
return { [key]: attachmentsConfig };
257+
}
237258
case "e2e":
238259
return { [key]: validateUserConfiguration(value) };
239260
case "component":
@@ -435,6 +456,22 @@ function validateEnvironmentOverrides(
435456
}
436457
}
437458

459+
if (hasOwnProperty(environment, "attachmentsAddScreenshots")) {
460+
const { attachmentsAddScreenshots } = environment;
461+
462+
if (isBoolean(attachmentsAddScreenshots)) {
463+
overrides.attachmentsAddScreenshots = attachmentsAddScreenshots;
464+
} else if (isString(attachmentsAddScreenshots)) {
465+
overrides.attachmentsAddScreenshots = stringToMaybeBoolean(
466+
attachmentsAddScreenshots,
467+
);
468+
} else {
469+
throw new Error(
470+
`Expected a boolean (attachmentsAddScreenshots), but got ${util.inspect(attachmentsAddScreenshots)}`,
471+
);
472+
}
473+
}
474+
438475
return overrides;
439476
}
440477

@@ -480,6 +517,7 @@ interface IEnvironmentOverrides {
480517
filterSpecs?: boolean;
481518
omitFiltered?: boolean;
482519
dryRun?: boolean;
520+
attachmentsAddScreenshots?: boolean;
483521
}
484522

485523
export interface IBaseUserConfiguration {
@@ -507,6 +545,9 @@ export interface IBaseUserConfiguration {
507545
filterSpecs?: boolean;
508546
omitFiltered?: boolean;
509547
dryRun?: boolean;
548+
attachments?: {
549+
addScreenshots: boolean;
550+
};
510551
}
511552

512553
export interface IUserConfiguration extends IBaseUserConfiguration {
@@ -541,6 +582,9 @@ export interface IPreprocessorConfiguration {
541582
readonly implicitIntegrationFolder: string;
542583
readonly isTrackingState: boolean;
543584
readonly dryRun: boolean;
585+
readonly attachments: {
586+
addScreenshots: boolean;
587+
};
544588
}
545589

546590
const DEFAULT_STEP_DEFINITIONS = [
@@ -674,6 +718,14 @@ export function combineIntoConfiguration(
674718
usage.enabled ||
675719
usingPrettyReporter);
676720

721+
const attachments: IPreprocessorConfiguration["attachments"] = {
722+
addScreenshots:
723+
overrides.attachmentsAddScreenshots ??
724+
specific?.attachments?.addScreenshots ??
725+
unspecific.attachments?.addScreenshots ??
726+
true,
727+
};
728+
677729
return {
678730
stepDefinitions,
679731
messages,
@@ -687,6 +739,7 @@ export function combineIntoConfiguration(
687739
implicitIntegrationFolder,
688740
isTrackingState,
689741
dryRun,
742+
attachments,
690743
};
691744
}
692745

0 commit comments

Comments
 (0)