diff --git a/README.md b/README.md index 827b1db..88b5afb 100644 --- a/README.md +++ b/README.md @@ -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(); @@ -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 diff --git a/cypress/support/commands.js b/cypress/support/commands.js index 000126d..c09387a 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -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(); \ No newline at end of file diff --git a/lib/commands.ts b/lib/commands.ts index c5b256f..e10d8b8 100644 --- a/lib/commands.ts +++ b/lib/commands.ts @@ -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", diff --git a/lib/plugin.js b/lib/plugin.js index 0633e27..489d385 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -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,