|
| 1 | +import { basename } from "path"; |
1 | 2 | import * as BlinkDiff from "blink-diff";
|
2 | 3 | import * as PngJsImage from "pngjs-image";
|
3 | 4 | import { ImageOptions } from "./image-options";
|
4 | 5 | import { INsCapabilities } from "./interfaces/ns-capabilities";
|
5 | 6 | import { IRectangle } from "./interfaces/rectangle";
|
6 | 7 | import { LogImageType } from "./enums/log-image-type";
|
7 |
| -import { basename } from "path"; |
| 8 | +import { UIElement } from "./ui-element"; |
| 9 | +import { AppiumDriver } from "./appium-driver"; |
| 10 | +import { logError } from "./utils"; |
| 11 | + |
| 12 | +export interface IImageCompareOptions { |
| 13 | + imageName?: string; |
| 14 | + timeOutSeconds?: number; |
| 15 | + tolerance?: number; |
| 16 | + toleranceType?: ImageOptions; |
| 17 | + /** |
| 18 | + * wait miliseconds before capture creating image |
| 19 | + */ |
| 20 | + waitOnCreatingInitialSnapshot?: number; |
| 21 | + /** |
| 22 | + * This property will keep image name as it is and will not add _actual postfix on initial capture |
| 23 | + */ |
| 24 | + preserveImageName?: boolean; |
| 25 | +} |
8 | 26 |
|
9 | 27 | export class ImageHelper {
|
10 | 28 |
|
11 | 29 | private _imageCropRect: IRectangle;
|
12 | 30 | private _blockOutAreas: IRectangle[];
|
13 |
| - private _waitOnCreatingInitialSnapshot: number; |
| 31 | + private _imagesResults = new Map<string, boolean>(); |
| 32 | + private _testName: string; |
| 33 | + private _options: IImageCompareOptions = { |
| 34 | + timeOutSeconds: 2, |
| 35 | + tolerance: 0, |
| 36 | + toleranceType: ImageOptions.pixel, |
| 37 | + waitOnCreatingInitialSnapshot: 2000, |
| 38 | + preserveImageName: false, |
| 39 | + }; |
| 40 | + |
| 41 | + constructor(private _args: INsCapabilities, private _driver: AppiumDriver) { |
| 42 | + } |
| 43 | + |
| 44 | + get options() { |
| 45 | + return this._options; |
| 46 | + } |
| 47 | + |
| 48 | + set options(options: IImageCompareOptions) { |
| 49 | + this._options = this.extendOptions(options); |
| 50 | + } |
14 | 51 |
|
15 |
| - constructor(private _args: INsCapabilities) { |
| 52 | + set testName(testName: string) { |
| 53 | + this._testName = testName; |
16 | 54 | }
|
17 | 55 |
|
18 |
| - get waitOnCreatingInitialSnapshot() { |
19 |
| - return this._waitOnCreatingInitialSnapshot; |
| 56 | + get testName() { |
| 57 | + return this._testName; |
20 | 58 | }
|
21 | 59 |
|
22 |
| - set waitOnCreatingInitialSnapshot(waitOnCreatingInitialSnapshot: number) { |
23 |
| - this._waitOnCreatingInitialSnapshot = waitOnCreatingInitialSnapshot; |
| 60 | + get imageComppareOptions() { |
| 61 | + this.extendOptions(this._options); |
| 62 | + |
| 63 | + return this._options; |
| 64 | + } |
| 65 | + |
| 66 | + set imageComppareOptions(imageComppareOptions: IImageCompareOptions) { |
| 67 | + this._options = this.extendOptions(imageComppareOptions); |
| 68 | + } |
| 69 | + |
| 70 | + public async compareScreen(options?: IImageCompareOptions) { |
| 71 | + options = this.extendOptions(options); |
| 72 | + const imageName = this.increaseImageName(options.imageName || this._testName); |
| 73 | + const result = await this._driver.compareScreen(imageName, options.timeOutSeconds, options.tolerance, options.toleranceType); |
| 74 | + this._imagesResults.set(imageName, result); |
| 75 | + |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
| 79 | + public async compareElement(element: UIElement, options?: IImageCompareOptions) { |
| 80 | + options = this.extendOptions(options); |
| 81 | + const imageName = this.increaseImageName(options.imageName || this._testName); |
| 82 | + const result = await this._driver.compareElement(element, imageName, options.tolerance, options.timeOutSeconds, options.toleranceType); |
| 83 | + this._imagesResults.set(imageName, result); |
| 84 | + |
| 85 | + return result; |
| 86 | + } |
| 87 | + |
| 88 | + public async compareRectangle(element: IRectangle, options?: IImageCompareOptions) { |
| 89 | + options = this.extendOptions(options); |
| 90 | + const imageName = this.increaseImageName(options.imageName || this._testName); |
| 91 | + const result = await this._driver.compareRectangle(element, imageName, options.timeOutSeconds, options.tolerance, options.toleranceType); |
| 92 | + this._imagesResults.set(imageName, result); |
| 93 | + |
| 94 | + return result; |
| 95 | + } |
| 96 | + |
| 97 | + public hasImageComparisonPassed() { |
| 98 | + let shouldFailTest = true; |
| 99 | + console.log(); |
| 100 | + this._imagesResults.forEach((v, k, map) => { |
| 101 | + if (!this._imagesResults.get(k)) { |
| 102 | + shouldFailTest = false; |
| 103 | + this._driver.testReporterLog(`Image comparison for image ${k} has failed!`); |
| 104 | + logError(`Image comparison for image ${k} has failed`); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + this.reset(); |
| 109 | + return shouldFailTest; |
| 110 | + } |
| 111 | + |
| 112 | + public reset() { |
| 113 | + this._imagesResults.clear(); |
| 114 | + } |
| 115 | + |
| 116 | + private increaseImageName(imageName: string) { |
| 117 | + if (this._imagesResults.size > 1) { |
| 118 | + const number = /\d+$/.test(imageName) ? +`${/\d+$/.exec(imageName)}` + 1 : `2`; |
| 119 | + imageName = `${imageName}_${number}`; |
| 120 | + } |
| 121 | + |
| 122 | + return imageName; |
| 123 | + } |
| 124 | + |
| 125 | + private extendOptions(options: IImageCompareOptions) { |
| 126 | + options = options || {}; |
| 127 | + Object.getOwnPropertyNames(this.options).forEach(prop => { |
| 128 | + if (!options[prop]) { |
| 129 | + options[prop] = this.options[prop]; |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + return options; |
24 | 134 | }
|
25 | 135 |
|
26 | 136 | get imageCropRect(): IRectangle {
|
|
0 commit comments