Skip to content

Commit

Permalink
feat: Set baseline branch for build
Browse files Browse the repository at this point in the history
  • Loading branch information
wsbaser authored Jan 27, 2023
1 parent 41d370c commit 3b565d7
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 10,415 deletions.
37 changes: 24 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ module.exports = (on, config) => {
// Required
"branchName": "develop",

// Branch with baseline
// Optional - when not set, main branch from project settings is used
"baselineBranchName": "release",

// Log errors instead of throwing exceptions
// Optional - default false
"enableSoftAssert": true,
Expand Down Expand Up @@ -108,6 +112,7 @@ VRT_PROJECT="Default project"
VRT_APIKEY="tXZVHX0EA4YQM1MGDD"
VRT_CIBUILDID="commit_sha"
VRT_BRANCHNAME="develop"
VRT_BASELINEBRANCHNAME="release"
VRT_ENABLESOFTASSERT=true
```

Expand All @@ -126,20 +131,25 @@ cy.vrtTrack("Whole page with default params");

cy.get("#navbar").vrtTrack("Separate element with default params");

cy.vrtTrack("Whole page with additional options", {
viewport: "1920x1080",
os: "MacOS",
device: "Cloud agent",
customTags: "Cloud, DarkTheme, Auth",
diffTollerancePercent: 1,
ignoreAreas: [{ x: 1, y: 2, width: 100, height: 200 }],
retryLimit: 2,
keepScreenshot: false, // Keep screenshot local copy, false by default
}, (err)=>{
console.log('Screenshot has diff with baseline', err);
return true; // Skip failing test
});
cy.vrtTrack(
"Whole page with additional options",
{
viewport: "1920x1080",
os: "MacOS",
device: "Cloud agent",
customTags: "Cloud, DarkTheme, Auth",
diffTollerancePercent: 1,
ignoreAreas: [{ x: 1, y: 2, width: 100, height: 200 }],
retryLimit: 2,
keepScreenshot: false, // Keep screenshot local copy, false by default
},
(err) => {
console.log("Screenshot has diff with baseline", err);
return true; // Skip failing test
}
);
```

##### options (optional)

Allows to set options for taking screenshot. All `options` from `screenshot` command are also supported [more details](https://docs.cypress.io/api/commands/screenshot.html#Arguments)
Expand All @@ -149,6 +159,7 @@ Viewport is taken from `Cypress.config()`, if option is not set
Browser is taken from `Cypress.browser.name`

##### errorCallbak (optional)

Allows you to define a callback that receives the error for custom side-effects.

Also allows to override assertion policy. When callback returns `true` this acts similar to `enableSoftAssertions` option in config, but allows to enable soft assertion only for one specific screenshot.
Expand Down
88 changes: 0 additions & 88 deletions cypress/integration/regression.spec.ts

This file was deleted.

9 changes: 2 additions & 7 deletions lib/commands.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/* global Cypress, cy */
import {
log,
shouldStopRetry,
checkResult,
trackImage,
trackWithRetry,
handleError,
trackBase64,
trackBuffer,
Expand Down Expand Up @@ -45,11 +43,8 @@ export const addVrtTrackCommand = () =>
prevSubject: ["optional", "element", "window", "document"],
},
(subject, name, options, errorCallback) => {
trackWithRetry(
() => trackImage(subject, name, options),
(result) => shouldStopRetry(result),
(result) => checkResult(result, errorCallback),
options?.retryLimit
trackImage(subject, name, options).then((result) =>
checkResult(result, errorCallback)
);
}
);
Expand Down
42 changes: 8 additions & 34 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,17 @@ export function addVisualRegressionTrackerPlugin(on, config) {
}
return null;
},
["VRT_TRACK_IMAGE_MULTIPART"]: async (props) => {
const data = multipartDtoToFormData({
...props,
buildId: vrt.buildId,
projectId: vrt.projectId,
branchName: vrt.config.branchName,
});

const result = await vrt["submitTestRunMultipart"](data);

if (!props.keepScreenshot) {
unlinkSync(props.imagePath);
}
return result;
},
["VRT_TRACK_BUFFER_MULTIPART"]: async (props) => {
const data = bufferDtoToFormData({
...props,
buildId: vrt.buildId,
projectId: vrt.projectId,
branchName: vrt.config.branchName,
});

const result = await vrt["submitTestRunMultipart"](data);
return result;
},
["VRT_TRACK_IMAGE_BASE64"]: async (props) => {
const result = await vrt["submitTestRunBase64"](props);
return result;
},
["VRT_PROCESS_ERROR_RESULT"]: async (testRunResult) => {
["VRT_TRACK"]: async (props) => {
try {
vrt["processTestRun"](testRunResult);
return await vrt.track(props, props?.retryLimit);
} catch (err) {
return err.message ?? err;
}
return null;
},
finally{
if (!props.keepScreenshot && props.imagePath) {
unlinkSync(props.imagePath);
}
}
}
});
}
47 changes: 16 additions & 31 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,40 +41,25 @@ export const toTestRunDto = ({
customTags: options?.customTags,
diffTollerancePercent: options?.diffTollerancePercent,
ignoreAreas: options?.ignoreAreas,
// next two properties actually do not belong to TestRunDto and ideally should be removed from this object
keepScreenshot: options?.keepScreenshot,
retryLimit: options?.retryLimit,
});

export const trackWithRetry = (
trackFn: () => Cypress.Chainable<TestRunResponse>,
shouldStopFn: (result: TestRunResponse) => boolean,
onStopFn: (result: TestRunResponse) => Cypress.Chainable<unknown>,
retryLimit: number = 2
): unknown => {
return trackFn().then((result) => {
if (retryLimit <= 0 || shouldStopFn(result)) {
onStopFn(result);
return;
}

log(`Diff found... Remaining retry attempts **${retryLimit}**`);
return trackWithRetry(trackFn, shouldStopFn, onStopFn, retryLimit - 1);
});
};

export const checkResult = (
result: TestRunResponse,
errorCallback?: (err:string) => boolean
) => cy.task("VRT_PROCESS_ERROR_RESULT", result, { log: false }).then((err) => {
if(err && errorCallback){
if(errorCallback(err as string)){
return;
result: TestRunResponse | string,
errorCallback?: (err: string) => boolean
) => {
if (typeof result === "string") {
// . this is an error message
if (errorCallback) {
if (errorCallback(result as string)) {
return;
}
}
handleError(result);
}
handleError(err);
});

export const shouldStopRetry = (result: TestRunResponse) =>
result?.status !== TestStatus.unresolved;
};

export const trackImage = (
subject: any,
Expand All @@ -97,7 +82,7 @@ export const trackImage = (
.then(() => log(`tracking ${name}`))
.then(() =>
cy.task(
"VRT_TRACK_IMAGE_MULTIPART",
"VRT_TRACK",
{
...toTestRunDto({ name, pixelRatio, options }),
imagePath,
Expand All @@ -114,7 +99,7 @@ export const trackBuffer = (
): Cypress.Chainable<TestRunResponse> => {
log(`tracking ${name}`);
return cy.task(
"VRT_TRACK_BUFFER_MULTIPART",
"VRT_TRACK",
{
...toTestRunDto({ name, options }),
imageBuffer,
Expand All @@ -130,7 +115,7 @@ export const trackBase64 = (
): Cypress.Chainable<TestRunResponse> => {
log(`tracking ${name}`);
return cy.task(
"VRT_TRACK_IMAGE_BASE64",
"VRT_TRACK",
{
...toTestRunDto({ name, options }),
imageBase64,
Expand Down
Loading

0 comments on commit 3b565d7

Please sign in to comment.