forked from stracker-phil/codeceptjs-pixelmatchhelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
353 lines (353 loc) · 11.2 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
export = PixelmatchHelper;
/**
* Helper class that integrates pixelmatch into CodeceptJS for visual regression
* tests.
*
* helpers: {
* PixelmatchHelper: {
* require: "codeceptjs-pixelmatchhelper",
* dirExpected: "./tests/screenshots/base/",
* dirDiff: "./tests/screenshots/diff/",
* dirActual: "./tests/output/", // Optional. Defaults to global.output_dir.
* diffPrefix: "Diff_" // Optional. Defaults to "Diff_"
* tolerance: 1.5,
* threshold: 0.1,
* dumpIntermediateImage: false,
* captureActual: true,
* captureExpected: true
* }
* }
*
* @author Philipp Stracker
*/
declare class PixelmatchHelper {
/**
* Constructor that initializes the helper.
* Called internally by CodeceptJS.
*
* @param {object} config
*/
constructor(config: object);
/**
* Relative path to the folder that contains relevant images.
*
* @type {{expected: string, actual: string, diff: string}}
*/
globalDir: {
expected: string;
actual: string;
diff: string;
};
/**
* Default tolserance level for comparisons.
*
* @type {float}
*/
globalTolerance: float;
/**
* Default threshold for all comparisons.
*
* @type {float}
*/
globalThreshold: float;
/**
* Filename prefix for generated difference files.
* @type {string}
*/
globalDiffPrefix: string;
/**
* Whether to save the intermediate images to the global output folder,
* after applying the bounds and ignore-boxes.
*
* Useful for debugging tests, but not recommended for production usage.
*
* @type {boolean}
*/
globalDumpIntermediateImage: boolean;
/**
* Whether to capture a new screenshot and use it as actual image, instead
* of loading the image from the `dirActual` folder.
*
* The new screenshot is saved to the `dirActual` folder before comparison,
* and will replace an existing file with the same name!
*
* @type {boolean|'missing'}
*/
globalCaptureActual: boolean | 'missing';
/**
* Whether to update the expected base image with a current screenshot
* before starting the comparison.
*
* The new screenshot is saved to the `dirExpected` folder, and will
* replace an existing file with the same name!
*
* @type {boolean|'missing'}
*/
globalCaptureExpected: boolean | 'missing';
/**
* Contains the image paths for the current test.
*
* @type {{expected: string, actual: string, diff: string}}
*/
path: {
expected: string;
actual: string;
diff: string;
};
/**
* Comparison options.
*
* @type {object}
*/
options: object;
/**
* Name of the image to compare.
*
* @type {string}
*/
imageName: string;
/**
* Holds comparison results.
*
* @type {{match: boolean, difference: float, diffImage: string, diffPixels: integer,
* totalPixels: integer, relevantPixels: integer}}
*/
result: {
match: boolean;
difference: float;
diffImage: string;
diffPixels: integer;
totalPixels: integer;
relevantPixels: integer;
};
/**
* Compares the given screenshot with the expected image. When too many
* differences are detected, the test will fail.
*
* I.checkVisualDifferences('dashboard.png');
* I.checkVisualDifferences('dashboard.png', { screenshot: true });
*
* @param {string} image - Name of the input image to compare.
* @param {object} [options] - Optional options for the comparison.
* @return {Promise}
*/
checkVisualDifferences(image: string, options?: object): Promise<any>;
/**
* Compares the given screenshot with the expected image and updates the
* class member `this.result` with details. This function does to trigger an
* assertion but can throw an error, when the images cannot be compared.
*
* @param {string} image - Name of the input image to compare.
* @param {object} [options] - Optional options for the comparison.
* @return {{match: boolean, difference: float}} Comparison details.
*/
getVisualDifferences(image: string, options?: object): {
match: boolean;
difference: float;
};
/**
* Take screenshot of individual element.
*
* @param {string} name - Name of the output image.
* @param {'actual'|'expected'} [which] - Optional. Whether the screenshot is
* the expected bas eimage, or an actual image for comparison.
* Defaults to 'actual'.
* @param {string} [element] - Optional. Selector of the element to
* screenshot, or empty to screenshot current viewport.
* @returns {Promise}
*/
takeScreenshot(name: string, which?: 'actual' | 'expected', element?: string): Promise<any>;
/**
* Takes a screenshot of the entire viewport and saves it as either an
* actual image, or an expected base-image.
*
* @param {string} name - Name of the output image.
* @param {'actual'|'expected'} which - Optional. Whether the screenshot is
* the expected bas eimage, or an actual image for comparison.
* Defaults to 'actual'.
* @param {string} element - Optional. Selector of the element to
* screenshot, or empty to screenshot current viewport.
* @private
*/
private _takeElementScreenshot;
/**
* Takes a screenshot of the entire viewport and saves it as either an
* actual image, or an expected base-image.
*
* @param {string} name - Name of the output image.
* @param {'actual'|'expected'} which - Optional. Whether the screenshot is
* the expected bas eimage, or an actual image for comparison.
* Defaults to 'actual'.
* @private
*/
private _takeScreenshot;
/**
* Clears pixels in the specified image that are outside the bounding rect
* or inside an ignored area.
*
* @param {PNG} png - The image to modify.
* @return {int} Number of cleared pixels.
* @private
*/
private _applyBounds;
/**
* Determines the bounding box of the given element on the current viewport.
*
* @param {string} selector - CSS|XPath|ID selector.
* @returns {Promise<{boundingBox: {left: int, top: int, right: int, bottom: int, width: int,
* height: int}}>}
*/
_getBoundingBox(selector: string): Promise<{
boundingBox: {
left: int;
top: int;
right: int;
bottom: int;
width: int;
height: int;
};
}>;
/**
* Captures the expected or actual image, depending on the captureFlag.
*
* @param {string} which - Which image to capture: 'expected', 'actual'.
* @param {bool|string} captureFlag - Either true, false or 'missing'.
* @private
*/
private _maybeCaptureImage;
/**
* Sanitizes the given options and updates all relevant class members with
* either the new, sanitized value, or with a default value.
*
* @param {string} image - Name of the image to compare.
* @param {object|undefined} options - The new options to set.
* @private
*/
private _setupTest;
/**
* Returns the instance of the current browser driver.
*
* @return {Puppeteer|WebDriver|Appium|WebDriverIO|TestCafe}
* @private
*/
private _getDriver;
/**
* Recursively creates the specified directory.
*
* @param dir
* @private
*/
private _mkdirp;
/**
* Deletes the specified file, if it exists.
*
* @param {string} file - The file to delete.
* @private
*/
private _deleteFile;
/**
* Tests, if the given file exists..
*
* @param {string} file - The file to check.
* @param {string} mode - Optional. Either empty, or 'read'/'write' to
* validate that the current user can either read or write the file.
* @private
*/
private _isFile;
/**
* Builds the absolute path to a relative folder.
*
* @param {string} dir - The relative folder name.
* @returns {string}
* @private
*/
private _resolvePath;
/**
* Returns the filename of an image.
*
* @param {string} which - Which image to return (expected, actual, diff).
* @param {string} suffix - Optional. A suffix to append to the filename.
* @private
*/
private _getFileName;
/**
* Builds an image path using the current image name and the specified folder.
*
* @param {string} which - The image to load (expected, actual, diff).
* @param {string} suffix - Optional. A suffix to append to the filename.
* @returns {string} Path to the image.
* @private
*/
private _buildPath;
/**
* Returns a list of absolute image paths of base images for the comparison.
* All files in the returned list exist in the filesystem.
*
* Naming convention:
*
* Files that contain a trailing "~<num>" suffix are considered part of the
* matching list.
*
* For example:
*
* image: "google-home"
* files:
* "google-home.png" # exact match
* "google-home~1.png" # variation
* "google-home~83.png" # variation
*
* @return {string[]}
* @private
*/
private _getExpectedImagePaths;
/**
* Loads the specified image and returns a PNG blob.
*
* @param {string} which - The image to load (expected, actual, diff).
* @param {string} suffix - Optional. A suffix to append to the filename.
* @return {object} An PNG object.
* @private
*/
private _loadPngImage;
/**
* Saves the specified PNG image to the filesystem.
* .
* @param {string} which - The image to load (expected, actual, diff).
* @param {object} png - An PNG image object.
* @param {string} suffix - Optional. A suffix to append to the filename.
* @private
*/
private _savePngImage;
/**
* Clears a rectangular area inside the given PNG image object. The change
* is only applied in-memory and does not affect the saved image.
*
* @param {object} png - The PNG object.
* @param {int} x0
* @param {int} y0
* @param {int} x1
* @param {int} y1
* @return {int} Number of cleared pixels.
* @private
*/
private _clearRect;
/**
* Casts the given value into a boolean. Several string terms are translated
* to boolean true. If validTerms are specified, and the given value matches
* one of those validTerms, the term is returned instead of a boolean.
*
* Sample:
*
* _toBool('yes') --> true
* _toBool('n') --> false
* _toBool('any') --> false
* _toBool('ANY', ['any', 'all']) --> 'any'
*
* @param {any} value - The value to cast.
* @param {array} validTerms - List of terms that should not be cast to a
* boolean but returned directly.
* @return {bool|string} Either a boolean or a lowercase string.
* @private
*/
private _toBool;
}