Skip to content

Commit 08cef0e

Browse files
authored
Merge pull request #33 from AnglesHQ/visual-verification-methods
Add comparison algorithm options to screenshot compare methods
2 parents 6b86937 + 20763db commit 08cef0e

3 files changed

Lines changed: 51 additions & 7 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export class CompareOptions {
2+
/** Comparison algorithm: 'pixel' (default), 'ssim', or 'phash' (JSON endpoints only). */
3+
algorithm?: 'pixel' | 'ssim' | 'phash';
4+
/** Per-pixel colour-distance threshold (0-1, pixel algorithm only). Defaults to 0.5. */
5+
threshold?: number;
6+
/** When true (pixel algorithm only), changed pixels are clustered into regions. */
7+
regions?: boolean;
8+
}
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1+
export class DiffRegion {
2+
x: number;
3+
y: number;
4+
width: number;
5+
height: number;
6+
/** Number of changed pixels inside the region. */
7+
pixels: number;
8+
}
9+
110
export class ImageCompareResponse {
11+
/** Algorithm that produced this result: 'pixel', 'ssim', or 'phash'. */
12+
algorithm?: string;
213
isSameDimensions: boolean;
314
rawMisMatchPercentage: number;
415
misMatchPercentage: number;
516
analysisTime: number;
6-
}
17+
/** SSIM score in [-1, 1] (1 = identical); present when algorithm is 'ssim'. */
18+
ssim?: number;
19+
/** Normalised perceptual-hash distance (0-1); present when algorithm is 'phash'. */
20+
distance?: number;
21+
/** Clustered change regions; present when requested with regions=true (pixel only). */
22+
regions?: DiffRegion[];
23+
}

src/lib/requests/ScreenshotRequests.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { StoreScreenshot } from '../models/requests/StoreScreenshot';
66
import { ImageCompareResponse } from '../models/response/ImageCompareResponse';
77
import { ImageFindResponse } from '../models/response/ImageFindResponse';
88
import { FindImageOptions } from '../models/requests/FindImageOptions';
9+
import { CompareOptions } from '../models/requests/CompareOptions';
910
import {DefaultResponse} from "../models/response/DefaultResponse";
1011

1112
export class ScreenshotRequests extends BaseRequests {
@@ -122,21 +123,39 @@ export class ScreenshotRequests extends BaseRequests {
122123
return this.get<Screenshot>(path);
123124
}
124125

125-
// TODO: Get screenshot compare Resemblejs JSON
126+
/**
127+
* Compares two stored screenshots and returns the comparison statistics.
128+
* @param {CompareOptions} [options] - algorithm/threshold/regions
129+
*/
130+
public compareScreenshots(screenshotId: string, screenshotCompareId: string, options?: CompareOptions): Promise<ImageCompareResponse> {
131+
return this.get<ImageCompareResponse>(`screenshot/${screenshotId}/compare/${screenshotCompareId}`, {
132+
params: options,
133+
});
134+
}
126135

127-
// TODO: get screenshot compare Resemblejs Image
136+
/**
137+
* Compares two stored screenshots and resolves with the diff image.
138+
*/
139+
public compareScreenshotsImage(screenshotId: string, screenshotCompareId: string, cache?: boolean, options?: CompareOptions): Promise<AxiosResponse> {
140+
return this.get<AxiosResponse>(`screenshot/${screenshotId}/compare/${screenshotCompareId}/image`, {
141+
params: { useCache: cache || false, ...options },
142+
responseType: 'arraybuffer',
143+
});
144+
}
128145

129-
public getBaselineCompareImage(screenshotId: string, cache: boolean): Promise<AxiosResponse> {
146+
public getBaselineCompareImage(screenshotId: string, cache: boolean, options?: CompareOptions): Promise<AxiosResponse> {
130147
const useCache = cache || false;
131148
return this.get<AxiosResponse>(`screenshot/${screenshotId}/baseline/compare/image/`,
132149
{
133-
params: { useCache },
150+
params: { useCache, ...options },
134151
responseType: 'arraybuffer'
135152
});
136153
}
137154

138-
public getBaselineCompare(screenshotId: string) : Promise<ImageCompareResponse> {
139-
return this.get<ImageCompareResponse>(`screenshot/${screenshotId}/baseline/compare/`);
155+
public getBaselineCompare(screenshotId: string, options?: CompareOptions) : Promise<ImageCompareResponse> {
156+
return this.get<ImageCompareResponse>(`screenshot/${screenshotId}/baseline/compare/`, {
157+
params: options,
158+
});
140159
}
141160

142161
/**

0 commit comments

Comments
 (0)