diff --git a/src/browser/commands/assert-view/errors/base-state-error.ts b/src/browser/commands/assert-view/errors/base-state-error.ts index 26969fc07..c8709ea6e 100644 --- a/src/browser/commands/assert-view/errors/base-state-error.ts +++ b/src/browser/commands/assert-view/errors/base-state-error.ts @@ -1,7 +1,7 @@ -import { ImageInfo } from "../../../../types"; +import { ImageInfo, RefImageInfo } from "../../../../types"; export class BaseStateError extends Error { - constructor(public stateName: string, public currImg: ImageInfo, public refImg: ImageInfo) { + constructor(public stateName: string, public currImg: ImageInfo, public refImg: RefImageInfo) { super(); this.name = this.constructor.name; diff --git a/src/browser/commands/assert-view/errors/image-diff-error.ts b/src/browser/commands/assert-view/errors/image-diff-error.ts index 0a8cd555a..7bd60607b 100644 --- a/src/browser/commands/assert-view/errors/image-diff-error.ts +++ b/src/browser/commands/assert-view/errors/image-diff-error.ts @@ -1,4 +1,4 @@ -import { ImageInfo } from "../../../../types"; +import { ImageInfo, RefImageInfo } from "../../../../types"; import Image from "../../../../image"; import { BaseStateError } from "./base-state-error"; @@ -17,7 +17,7 @@ type DiffAreas = Pick; type ImageDiffErrorConstructor = new (params: { stateName: string; currImg: ImageInfo; - refImg: ImageInfo; + refImg: RefImageInfo; diffOpts: DiffOptions; diffAreas: DiffAreas; diffBuffer: Buffer; @@ -28,7 +28,7 @@ type ImageDiffErrorConstructor = new (params: { interface ImageDiffErrorData { stateName: string; currImg: ImageInfo; - refImg: ImageInfo; + refImg: RefImageInfo; diffOpts: DiffOptions; diffBounds: LooksSameResult["diffBounds"]; diffClusters: LooksSameResult["diffClusters"]; @@ -60,7 +60,7 @@ export class ImageDiffError extends BaseStateError { }: { stateName: string; currImg: ImageInfo; - refImg: ImageInfo; + refImg: RefImageInfo; diffOpts: DiffOptions; diffAreas?: DiffAreas; diffBuffer: Buffer; @@ -88,7 +88,7 @@ export class ImageDiffError extends BaseStateError { }: { stateName: string; currImg: ImageInfo; - refImg: ImageInfo; + refImg: RefImageInfo; diffOpts: DiffOptions; diffAreas?: DiffAreas; diffBuffer: Buffer; diff --git a/src/browser/commands/assert-view/errors/no-ref-image-error.ts b/src/browser/commands/assert-view/errors/no-ref-image-error.ts index 518e9a1a5..4ee532e81 100644 --- a/src/browser/commands/assert-view/errors/no-ref-image-error.ts +++ b/src/browser/commands/assert-view/errors/no-ref-image-error.ts @@ -1,12 +1,12 @@ import { BaseStateError } from "./base-state-error"; -import { ImageInfo } from "../../../../types"; +import { ImageInfo, RefImageInfo } from "../../../../types"; -type NoRefImageErrorConstructor = new (stateName: string, currImg: ImageInfo, refImg: ImageInfo) => T; +type NoRefImageErrorConstructor = new (stateName: string, currImg: ImageInfo, refImg: RefImageInfo) => T; interface NoRefImageErrorData { stateName: string; currImg: ImageInfo; - refImg: ImageInfo; + refImg: RefImageInfo; } export class NoRefImageError extends BaseStateError { @@ -14,7 +14,7 @@ export class NoRefImageError extends BaseStateError { this: NoRefImageErrorConstructor, stateName: string, currImg: ImageInfo, - refImg: ImageInfo, + refImg: RefImageInfo, ): T { return new this(stateName, currImg, refImg); } @@ -23,7 +23,7 @@ export class NoRefImageError extends BaseStateError { return new this(data.stateName, data.currImg, data.refImg); } - constructor(stateName: string, currImg: ImageInfo, refImg: ImageInfo) { + constructor(stateName: string, currImg: ImageInfo, refImg: RefImageInfo) { super(stateName, currImg, refImg); this.message = `can not find reference image at ${this.refImg.path} for "${stateName}" state`; diff --git a/src/browser/commands/assert-view/index.js b/src/browser/commands/assert-view/index.js index ebc3381d8..3ee59983b 100644 --- a/src/browser/commands/assert-view/index.js +++ b/src/browser/commands/assert-view/index.js @@ -1,6 +1,7 @@ "use strict"; const fs = require("fs-extra"); +const path = require("path"); const _ = require("lodash"); const Promise = require("bluebird"); const { pngValidator: validatePng } = require("png-validator"); @@ -86,7 +87,9 @@ module.exports.default = browser => { const currImg = { path: temp.path(Object.assign(tempOpts, { suffix: ".png" })), size: currSize }; const test = session.executionContext.ctx.currentTest; - const refImg = { path: config.getScreenshotPath(test, state), size: null }; + const refImgAbsolutePath = config.getScreenshotPath(test, state); + const refImgRelativePath = refImgAbsolutePath && path.relative(process.cwd(), refImgAbsolutePath); + const refImg = { path: refImgAbsolutePath, relativePath: refImgRelativePath, size: null }; const { emitter } = browser; if (!fs.existsSync(refImg.path)) { diff --git a/src/types/index.ts b/src/types/index.ts index b163109b7..46da860ee 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -62,6 +62,10 @@ export interface ImageInfo { size: ImageSize; } +export interface RefImageInfo extends ImageInfo { + relativePath: string; +} + export interface DiffOptions extends LooksSameOptions { current: string; reference: string; @@ -69,7 +73,7 @@ export interface DiffOptions extends LooksSameOptions { } export interface AssertViewResultSuccess { - refImg: ImageInfo; + refImg: RefImageInfo; stateName: string; } @@ -80,7 +84,7 @@ export interface AssertViewResultDiff { diffOpts: DiffOptions; message: string; name: "ImageDiffError"; - refImg: ImageInfo; + refImg: RefImageInfo; stack: string; stateName: string; differentPixels: number; @@ -91,7 +95,7 @@ export interface AssertViewResultNoRefImage { currImg: ImageInfo; message: string; name: "NoRefImageError"; - refImg: ImageInfo; + refImg: RefImageInfo; stack: string; stateName: string; } @@ -211,7 +215,7 @@ export type MasterEventHandler = { (event: Events["WARNING"], callback: () => void): T; (event: Events["ERROR"], callback: (err: Error) => void): T; - (event: Events["UPDATE_REFERENCE"], callback: (data: { state: string; refImg: ImageInfo }) => void): T; + (event: Events["UPDATE_REFERENCE"], callback: (data: { state: string; refImg: RefImageInfo }) => void): T; (event: Events["NEW_BROWSER"], callback: SyncSessionEventCallback): T; }; @@ -221,6 +225,6 @@ export type WorkerEventHandler = { (event: Events["AFTER_FILE_READ"], callback: (data: AfterFileReadData) => void): T; (event: Events["AFTER_TESTS_READ"], callback: (collection: TestCollection) => void): T; - (event: Events["UPDATE_REFERENCE"], callback: (data: { state: string; refImg: ImageInfo }) => void): T; + (event: Events["UPDATE_REFERENCE"], callback: (data: { state: string; refImg: RefImageInfo }) => void): T; (event: Events["NEW_BROWSER"], callback: SyncSessionEventCallback): T; }; diff --git a/src/worker/testplane.ts b/src/worker/testplane.ts index 8da53a89e..d5e7c31ec 100644 --- a/src/worker/testplane.ts +++ b/src/worker/testplane.ts @@ -2,7 +2,7 @@ import { passthroughEvent } from "../events/utils"; import { WorkerEvents } from "../events"; import Runner from "./runner"; import { BaseTestplane } from "../base-testplane"; -import { ImageInfo, WdioBrowser, WorkerEventHandler } from "../types"; +import { RefImageInfo, WdioBrowser, WorkerEventHandler } from "../types"; export interface WorkerRunTestOpts { browserId: string; @@ -16,7 +16,7 @@ export interface WorkerRunTestOpts { export interface AssertViewResultsSuccess { stateName: string; - refImg: ImageInfo; + refImg: RefImageInfo; } export interface WorkerRunTestTestplaneCtx { diff --git a/test/src/browser/commands/assert-view/index.js b/test/src/browser/commands/assert-view/index.js index 495b20a4c..c135a7ab4 100644 --- a/test/src/browser/commands/assert-view/index.js +++ b/test/src/browser/commands/assert-view/index.js @@ -93,6 +93,8 @@ describe("assertView command", () => { sandbox.stub(updateRefs, "handleNoRefImage").resolves(); sandbox.stub(updateRefs, "handleImageDiff").resolves(); + + sandbox.stub(process, "cwd").returns("/ref/cwd"); }); afterEach(() => sandbox.restore()); @@ -442,7 +444,7 @@ describe("assertView command", () => { NoRefImageError.create, "state", { path: "/curr/path", size: { width: 100, height: 200 } }, - { path: "/ref/path", size: null }, + { path: "/ref/path", relativePath: "../path", size: null }, ); }); @@ -894,8 +896,22 @@ describe("assertView command", () => { await fn(browser, "complex"); assert.deepEqual(browser.publicAPI.executionContext.testplaneCtx.assertViewResults.get(), [ - { stateName: "plain", refImg: { path: "/ref/path/plain", size: { width: 100, height: 200 } } }, - { stateName: "complex", refImg: { path: "/ref/path/complex", size: { width: 300, height: 400 } } }, + { + stateName: "plain", + refImg: { + path: "/ref/path/plain", + relativePath: "../path/plain", + size: { width: 100, height: 200 }, + }, + }, + { + stateName: "complex", + refImg: { + path: "/ref/path/complex", + relativePath: "../path/complex", + size: { width: 300, height: 400 }, + }, + }, ]); }); });