Skip to content

Commit 7a5e3a8

Browse files
authored
feat: add matchAgainstPath option (#146)
allow to compare screenshots against custom reference images add docs add example test case closes #88 BREAKING CHANGE: matchImage returns object containing comparison details from now on (previously was returning subject element from a chain)
1 parent 9838a40 commit 7a5e3a8

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ cy.matchImage({
178178
// title used for naming the image file
179179
// default: Cypress.currentTest.titlePath (your test title)
180180
title: `${Cypress.currentTest.titlePath.join(' ')} (${Cypress.browser.displayName})`
181+
// pass a path to custom image that should be used for comparison
182+
// instead of checking against the image from previous run
183+
// default: undefined
184+
matchAgainstPath: '/path/to/reference-image.png'
181185
})
182186
```
183187

example/cypress/e2e/spec.cy.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ describe('My First Test', () => {
33
cy.visit('/')
44
cy.contains('h1', 'Welcome to Your Vue.js App')
55
cy.matchImage()
6+
.then(({ imgNewPath }) => {
7+
// match against image from custom path
8+
cy.matchImage({ matchAgainstPath: imgNewPath });
9+
})
610
})
711
})

src/commands.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ declare global {
1313
imagesDir?: string;
1414
maxDiffThreshold?: number;
1515
title?: string;
16+
matchAgainstPath?: string;
17+
// IDEA: to be implemented if support for files NOT from filesystem needed
18+
// matchAgainst?: string | Buffer;
19+
};
20+
21+
type MatchImageReturn = {
22+
diffValue: number | undefined;
23+
imgNewPath: string;
24+
imgPath: string;
25+
imgDiffPath: string;
26+
imgNewBase64: string | undefined;
27+
imgBase64: string | undefined;
28+
imgDiffBase64: string | undefined;
29+
imgNew: InstanceType<Cypress['Buffer']> | undefined;
30+
img: InstanceType<Cypress['Buffer']> | undefined;
31+
imgDiff: InstanceType<Cypress['Buffer']> | undefined;
1632
};
1733

1834
interface Chainable<Subject> {
@@ -21,7 +37,7 @@ declare global {
2137
* @memberof Cypress.Chainable
2238
* @example cy.get('.my-element').matchImage();
2339
*/
24-
matchImage(options?: Cypress.MatchImageOptions): Chainable<Subject>;
40+
matchImage(options?: Cypress.MatchImageOptions): Chainable<MatchImageReturn>;
2541
}
2642
}
2743
}
@@ -69,6 +85,7 @@ export const getConfig = (options: Cypress.MatchImageOptions) => ({
6985
| Partial<Cypress.ScreenshotDefaultsOptions>
7086
| undefined) ||
7187
{},
88+
matchAgainstPath: options.matchAgainstPath || undefined,
7289
});
7390

7491
Cypress.Commands.add(
@@ -88,6 +105,7 @@ Cypress.Commands.add(
88105
maxDiffThreshold,
89106
diffConfig,
90107
screenshotConfig,
108+
matchAgainstPath,
91109
} = getConfig(options);
92110

93111
return cy
@@ -115,14 +133,14 @@ Cypress.Commands.add(
115133
})
116134
.then(() => imgPath);
117135
})
118-
.then((imgPath) =>
119-
cy
136+
.then((imgPath) => {
137+
return cy
120138
.task<CompareImagesTaskReturn>(
121139
TASK.compareImages,
122140
{
123141
scaleFactor,
124142
imgNew: imgPath,
125-
imgOld: imgPath.replace(FILE_SUFFIX.actual, ""),
143+
imgOld: matchAgainstPath || imgPath.replace(FILE_SUFFIX.actual, ""),
126144
updateImages,
127145
maxDiffThreshold,
128146
diffConfig,
@@ -133,7 +151,7 @@ Cypress.Commands.add(
133151
res,
134152
imgPath,
135153
}))
136-
)
154+
})
137155
.then(({ res, imgPath }) => {
138156
const log = Cypress.log({
139157
name: "log",
@@ -170,6 +188,19 @@ Cypress.Commands.add(
170188
log.set("consoleProps", () => res);
171189
throw constructCypressError(log, new Error(res.message));
172190
}
191+
192+
return {
193+
diffValue: res.imgDiff,
194+
imgNewPath: imgPath,
195+
imgPath: imgPath.replace(FILE_SUFFIX.actual, ""),
196+
imgDiffPath: imgPath.replace(FILE_SUFFIX.actual, FILE_SUFFIX.diff),
197+
imgNewBase64: res.imgNewBase64,
198+
imgBase64: res.imgOldBase64,
199+
imgDiffBase64: res.imgDiffBase64,
200+
imgNew: typeof res.imgNewBase64 === 'string' ? Cypress.Buffer.from(res.imgNewBase64, 'base64') : undefined,
201+
img: typeof res.imgOldBase64 === 'string' ? Cypress.Buffer.from(res.imgOldBase64, 'base64') : undefined,
202+
imgDiff: typeof res.imgDiffBase64 === 'string' ? Cypress.Buffer.from(res.imgDiffBase64, 'base64') : undefined,
203+
}
173204
});
174205
}
175206
);

0 commit comments

Comments
 (0)