Skip to content

[SDK-3090] Remote screenshotter service support #1

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 11 commits into from
Oct 28, 2022
55 changes: 44 additions & 11 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare global {
namespace Cypress {
type MatchImageOptions = {
screenshotConfig?: Partial<Cypress.ScreenshotDefaultsOptions>;
remoteScreenshotServiceUrl?: string;
diffConfig?: Parameters<typeof pixelmatch>[5];
updateImages?: boolean;
/**
Expand Down Expand Up @@ -90,6 +91,12 @@ export const getConfig = (options: Cypress.MatchImageOptions) => {
| Partial<Cypress.ScreenshotDefaultsOptions>
| undefined) ||
{},
remoteScreenshotServiceUrl:
options.remoteScreenshotServiceUrl ||
(Cypress.env("pluginVisualRegressionRemoteScreenshotServiceUrl") as
| string
| undefined) ||
{},
};
};

Expand All @@ -107,6 +114,7 @@ Cypress.Commands.add(
maxDiffThreshold,
diffConfig,
screenshotConfig,
remoteScreenshotServiceUrl,
} = getConfig(options);

return cy
Expand All @@ -124,17 +132,42 @@ Cypress.Commands.add(
)
.then(({ screenshotPath, title: titleFromTask }) => {
title = titleFromTask;
let imgPath: string;
return ($el ? cy.wrap($el) : cy)
.screenshot(screenshotPath, {
...screenshotConfig,
onAfterScreenshot(el, props) {
imgPath = props.path;
screenshotConfig.onAfterScreenshot?.(el, props);
},
log: false,
})
.then(() => imgPath);

if (remoteScreenshotServiceUrl) {
return cy
.document()
.then((doc) => {
return cy
.request({
url: remoteScreenshotServiceUrl,
method: "POST",
encoding: "binary",
body: {
html: ($el?.html() || doc.body.parentElement?.innerHTML),
},
} as Cypress.RequestOptions)
.then((response) => {
return cy.writeFile(
screenshotPath as string,
response.body,
"binary"
);
});
})
.then(() => screenshotPath as string);
} else {
let imgPath: string;
return ($el ? cy.wrap($el) : cy)
.screenshot(screenshotPath, {
...screenshotConfig,
onAfterScreenshot(el, props) {
imgPath = props.path;
screenshotConfig.onAfterScreenshot?.(el, props);
},
log: false,
})
.then(() => imgPath);
}
})
.then((imgPath) =>
cy
Expand Down