From 0cc7a2a356575c67b78002de381d12f8a49a3f6a Mon Sep 17 00:00:00 2001 From: Tobias Struckmeier Date: Fri, 16 Sep 2022 23:37:43 +0200 Subject: [PATCH 1/2] Use cypress run events to signal the test run to VRT In my opinion it would make sense if the plugin automatically starts and stops when the cypress run starts and stops. My suggestion would be to use the cypress events to do it. Makes the usage much simpler and also prevents the VRT plugin turn on / off the progress indicator for every spec. Whats your opinion on this? --- lib/plugin.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/plugin.js b/lib/plugin.js index 0633e27..93f4937 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -8,6 +8,13 @@ import { export function addVisualRegressionTrackerPlugin(on, config) { const vrtConfig = config?.env?.visualRegressionTracker; let vrt = new VisualRegressionTracker(vrtConfig); + + on("before:run", async () => { + await vrt.start(); + } + on("after:run", async () => { + await vrt.stop(); + } on("task", { ["VRT_START"]: async (props) => { From 9afa5388e165c5137fc37de34ac96f3437aa7bdb Mon Sep 17 00:00:00 2001 From: Tobias Struckmeier Date: Tue, 20 Sep 2022 00:35:51 +0200 Subject: [PATCH 2/2] Remove start/stop commands --- README.md | 8 +------ cypress/support/commands.js | 31 +------------------------ lib/commands.ts | 28 ----------------------- lib/plugin.js | 45 +++++++++++++++++-------------------- 4 files changed, 22 insertions(+), 90 deletions(-) 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 93f4937..489d385 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -5,38 +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 () => { - await vrt.start(); - } + try { + await vrt.start(); + } catch (err) { + reportError(`Error during startup: ${err.message ?? err}`); + } + }, on("after:run", async () => { - await vrt.stop(); - } - + 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,