Skip to content

Commit 59aff43

Browse files
authored
[minor] setting for screenshots (#84)
1 parent a1d3f8a commit 59aff43

File tree

15 files changed

+775
-18
lines changed

15 files changed

+775
-18
lines changed

.github/workflows/build.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ jobs:
9999
fi
100100
101101
echo "publishing alpha, for other commit message should contain [minor], [major] or [patch]/[fix]"
102-
npm run publish:alpha
103-
git push --tags
102+
npm run publish:alpha || publish:alpha:init || true
103+
git push --tags || true
104104
105105

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
## Change Log
2+
### 0.16.0
3+
- [minor] setting to attach screenshots to a steps by options to cy.screeshot command
24
### 0.15.3
35
- [patch] fix moving test attachments during execution for test
6+
### 0.15.2
7+
- [patch] minor packages updates
48
### 0.15.1
59
- [patch] fix of corner case with custom commands in #75
610

README.pack.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Example report is here - [Allure Report example](https://mmisty.github.io/cypres
3737
5. [Examples](#examples)
3838
5. [Allure TestOps](#allure-testops)
3939
5. [Troubleshooting](#troubleshooting)
40+
6. See also:
41+
- [adding screenshots](docs/screenshots.md)
4042
6. [Credits](#credits)
4143
6. [Change log](#change-log)
4244
6. [Support](#support)

docs/screenshots.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Screenshots
2+
3+
By default, adapter adds all screenshots to a test.
4+
5+
There is possibility to attach screenshots made by
6+
`cy.screenshot` command to a step.
7+
8+
To attach to a step use
9+
setting to screenshot command
10+
11+
```typescript
12+
cy.screenshot({ allureAttachToStep: true });
13+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { visitHtml } from '../../common/helper';
2+
3+
Cypress.env('allureSkipCommands', 'screenshot');
4+
describe('screenshot', () => {
5+
describe('screenshot - no name @screen', () => {
6+
beforeEach(() => {
7+
visitHtml();
8+
});
9+
10+
it('test 1 - should have 1 step screenshot', () => {
11+
cy.allure().startStep('step 1');
12+
cy.screenshot({ allureAttachToStep: true });
13+
cy.allure().endStep();
14+
});
15+
16+
it('test 1.1 - should have 1 step screenshot for element', () => {
17+
cy.allure().startStep('step 1');
18+
cy.get('div').first().screenshot({ allureAttachToStep: true });
19+
cy.allure().endStep();
20+
});
21+
22+
it('test 2 - should have 1 step screenshot and one test screenshot', () => {
23+
cy.allure().startStep('step 1');
24+
cy.screenshot({ allureAttachToStep: true });
25+
cy.screenshot();
26+
cy.allure().endStep();
27+
});
28+
29+
it('test 2.1 - should have 1 step screenshot and one test screenshot for element', () => {
30+
cy.allure().startStep('step 1');
31+
cy.get('div').first().screenshot({ allureAttachToStep: true });
32+
cy.get('div').eq(1).screenshot();
33+
cy.allure().endStep();
34+
});
35+
36+
it('test 3 - should have 1 test screenshot', () => {
37+
cy.allure().startStep('step 1');
38+
cy.screenshot();
39+
cy.allure().endStep();
40+
});
41+
42+
it('test 3.1 - should have 1 test screenshot for element', () => {
43+
cy.allure().startStep('step 1');
44+
cy.get('div').first().screenshot();
45+
cy.allure().endStep();
46+
});
47+
});
48+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { visitHtml } from '../../common/helper';
2+
3+
Cypress.env('allureSkipCommands', 'screenshot');
4+
describe('screenshot', () => {
5+
describe('screenshot with name @screen', () => {
6+
beforeEach(() => {
7+
visitHtml();
8+
});
9+
10+
it('test 1 - should have 1 step screenshot', () => {
11+
cy.allure().startStep('step 1');
12+
cy.screenshot('my-step', { allureAttachToStep: true });
13+
cy.allure().endStep();
14+
});
15+
16+
it('test 1.1 - should have 1 step screenshot for element', () => {
17+
cy.allure().startStep('step 1');
18+
cy.get('div').first().screenshot('my-element', { allureAttachToStep: true });
19+
cy.allure().endStep();
20+
});
21+
22+
it('test 2 - should have 1 step screenshot and one test screenshot', () => {
23+
cy.allure().startStep('step 1');
24+
cy.screenshot('my-step', { allureAttachToStep: true });
25+
cy.screenshot('my-test');
26+
cy.allure().endStep();
27+
});
28+
29+
it('test 2.1 - should have 1 step screenshot and one test screenshot for element', () => {
30+
cy.allure().startStep('step 1');
31+
cy.get('div').first().screenshot('my-step', { allureAttachToStep: true });
32+
cy.get('div').eq(1).screenshot('my-test');
33+
cy.allure().endStep();
34+
});
35+
36+
it('test 3 - should have 1 test screenshot', () => {
37+
cy.allure().startStep('step 1');
38+
cy.screenshot('my-test');
39+
cy.allure().endStep();
40+
});
41+
42+
it('test 3.1 - should have 1 test screenshot for element', () => {
43+
cy.allure().startStep('step 1');
44+
cy.get('div').first().screenshot('my-test');
45+
cy.allure().endStep();
46+
});
47+
});
48+
});

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"prepublish": "npm run lint",
5656
"prepublishOnly": "{ echo \"Run script 'npm run publish:pack', 'npm run publish:minor', etc from package.json\n\"; exit 1; }",
5757
"publishPack": "npm run pack && cd lib && npm publish --tag $TAG",
58-
"publish:alpha": "export TAG=alpha && export ver=$(semver $(npm show . version --tag alpha) --increment -i prerelease --preid 'alpha') && npm run publishPack && npm run postpublish",
58+
"publish:alpha": "export TAG=alpha && export ver=$(semver $(npm show . version --tag alpha || npm show . version) --increment -i prerelease --preid 'alpha') && npm run publishPack && npm run postpublish",
59+
"publish:alpha:init": "export TAG=alpha && export ver=$(semver $(npm show . version) --increment -i prerelease --preid 'alpha') && npm run publishPack && npm run postpublish",
5960
"publish:patch": "export TAG=latest && export ver=$(semver $(npm show . version) --increment -i patch) && npm run publishPack && npm run postpublish",
6061
"publish:minor": "export TAG=latest && export ver=$(semver $(npm show . version) --increment -i minor) && npm run publishPack && npm run postpublish",
6162
"publish:major": "export TAG=latest && export ver=$(semver $(npm show . version) --increment -i major) && npm run publishPack && npm run postpublish",

src/commands/index.ts

+17
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ export const registerCommands = () => {
8686
return cmd?.get('prev')?.attributes?.subject;
8787
};
8888

89+
// first arg may be JQuery
90+
Cypress.Commands.overwrite('screenshot', function (originalFn, ...args) {
91+
const setSettingAttachToStep = (res: boolean) => {
92+
(window as unknown as { allureAttachToStep: boolean }).allureAttachToStep = res;
93+
};
94+
95+
const getAllureAttachToStep = (obj: unknown) => {
96+
return obj && typeof obj === 'object' && (obj as any).allureAttachToStep;
97+
};
98+
99+
const toStep = args.some(t => getAllureAttachToStep(t));
100+
101+
setSettingAttachToStep(toStep);
102+
103+
return originalFn(...args);
104+
});
105+
89106
// not changing the subject
90107
// eslint-disable-next-line @typescript-eslint/no-explicit-any
91108
Cypress.Commands.add('doSyncCommand', function (syncFn: (subj: any) => any) {

src/cypress/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-namespace
22
declare namespace Cypress {
3+
interface ScreenshotOptions {
4+
/**
5+
* Add screenshot to a step when true, otherwise to a test
6+
*/
7+
allureAttachToStep: boolean;
8+
}
9+
310
export type Status = 'passed' | 'failed' | 'skipped' | 'broken' | 'unknown';
411
export type CommandT = {
512
state?: string;

src/plugins/allure-reporter-plugin.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ const copyFileToWatch = async (
109109
* Get all attachments for test to move them to watch folder
110110
* @param item test item
111111
*/
112-
const getAllAttachments = (item: ExecutableItem) => {
112+
const getAllAttachments = (item: ExecutableItem): Attachment[] => {
113113
const attachmentsResult: Attachment[] = [];
114114

115115
const inner = (steps: ExecutableItem[], accumulatedRes: Attachment[]): Attachment[] => {
@@ -866,12 +866,14 @@ export class AllureReporter {
866866
console.error(`${packageLog} Result file doesn't exist: ${file}`);
867867
}
868868
};
869-
waitResultWritten(this.allureResults, `${this.allureResults}/${uid}-result.json`);
869+
const testFile = `${this.allureResults}/${uid}-result.json`;
870+
871+
waitResultWritten(this.allureResults, testFile);
870872

871873
// move to watch
872874

873875
log('testEnded: will move result to watch folder');
874-
await copyFileToWatch({ test: `${this.allureResults}/${uid}-result.json`, attachments }, this.allureResultsWatch);
876+
await copyFileToWatch({ test: testFile, attachments }, this.allureResultsWatch);
875877
}
876878

877879
startStep(arg: AllureTaskArgs<'stepStarted'>) {

src/setup/cypress-events.ts

-6
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,6 @@ export const handleCyLogEvents = (
466466
Cypress.Allure.on('cmd:started', (command: CommandT) => {
467467
const { name, isLog, message: cmdMessage, args } = commandParams(command);
468468

469-
if (name === 'screenshot') {
470-
// add screenshot to report
471-
const screenName = command.attributes?.args[0] ?? 'anyName';
472-
emit({ task: 'screenshotOne', arg: { forStep: true, name: screenName } });
473-
}
474-
475469
if (!isLogCommand(isLog, name) || !allureLogCyCommands()) {
476470
return;
477471
}

src/setup/screenshots.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ export const registerScreenshotHandler = () => {
1515
const [{ path }] = args;
1616

1717
if (path) {
18-
// todo setting
19-
debug(`Attaching: ${path}`);
20-
Cypress.Allure.testFileAttachment(basename(path), path, getContentType(path));
18+
const win = window as unknown as { allureAttachToStep: boolean };
19+
20+
if (win.allureAttachToStep) {
21+
debug(`Attaching to step: ${path}`);
22+
Cypress.Allure.fileAttachment(basename(path), path, getContentType(path));
23+
} else {
24+
debug(`Attaching to test: ${path}`);
25+
Cypress.Allure.testFileAttachment(basename(path), path, getContentType(path));
26+
}
2127
} else {
2228
debug(`No path: ${JSON.stringify(args)}`);
2329
}

tests/cy-helper/utils.ts

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ export const fixResult = (results: AllureTest[]): AllureTest[] => {
3838
...s,
3939
start: date,
4040
stop: date + 11,
41+
attachments: s.attachments.map(t => ({
42+
...t,
43+
name: t.name.replace(/\d{5,}/g, 'number'),
44+
source: `source${path.extname(t.source)}`,
45+
})),
4146
steps: replaceSteps(s.steps),
4247
}));
4348
};

0 commit comments

Comments
 (0)