Skip to content

feat: add matchAgainstPath option #146

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

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ cy.matchImage({
// title used for naming the image file
// default: Cypress.currentTest.titlePath (your test title)
title: `${Cypress.currentTest.titlePath.join(' ')} (${Cypress.browser.displayName})`
// pass a path to custom image that should be used for comparison
// instead of checking against the image from previous run
// default: undefined
matchAgainstPath: '/path/to/reference-image.png'
})
```

Expand Down
4 changes: 4 additions & 0 deletions example/cypress/e2e/spec.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ describe('My First Test', () => {
cy.visit('/')
cy.contains('h1', 'Welcome to Your Vue.js App')
cy.matchImage()
.then(({ imgNewPath }) => {
// match against image from custom path
cy.matchImage({ matchAgainstPath: imgNewPath });
})
})
})
41 changes: 36 additions & 5 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ declare global {
imagesDir?: string;
maxDiffThreshold?: number;
title?: string;
matchAgainstPath?: string;
// IDEA: to be implemented if support for files NOT from filesystem needed
// matchAgainst?: string | Buffer;
};

type MatchImageReturn = {
diffValue: number | undefined;
imgNewPath: string;
imgPath: string;
imgDiffPath: string;
imgNewBase64: string | undefined;
imgBase64: string | undefined;
imgDiffBase64: string | undefined;
imgNew: InstanceType<Cypress['Buffer']> | undefined;
img: InstanceType<Cypress['Buffer']> | undefined;
imgDiff: InstanceType<Cypress['Buffer']> | undefined;
};

interface Chainable<Subject> {
Expand All @@ -21,7 +37,7 @@ declare global {
* @memberof Cypress.Chainable
* @example cy.get('.my-element').matchImage();
*/
matchImage(options?: Cypress.MatchImageOptions): Chainable<Subject>;
matchImage(options?: Cypress.MatchImageOptions): Chainable<MatchImageReturn>;
}
}
}
Expand Down Expand Up @@ -69,6 +85,7 @@ export const getConfig = (options: Cypress.MatchImageOptions) => ({
| Partial<Cypress.ScreenshotDefaultsOptions>
| undefined) ||
{},
matchAgainstPath: options.matchAgainstPath || undefined,
});

Cypress.Commands.add(
Expand All @@ -88,6 +105,7 @@ Cypress.Commands.add(
maxDiffThreshold,
diffConfig,
screenshotConfig,
matchAgainstPath,
} = getConfig(options);

return cy
Expand Down Expand Up @@ -115,14 +133,14 @@ Cypress.Commands.add(
})
.then(() => imgPath);
})
.then((imgPath) =>
cy
.then((imgPath) => {
return cy
.task<CompareImagesTaskReturn>(
TASK.compareImages,
{
scaleFactor,
imgNew: imgPath,
imgOld: imgPath.replace(FILE_SUFFIX.actual, ""),
imgOld: matchAgainstPath || imgPath.replace(FILE_SUFFIX.actual, ""),
updateImages,
maxDiffThreshold,
diffConfig,
Expand All @@ -133,7 +151,7 @@ Cypress.Commands.add(
res,
imgPath,
}))
)
})
.then(({ res, imgPath }) => {
const log = Cypress.log({
name: "log",
Expand Down Expand Up @@ -170,6 +188,19 @@ Cypress.Commands.add(
log.set("consoleProps", () => res);
throw constructCypressError(log, new Error(res.message));
}

return {
diffValue: res.imgDiff,
imgNewPath: imgPath,
imgPath: imgPath.replace(FILE_SUFFIX.actual, ""),
imgDiffPath: imgPath.replace(FILE_SUFFIX.actual, FILE_SUFFIX.diff),
imgNewBase64: res.imgNewBase64,
imgBase64: res.imgOldBase64,
imgDiffBase64: res.imgDiffBase64,
imgNew: typeof res.imgNewBase64 === 'string' ? Cypress.Buffer.from(res.imgNewBase64, 'base64') : undefined,
img: typeof res.imgOldBase64 === 'string' ? Cypress.Buffer.from(res.imgOldBase64, 'base64') : undefined,
imgDiff: typeof res.imgDiffBase64 === 'string' ? Cypress.Buffer.from(res.imgDiffBase64, 'base64') : undefined,
}
});
}
);