Skip to content
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

Add possibility to intercept vrtTrack result from the test #198

Merged
merged 6 commits into from
Sep 12, 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
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ cy.vrtStart();

### Assert

All `options` from `screenshot` command are also supported [more details](https://docs.cypress.io/api/commands/screenshot.html#Arguments)

```js
cy.vrtTrack("Whole page with default params");

Expand All @@ -137,13 +135,24 @@ cy.vrtTrack("Whole page with additional options", {
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)

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.

### Teadown

```js
Expand Down
30 changes: 30 additions & 0 deletions cypress/integration/error-callback.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* global cy */
/// <reference types="cypress" />
const vrtErrors=[];

function vrtTrack(name){
cy.vrtTrack(name, null, (err) => {
vrtErrors.push(err);
return true;
});
}

before(() => {
cy.vrtStart();
});

after(() => {
cy.vrtStop();
expect(vrtErrors).to.have.length(2);
});

context("Error Callback", () => {
beforeEach(() => {
cy.visit("https://jestjs.io/");
});

it("should track several screenshots", () => {
vrtTrack("Page with UI change 1");
vrtTrack("Page with UI change 2");
});
});
4 changes: 2 additions & 2 deletions lib/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export const addVrtTrackCommand = () =>
{
prevSubject: ["optional", "element", "window", "document"],
},
(subject, name, options) => {
(subject, name, options, errorCallback) => {
trackWithRetry(
() => trackImage(subject, name, options),
(result) => shouldStopRetry(result),
(result) => checkResult(result),
(result) => checkResult(result, errorCallback),
options?.retryLimit
);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ declare namespace Cypress {
Timeoutable &
ScreenshotOptions &
TrackOptions & { keepScreenshot?: boolean }
>
>,
errorCallback?: (err:string) => boolean
): Chainable<null>;

vrtTrackBuffer(
Expand Down
13 changes: 11 additions & 2 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,17 @@ export const trackWithRetry = (
});
};

export const checkResult = (result: TestRunResponse) =>
cy.task("VRT_PROCESS_ERROR_RESULT", result, { log: false }).then(handleError);
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;
}
}
handleError(err);
});

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