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

Use cypress run events to automatically signal the test run to VRT #200

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ Npm: https://www.npmjs.com/package/@visual-regression-tracker/agent-cypress
```js
import {
addVrtTrackCommand,
addVrtStartCommand,
addVrtStopCommand,
addVrtTrackBufferCommand,
addVrtTrackBase64Command,
} from "@visual-regression-tracker/agent-cypress/dist/commands";

addVrtStartCommand();
addVrtStopCommand();
addVrtTrackCommand();
addVrtTrackBufferCommand();
addVrtTrackBase64Command();
Expand Down Expand Up @@ -115,9 +111,7 @@ VRT_ENABLESOFTASSERT=true

### Setup

```js
cy.vrtStart();
```
vrtStart and vrtStop are now handled automatically when the testrun starts / ends. No need to call the functions in before and after blocks anymore.

### Assert

Expand Down
31 changes: 1 addition & 30 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add("login", (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
import {
addVrtTrackCommand,
addVrtStartCommand,
addVrtStopCommand,
} from "../../dist/commands";

addVrtStartCommand();
addVrtStopCommand();
addVrtTrackCommand();
addVrtTrackCommand();
28 changes: 0 additions & 28 deletions lib/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,6 @@ import {
trackBuffer,
} from "./utils";

export const addVrtStartCommand = () => {
Cypress.Commands.add(
"vrtStart",
{
prevSubject: ["optional"],
},
() => {
cy.task("VRT_START", {}, { log: false })
.then(handleError)
.then(() => log("Started"));
}
);
};

export const addVrtStopCommand = () => {
Cypress.Commands.add(
"vrtStop",
{
prevSubject: ["optional"],
},
() => {
cy.task("VRT_STOP", {}, { log: false })
.then(handleError)
.then(() => log("Stopped"));
}
);
};

export const addVrtTrackCommand = () =>
Cypress.Commands.add(
"vrtTrack",
Expand Down
44 changes: 23 additions & 21 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,33 @@ import {
bufferDtoToFormData,
} from "@visual-regression-tracker/sdk-js";

// We can't utilize Cypress Logger in tasks
reportError = (errorMessage) => {
console.error(`Visual Regresssion Tracker: ${errorMessage}`);
}
log = (message ) => {
console.log(`Visual Regression Tracker: ${message}`);
}

export function addVisualRegressionTrackerPlugin(on, config) {
const vrtConfig = config?.env?.visualRegressionTracker;
let vrt = new VisualRegressionTracker(vrtConfig);


on("before:run", async () => {
try {
await vrt.start();
} catch (err) {
reportError(`Error during startup: ${err.message ?? err}`);
}
},
on("after:run", async () => {
try {
await vrt.stop();
} catch (err) {
reportError(`Error when trying to stop: ${err.message ?? err}`);
}
},
on("task", {
["VRT_START"]: async (props) => {
try {
if (!vrt["isStarted"]()) {
await vrt.start();
}
} catch (err) {
return err.message ?? err;
}
return null;
},
["VRT_STOP"]: async (props) => {
try {
if (vrt["isStarted"]()) {
await vrt.stop();
}
} catch (err) {
return err.message ?? err;
}
return null;
},
["VRT_TRACK_IMAGE_MULTIPART"]: async (props) => {
const data = multipartDtoToFormData({
...props,
Expand Down