Skip to content

Commit 68a656b

Browse files
authored
Merge branch 'master' into screenshotFolder
2 parents 7bb0c3b + 5181f41 commit 68a656b

File tree

3 files changed

+54
-30
lines changed

3 files changed

+54
-30
lines changed

README.md

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
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.
33

4-
codeceptjs-resemblehelper is [CodeceptJS](https://codecept.io/) helper which can be used to compare screenshots and make the tests fail/pass based on the tolerance allowed
4+
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

66
If two screenshot comparisons have difference greater then the tolerance provided, the test will fail.
77

88
NPM package: https://www.npmjs.com/package/codeceptjs-resemblehelper
99

10-
To install the package, just run `npm install codeceptjs-resemblehelper`
10+
To install the package, just run `npm install codeceptjs-resemblehelper`.
1111

1212
### Configuration
1313

@@ -26,18 +26,18 @@ Example:
2626
}
2727
}
2828
```
29+
2930
To use the Helper, users must provide the two parameters:
3031

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

3334
`diffFolder`: This will the folder where resemble would try to store the difference image, which can be viewed later.
3435

3536
Usage, these are major functions that help in visual testing
3637

3738
First one is the `seeVisualDiff` which basically takes two parameters
38-
1) `baseImage` Name of the base image, this will be the image used for comparison with the screenshot image,
39-
it is mandatory to have the same image file names for base and screenshot image
40-
2) `options` options can be passed which include `prepaseBaseImage` and `tolerance`
39+
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.
40+
2) `options` options can be passed which include `prepaseBaseImage` and `tolerance`.
4141

4242
```js
4343
/**
@@ -48,7 +48,7 @@ it is mandatory to have the same image file names for base and screenshot image
4848
*/
4949
async seeVisualDiff(baseImage, options) {}
5050
```
51-
Second one is the `seeVisualDiffForElement` which basically compares elements on the screenshot, Selector for element must be provided
51+
Second one is the `seeVisualDiffForElement` which basically compares elements on the screenshot, selector for element must be provided.
5252

5353
It is exactly same as `seeVisualDiff` function, only an additional `selector` CSS|XPath|ID locators is provided
5454
```js
@@ -63,14 +63,14 @@ It is exactly same as `seeVisualDiff` function, only an additional `selector` CS
6363
async seeVisualDiffForElement(selector, baseImage, options){}
6464
```
6565
> Note:
66-
`seeVisualDiffForElement` only works when the page for baseImage is open in the browser, so that webdriver can fetch coordinates of the provided selector
66+
`seeVisualDiffForElement` only works when the page for baseImage is open in the browser, so that WebdriverIO can fetch coordinates of the provided selector.
67+
68+
Third one is the `screenshotElement` which basically takes screenshot of the element. Selector for the element must be provided. It saves the image in the output directory as mentioned in the config folder.
6769

68-
Third one is the `screenshotElement` which basically takes screenshot of the element. Selector for the element must be provided.
69-
It saves the image in the output directory as mentioned in the config folder.
7070
```js
7171
I.screenshotElement("selectorForElement", "nameForImage");
7272
```
73-
>Note: This method only works with puppeteer.
73+
> Note: This method only works with puppeteer.
7474
7575
Finally to use the helper in your test, you can write something like this:
7676

@@ -97,7 +97,7 @@ Scenario('Compare CPU Usage Images', async (I) => {
9797
I.seeVisualDiffForElement("//div[@class='panel-container']", "Complete_Dashboard_Image.png", {prepareBaseImage: false, tolerance: 3});
9898
});
9999
```
100-
>Note: `seeVisualDiff` and `seeVisualDiffElement` work only when the dimensions of the screenshot as well as the base image are same so as to avoid unexpected results.
100+
> Note: `seeVisualDiff` and `seeVisualDiffElement` work only when the dimensions of the screenshot as well as the base image are same so as to avoid unexpected results.
101101
102102
### Ignored Box
103103
You can also exclude part of the image from comparison, by specifying the excluded area in pixels from the top left.
@@ -113,15 +113,17 @@ const box = {
113113
I.seeVisualDiff("image.png", {prepareBaseImage: true, tolerance: 1, ignoredBox: box});
114114
```
115115
After this, that specific mentioned part will be ignored while comparison.
116-
This works with both `seeVisualDiff` and `seeVisualDiffForElement`.
116+
This works for `seeVisualDiff` and `seeVisualDiffForElement`.
117117

118118
### Allure Reporter
119119
Allure reports may also be generated directly from the tool. To do so, add
120+
120121
```
121122
"plugins": {
122123
"allure": {}
123124
}
124125
```
126+
125127
in the config file.
126128
The attachments will be added to the report only when the calulated mismatch is greater than the given tolerance.
127129
Set `output` to where the generated report is to be stored. Default is the output directory of the project.
@@ -150,8 +152,8 @@ When this option has been provided, the helper will download the base image from
150152
This base image has to be located inside a folder named "*base*".
151153
The resultant output image will be uploaded in a folder named "*output*" and diff image will be uploaded to a folder named "*diff*" in the S3 bucket.
152154
If the `prepareBaseImage` option is marked `true`, then the generated base image will be uploaded to a folder named "*base*" in the S3 bucket.
153-
>Note: The tests may take a bit longer to run when the AWS configuration is provided as determined by the internet speed to upload/download images.
155+
> Note: The tests may take a bit longer to run when the AWS configuration is provided as determined by the internet speed to upload/download images.
154156
155157
### Known Issues:
156158

157-
> Issue in Windows where the image comparison is not carried out, and therefore no Mismatch Percentage is shown. See 'loadImageData' function in resemble.js
159+
> Issue in Windows where the image comparison is not carried out, and therefore no Mismatch Percentage is shown. See 'loadImageData' function in resemble.js

index.js

+35-13
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,29 @@ class ResembleHelper extends Helper {
1616
/**
1717
* Compare Images
1818
*
19-
* @param image1
20-
* @param image2
19+
* @param image
2120
* @param diffImage
2221
* @param options
2322
* @returns {Promise<any | never>}
2423
*/
25-
async _compareImages(image1, image2, diffImage, options) {
26-
image1 = this.config.baseFolder + image1;
27-
image2 = this.config.screenshotFolder + image2;
24+
async _compareImages(image, diffImage, options) {
25+
const image1 = this.config.baseFolder + image;
26+
const image2 = this.config.screenshotFolder + image;
27+
28+
// check whether the base and the screenshot images are present.
29+
fs.access(image1, fs.constants.F_OK | fs.constants.W_OK, (err) => {
30+
if (err) {
31+
throw new Error(
32+
`${image1} ${err.code === 'ENOENT' ? 'base image does not exist' : 'is read-only'}`);
33+
}
34+
});
35+
36+
fs.access(image2, fs.constants.F_OK | fs.constants.W_OK, (err) => {
37+
if (err) {
38+
throw new Error(
39+
`${image2} ${err.code === 'ENOENT' ? 'screenshot image does not exist' : 'is read-only'}`);
40+
}
41+
});
2842

2943
return new Promise((resolve, reject) => {
3044

@@ -40,7 +54,7 @@ class ResembleHelper extends Helper {
4054
if (err) {
4155
reject(err);
4256
} else {
43-
if(!data.isSameDimensions) throw new Error("The images are not of same dimensions. Please use images of same dimensions so as to avoid any unexpected results.")
57+
if(!data.isSameDimensions) reject(new Error("The images are not of same dimensions. Please use images of same dimensions so as to avoid any unexpected results."));
4458
resolve(data);
4559
if (data.misMatchPercentage >= tolerance) {
4660
mkdirp(getDirName(this.config.diffFolder + diffImage), function (err) {
@@ -63,14 +77,13 @@ class ResembleHelper extends Helper {
6377

6478
/**
6579
*
66-
* @param image1
80+
* @param image
6781
* @param options
6882
* @returns {Promise<*>}
6983
*/
70-
async _fetchMisMatchPercentage(image1, options) {
71-
const image2 = image1;
72-
const diffImage = "Diff_" + image1.split(".")[0];
73-
const result = this._compareImages(image1, image2, diffImage, options);
84+
async _fetchMisMatchPercentage(image, options) {
85+
const diffImage = "Diff_" + image.split(".")[0];
86+
const result = this._compareImages(image, diffImage, options);
7487
const data = await Promise.resolve(result);
7588
return data.misMatchPercentage;
7689
}
@@ -92,8 +105,17 @@ class ResembleHelper extends Helper {
92105
await el.screenshot({
93106
path: global.output_dir + "/" + name + '.png'
94107
});
95-
}
96-
else throw new Error("Method only works with Puppeteer");
108+
} else if (this.helpers['WebDriver']) {
109+
const configuration = this.config;
110+
111+
await helper.waitForVisible(selector);
112+
const els = await helper._locate(selector);
113+
if (!els.length) throw new Error(`Element ${selector} couldn't be located`);
114+
const el = els[0];
115+
116+
await el.saveScreenshot(configuration.screenshotFolder + name + '.png');
117+
}
118+
else throw new Error("Method only works with Puppeteer and WebDriver helpers.");
97119
}
98120

99121
/**

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codeceptjs-resemblehelper",
3-
"version": "1.6.0",
3+
"version": "1.7.0",
44
"description": "Resemble Js helper for CodeceptJS, with Support for Webdriver, Puppeteer & Appium",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)