Skip to content

Commit 99b6845

Browse files
puneet0191jonataskJonatas Kirschguiyomhyankydoo
authored
Prepare 1.9.1 release (#68)
* Issue #60 - Prepare all baselines of test by setting parameter in config (#65) Co-authored-by: Jonatas Kirsch <[email protected]> * Update Dependecies, Fix Fatal Error with mkdirp and Mac (#66) * Update Dependencies * Fix Error on Mac * Avoid failing the test for a given threshold but yet generating the difference image (#63) * Option bypassFailure allowing the user to avoid failing the test for a given threshold but yet generating the difference image * Renamed option to skipFailure Co-authored-by: Jonatas Kirsch <[email protected]> * feat(testcafe): add the support for testcafe (#62) * passing through output settings to resemble.js (#59) Co-authored-by: JANK Michael <[email protected]> * Issue 48 - Add custom assert message (#64) Co-authored-by: Jonatas Kirsch <[email protected]> Co-authored-by: Jonatas Kirsch <[email protected]> Co-authored-by: Jonatas Kirsch <[email protected]> Co-authored-by: Guillaume Camus <[email protected]> Co-authored-by: yankydoo <[email protected]> Co-authored-by: JANK Michael <[email protected]>
1 parent f2eb5b2 commit 99b6845

File tree

3 files changed

+87
-33
lines changed

3 files changed

+87
-33
lines changed

Diff for: README.md

+36-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# codeceptjs-resemblehelper
2-
Helper for resemble.js, used for image comparison in tests with WebdriverIO.
2+
Helper for resemble.js, used for image comparison in tests with WebdriverIO or Puppeteer.
33

44
codeceptjs-resemblehelper is a [CodeceptJS](https://codecept.io/) helper which can be used to compare screenshots and make the tests fail/pass based on the tolerance allowed.
55

@@ -21,19 +21,25 @@ Example:
2121
"ResembleHelper" : {
2222
"require": "codeceptjs-resemblehelper",
2323
"baseFolder": "./tests/screenshots/base/",
24-
"diffFolder": "./tests/screenshots/diff/"
24+
"diffFolder": "./tests/screenshots/diff/",
25+
"prepareBaseImage": true
2526
}
2627
}
2728
}
2829
```
2930

30-
To use the Helper, users must provide the two parameters:
31+
To use the Helper, users may provide the parameters:
3132

32-
`baseFolder`: This is the folder for base images, which will be used with screenshot for comparison.
33+
`baseFolder`: Mandatory. This is the folder for base images, which will be used with screenshot for comparison.
3334

34-
`diffFolder`: This will the folder where resemble would try to store the difference image, which can be viewed later.
35+
`diffFolder`: Mandatory. This will the folder where resemble would try to store the difference image, which can be viewed later.
3536

36-
Usage, these are major functions that help in visual testing
37+
`prepareBaseImage`: Optional. When `true` then the system replaces all of the baselines related to the test case(s) you ran. This is equivalent of setting the option `prepareBaseImage: true` in all verifications of the test file.
38+
39+
40+
### Usage
41+
42+
These are the major functions that help in visual testing:
3743

3844
First one is the `seeVisualDiff` which basically takes two parameters
3945
1) `baseImage` Name of the base image, this will be the image used for comparison with the screenshot image. It is mandatory to have the same image file names for base and screenshot image.
@@ -102,7 +108,7 @@ Scenario('Compare CPU Usage Images', async (I) => {
102108
### Ignored Box
103109
You can also exclude part of the image from comparison, by specifying the excluded area in pixels from the top left.
104110
Just declare an object and pass it in options as `ignoredBox`:
105-
```
111+
```js
106112
const box = {
107113
left: 0,
108114
top: 10,
@@ -115,6 +121,29 @@ I.seeVisualDiff("image.png", {prepareBaseImage: true, tolerance: 1, ignoredBox:
115121
After this, that specific mentioned part will be ignored while comparison.
116122
This works for `seeVisualDiff` and `seeVisualDiffForElement`.
117123

124+
### resemble.js Output Settings
125+
You can set further output settings used by resemble.js. Declare an object specifying them and pass it in the options as `outputSettings`:
126+
127+
```js
128+
const outputSettings = {
129+
ignoreAreasColoredWith: {r: 250, g: 250, b: 250, a: 0},
130+
// read more here: https://github.com/rsmbl/Resemble.js
131+
};
132+
I.seeVisualDiff("image.png", {prepareBaseImage: true, tolerance: 1, outputSettings: outputSettings});
133+
```
134+
135+
Refer to the [resemble.js](https://github.com/rsmbl/Resemble.js) documentation for available output settings.
136+
137+
### Skip Failure
138+
You can avoid the test fails for a given threshold but yet generates the difference image.
139+
Just declare an object and pass it in options as `skipFailure`:
140+
```
141+
I.seeVisualDiff("image.png", {prepareBaseImage: true, tolerance: 1, skipFailure: true});
142+
```
143+
After this, the system generates the difference image but does not fail the test.
144+
This works for `seeVisualDiff` and `seeVisualDiffForElement`.
145+
146+
118147
### Allure Reporter
119148
Allure reports may also be generated directly from the tool. To do so, add
120149

Diff for: index.js

+44-19
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ResembleHelper extends Helper {
1919
this.baseFolder = this.resolvePath(config.baseFolder);
2020
this.diffFolder = this.resolvePath(config.diffFolder);
2121
this.screenshotFolder = global.output_dir + "/";
22+
this.prepareBaseImage = config.prepareBaseImage;
2223
}
2324

2425
resolvePath(folderPath) {
@@ -57,9 +58,13 @@ class ResembleHelper extends Helper {
5758

5859
return new Promise((resolve, reject) => {
5960

61+
if (!options.outputSettings) {
62+
options.outputSettings = {};
63+
}
6064
resemble.outputSettings({
6165
boundingBox: options.boundingBox,
62-
ignoredBox: options.ignoredBox
66+
ignoredBox: options.ignoredBox,
67+
...options.outputSettings,
6368
});
6469

6570
this.debug("Tolerance Level Provided " + options.tolerance);
@@ -76,9 +81,9 @@ class ResembleHelper extends Helper {
7681
}
7782
resolve(data);
7883
if (data.misMatchPercentage >= tolerance) {
79-
mkdirp(getDirName(this.diffFolder + diffImage), function (error) {
80-
if (error) return cb(error);
81-
});
84+
if (!fs.existsSync(getDirName(this.diffFolder + diffImage))) {
85+
fs.mkdirSync(getDirName(this.diffFolder + diffImage));
86+
}
8287
fs.writeFileSync(this.diffFolder + diffImage + '.png', data.getBuffer());
8388
const diffImagePath = path.join(process.cwd(), this.diffFolder + diffImage + '.png');
8489
this.debug("Diff Image File Saved to: " + diffImagePath);
@@ -125,7 +130,14 @@ class ResembleHelper extends Helper {
125130
const el = els[0];
126131

127132
await el.saveScreenshot(this.screenshotFolder + name + '.png');
128-
} else throw new Error("Method only works with Puppeteer and WebDriver helpers.");
133+
} else if (this.helpers['TestCafe']) {
134+
await helper.waitForVisible(selector);
135+
const els = await helper._locate(selector);
136+
if (!await els.count) throw new Error(`Element ${selector} couldn't be located`);
137+
const { t } = this.helpers['TestCafe'];
138+
139+
await t.takeElementScreenshot(els, name);
140+
} else throw new Error("Method only works with Puppeteer, WebDriver or TestCafe helpers.");
129141
}
130142

131143
/**
@@ -177,7 +189,7 @@ class ResembleHelper extends Helper {
177189
* @param region
178190
* @param bucketName
179191
* @param baseImage
180-
* @param ifBaseImage - tells if the prepareBaseImage is true or false. If false, then it won't upload the baseImage.
192+
* @param ifBaseImage - tells if the prepareBaseImage is true or false. If false, then it won't upload the baseImage. However, this parameter is not considered if the config file has a prepareBaseImage set to true.
181193
* @returns {Promise<void>}
182194
*/
183195

@@ -296,32 +308,32 @@ class ResembleHelper extends Helper {
296308
options.tolerance = 0;
297309
}
298310

299-
const awsC = this.config.aws;
311+
if (this.prepareBaseImage) {
312+
options.prepareBaseImage = true;
313+
}
300314

315+
const awsC = this.config.aws;
301316
if (awsC !== undefined && options.prepareBaseImage === false) {
302317
await this._download(awsC.accessKeyId, awsC.secretAccessKey, awsC.region, awsC.bucketName, baseImage);
303318
}
304-
305319
if (options.prepareBaseImage !== undefined && options.prepareBaseImage) {
306320
await this._prepareBaseImage(baseImage);
307321
}
308-
309322
if (selector) {
310323
options.boundingBox = await this._getBoundingBox(selector);
311324
}
312-
313325
const misMatch = await this._fetchMisMatchPercentage(baseImage, options);
314-
315326
this._addAttachment(baseImage, misMatch, options.tolerance);
316-
317327
this._addMochaContext(baseImage, misMatch, options.tolerance);
318-
319328
if (awsC !== undefined) {
320329
await this._upload(awsC.accessKeyId, awsC.secretAccessKey, awsC.region, awsC.bucketName, baseImage, options.prepareBaseImage)
321330
}
322331

323-
this.debug("MisMatch Percentage Calculated is " + misMatch);
324-
assert(misMatch <= options.tolerance, "MissMatch Percentage " + misMatch);
332+
this.debug("MisMatch Percentage Calculated is " + misMatch + " for baseline " + baseImage);
333+
334+
if (options.skipFailure === false) {
335+
assert(misMatch <= options.tolerance, "Screenshot does not match with the baseline " + baseImage + " when MissMatch Percentage is " + misMatch);
336+
}
325337
}
326338

327339
/**
@@ -369,17 +381,24 @@ class ResembleHelper extends Helper {
369381
const helper = this._getHelper();
370382
await helper.waitForVisible(selector);
371383
const els = await helper._locate(selector);
372-
if (!els.length) throw new Error(`Element ${selector} couldn't be located`);
373-
const el = els[0];
384+
385+
if (this.helpers['TestCafe']) {
386+
if (await els.count != 1) throw new Error(`Element ${selector} couldn't be located or isn't unique on the page`);
387+
}
388+
else {
389+
if (!els.length) throw new Error(`Element ${selector} couldn't be located`);
390+
}
374391

375392
let location, size;
376393

377394
if (this.helpers['Puppeteer']) {
395+
const el = els[0];
378396
const box = await el.boundingBox();
379397
size = location = box;
380398
}
381399

382400
if (this.helpers['WebDriver'] || this.helpers['Appium']) {
401+
const el = els[0];
383402
location = await el.getLocation();
384403
size = await el.getSize();
385404
}
@@ -388,6 +407,9 @@ class ResembleHelper extends Helper {
388407
location = await helper.browser.getLocation(selector);
389408
size = await helper.browser.getElementSize(selector);
390409
}
410+
if (this.helpers['TestCafe']) {
411+
return await els.boundingClientRect;
412+
}
391413

392414
if (!size) {
393415
throw new Error("Cannot get element size!");
@@ -421,9 +443,12 @@ class ResembleHelper extends Helper {
421443
if (this.helpers['WebDriverIO']) {
422444
return this.helpers['WebDriverIO'];
423445
}
446+
if (this.helpers['TestCafe']) {
447+
return this.helpers['TestCafe'];
448+
}
424449

425-
throw new Error('No matching helper found. Supported helpers: WebDriver/Appium/Puppeteer');
450+
throw new Error('No matching helper found. Supported helpers: WebDriver/Appium/Puppeteer/TestCafe');
426451
}
427452
}
428453

429-
module.exports = ResembleHelper;
454+
module.exports = ResembleHelper;

Diff for: package.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
{
22
"name": "codeceptjs-resemblehelper",
3-
"version": "1.9.0",
3+
"version": "1.9.1",
44
"description": "Resemble Js helper for CodeceptJS, with Support for Webdriver, Puppeteer & Appium",
55
"repository": {
66
"type": "git",
77
"url": "[email protected]:Percona-Lab/codeceptjs-resemblehelper.git"
88
},
99
"dependencies": {
10-
"assert": "^1.4.1",
11-
"canvas": "^2.2.0",
10+
"assert": "^1.5.0",
11+
"canvas": "^2.6.1",
1212
"mz": "^2.7.0",
13-
"resemblejs": "^3.0.0",
14-
"mkdirp": "^0.5.1",
13+
"resemblejs": "^3.2.4",
14+
"mkdirp": "^1.0.4",
1515
"path": "^0.12.7",
16-
"aws-sdk": "^2.476.0",
17-
"image-size": "^0.7.4"
16+
"aws-sdk": "^2.662.0",
17+
"image-size": "^0.8.3"
1818
},
1919
"devDependencies": {
2020
"allure-commandline": "^2.13.0",

0 commit comments

Comments
 (0)