Skip to content

Commit

Permalink
Merge pull request #954 from gemini-testing/TESTPLANE-90.relativePath
Browse files Browse the repository at this point in the history
feat: add relativePath to refImg
  • Loading branch information
KuznetsovRoman authored Jun 19, 2024
2 parents ded818b + 0bb23cf commit cb9da78
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/browser/commands/assert-view/errors/base-state-error.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
10 changes: 5 additions & 5 deletions src/browser/commands/assert-view/errors/image-diff-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImageInfo } from "../../../../types";
import { ImageInfo, RefImageInfo } from "../../../../types";

import Image from "../../../../image";
import { BaseStateError } from "./base-state-error";
Expand All @@ -17,7 +17,7 @@ type DiffAreas = Pick<LooksSameResult, "diffClusters" | "diffBounds">;
type ImageDiffErrorConstructor<T> = new (params: {
stateName: string;
currImg: ImageInfo;
refImg: ImageInfo;
refImg: RefImageInfo;
diffOpts: DiffOptions;
diffAreas: DiffAreas;
diffBuffer: Buffer;
Expand All @@ -28,7 +28,7 @@ type ImageDiffErrorConstructor<T> = new (params: {
interface ImageDiffErrorData {
stateName: string;
currImg: ImageInfo;
refImg: ImageInfo;
refImg: RefImageInfo;
diffOpts: DiffOptions;
diffBounds: LooksSameResult["diffBounds"];
diffClusters: LooksSameResult["diffClusters"];
Expand Down Expand Up @@ -60,7 +60,7 @@ export class ImageDiffError extends BaseStateError {
}: {
stateName: string;
currImg: ImageInfo;
refImg: ImageInfo;
refImg: RefImageInfo;
diffOpts: DiffOptions;
diffAreas?: DiffAreas;
diffBuffer: Buffer;
Expand Down Expand Up @@ -88,7 +88,7 @@ export class ImageDiffError extends BaseStateError {
}: {
stateName: string;
currImg: ImageInfo;
refImg: ImageInfo;
refImg: RefImageInfo;
diffOpts: DiffOptions;
diffAreas?: DiffAreas;
diffBuffer: Buffer;
Expand Down
10 changes: 5 additions & 5 deletions src/browser/commands/assert-view/errors/no-ref-image-error.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { BaseStateError } from "./base-state-error";
import { ImageInfo } from "../../../../types";
import { ImageInfo, RefImageInfo } from "../../../../types";

type NoRefImageErrorConstructor<T> = new (stateName: string, currImg: ImageInfo, refImg: ImageInfo) => T;
type NoRefImageErrorConstructor<T> = new (stateName: string, currImg: ImageInfo, refImg: RefImageInfo) => T;

interface NoRefImageErrorData {
stateName: string;
currImg: ImageInfo;
refImg: ImageInfo;
refImg: RefImageInfo;
}

export class NoRefImageError extends BaseStateError {
static create<T extends NoRefImageError>(
this: NoRefImageErrorConstructor<T>,
stateName: string,
currImg: ImageInfo,
refImg: ImageInfo,
refImg: RefImageInfo,
): T {
return new this(stateName, currImg, refImg);
}
Expand All @@ -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`;
Expand Down
5 changes: 4 additions & 1 deletion src/browser/commands/assert-view/index.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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)) {
Expand Down
14 changes: 9 additions & 5 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,18 @@ export interface ImageInfo {
size: ImageSize;
}

export interface RefImageInfo extends ImageInfo {
relativePath: string;
}

export interface DiffOptions extends LooksSameOptions {
current: string;
reference: string;
diffColor: string;
}

export interface AssertViewResultSuccess {
refImg: ImageInfo;
refImg: RefImageInfo;
stateName: string;
}

Expand All @@ -80,7 +84,7 @@ export interface AssertViewResultDiff {
diffOpts: DiffOptions;
message: string;
name: "ImageDiffError";
refImg: ImageInfo;
refImg: RefImageInfo;
stack: string;
stateName: string;
differentPixels: number;
Expand All @@ -91,7 +95,7 @@ export interface AssertViewResultNoRefImage {
currImg: ImageInfo;
message: string;
name: "NoRefImageError";
refImg: ImageInfo;
refImg: RefImageInfo;
stack: string;
stateName: string;
}
Expand Down Expand Up @@ -211,7 +215,7 @@ export type MasterEventHandler<T extends BaseTestplane> = {
(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;
};

Expand All @@ -221,6 +225,6 @@ export type WorkerEventHandler<T extends BaseTestplane> = {
(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;
};
4 changes: 2 additions & 2 deletions src/worker/testplane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,7 +16,7 @@ export interface WorkerRunTestOpts {

export interface AssertViewResultsSuccess {
stateName: string;
refImg: ImageInfo;
refImg: RefImageInfo;
}

export interface WorkerRunTestTestplaneCtx {
Expand Down
22 changes: 19 additions & 3 deletions test/src/browser/commands/assert-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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 },
);
});

Expand Down Expand Up @@ -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 },
},
},
]);
});
});
Expand Down

0 comments on commit cb9da78

Please sign in to comment.