Skip to content

Commit 5721a69

Browse files
authored
Merge branch 'master' into reporter
2 parents 7173ede + f38d152 commit 5721a69

File tree

4 files changed

+168
-10
lines changed

4 files changed

+168
-10
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,6 @@ typings/
5959

6060
# next.js build output
6161
.next
62+
63+
#webstorm
64+
.idea

README.md

+35-3
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ It is exactly same as `seeVisualDiff` function, only an additional `selector` CS
7272
Third one is the `screenshotElement` which basically takes screenshot of the element. Selector for the element must be provided.
7373
It saves the image in the output directory as mentioned in the config folder.
7474
This method only works with puppeteer.
75-
```
75+
```js
7676
I.screenshotElement("selectorForElement", "nameForImage");
7777
```
7878

7979
Finally to use the helper in your test, you can write something like this:
8080

81-
```
81+
```js
8282
Feature('to verify monitoried Remote Db instances');
8383

8484
Scenario('Open the System Overview Dashboard', async (I, adminPage, loginPage) => {
@@ -102,6 +102,7 @@ Scenario('Compare CPU Usage Images', async (I) => {
102102
});
103103
```
104104

105+
### Allure Reporter
105106
Allure reports may also be generated directly from the tool. To do so, add
106107
```
107108
"plugins": {
@@ -110,4 +111,35 @@ Allure reports may also be generated directly from the tool. To do so, add
110111
```
111112
in the config file.
112113
The attachments will be added to the report only when the calulated mismatch is greater than the given tolerance.
113-
Set `output` to where the generated report is to be stored. Default is the output directory of the project.
114+
Set `output` to where the generated report is to be stored. Default is the output directory of the project.
115+
116+
### AWS Support
117+
AWS S3 support to upload and download various images is also provided.
118+
It can be used by adding the *aws* code inside `"ResembleHelper"` in the `"helpers"` section in config file. The final result should look like:
119+
```json
120+
{
121+
"helpers": {
122+
"ResembleHelper" : {
123+
"require": "codeceptjs-resemblehelper",
124+
"screenshotFolder" : "<location of output folder>",
125+
"baseFolder": "<location of base folder>",
126+
"diffFolder": "<location of diff folder>",
127+
"aws": {
128+
"accessKeyId" : "<Your AccessKeyId>",
129+
"secretAccessKey": "<Your secretAccessKey>",
130+
"region": "<Region of Bucket>",
131+
"bucketName": "<Bucket Name>"
132+
}
133+
}
134+
}
135+
}
136+
```
137+
When this option has been provided, the helper will download the base image from the S3 bucket.
138+
This base image has to be located inside a folder named "*base*".
139+
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.
140+
If the `prepareBaseImage` option is marked `true`, then the generated base image will be uploaded to a folder named "*base*" in the S3 bucket.
141+
>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.
142+
143+
### Known Issues:
144+
145+
> 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

+128-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const resemble = require("resemblejs");
22
const fs = require('fs');
33
const assert = require('assert');
44
const mkdirp = require('mkdirp');
5-
const path = require('path');
6-
const getDirName = path.dirname;
5+
const getDirName = require('path').dirname;
6+
const AWS = require('aws-sdk');
77

88
/**
99
* Resemble.js helper class for CodeceptJS, this allows screen comparison
@@ -104,11 +104,111 @@ class ResembleHelper extends Helper {
104104
const allure = codeceptjs.container.plugins('allure');
105105
const diffImage = "Diff_" + baseImage.split(".")[0] + ".png";
106106

107-
if(allure && misMatch >= tolerance) {
108-
allure.addAttachment('Base Image', fs.readFileSync(path.join(this.config.baseFolder, baseImage)), 'image/png');
109-
allure.addAttachment('Screenshot Image', fs.readFileSync(path.join(this.config.screenshotFolder, baseImage)), 'image/png');
110-
allure.addAttachment('Diff Image', fs.readFileSync(path.join(this.config.diffFolder, diffImage)), 'image/png');
107+
if(allure !== undefined && misMatch >= tolerance) {
108+
allure.addAttachment('Base Image', fs.readFileSync(this.config.baseFolder + baseImage), 'image/png');
109+
allure.addAttachment('Screenshot Image', fs.readFileSync(this.config.screenshotFolder + baseImage), 'image/png');
110+
allure.addAttachment('Diff Image', fs.readFileSync(this.config.diffFolder + diffImage), 'image/png');
111111
}
112+
}
113+
114+
/**
115+
* This method uploads the diff and screenshot images into the bucket with diff image under bucketName/diff/diffImage and the screenshot image as
116+
* bucketName/output/ssImage
117+
* @param accessKeyId
118+
* @param secretAccessKey
119+
* @param region
120+
* @param bucketName
121+
* @param baseImage
122+
* @param ifBaseImage - tells if the prepareBaseImage is true or false. If false, then it won't upload the baseImage.
123+
* @returns {Promise<void>}
124+
*/
125+
126+
async _upload(accessKeyId, secretAccessKey, region, bucketName, baseImage, ifBaseImage) {
127+
console.log("Starting Upload... ");
128+
const s3 = new AWS.S3({
129+
accessKeyId: accessKeyId,
130+
secretAccessKey: secretAccessKey,
131+
region: region
132+
});
133+
fs.readFile(this.config.screenshotFolder + baseImage, (err, data) => {
134+
if(err) throw err;
135+
let base64data = new Buffer(data, 'binary');
136+
const params = {
137+
Bucket: bucketName,
138+
Key: `output/${baseImage}`,
139+
Body: base64data
140+
};
141+
s3.upload(params, (uerr, data) => {
142+
if(uerr) throw uerr;
143+
console.log(`Screenshot Image uploaded successfully at ${data.Location}`);
144+
});
145+
});
146+
fs.readFile(this.config.diffFolder + "Diff_" + baseImage, (err, data) => {
147+
if(err) console.log("Diff image not generated");
148+
else {
149+
let base64data = new Buffer(data, 'binary');
150+
const params = {
151+
Bucket: bucketName,
152+
Key: `diff/Diff_${baseImage}`,
153+
Body: base64data
154+
};
155+
s3.upload(params, (uerr, data) => {
156+
if(uerr) throw uerr;
157+
console.log(`Diff Image uploaded successfully at ${data.Location}`)
158+
});
159+
}
160+
});
161+
if(ifBaseImage) {
162+
fs.readFile(this.config.baseFolder + baseImage, (err, data) => {
163+
if(err) throw err;
164+
else {
165+
let base64data = new Buffer(data, 'binary');
166+
const params = {
167+
Bucket: bucketName,
168+
Key: `base/${baseImage}`,
169+
Body: base64data
170+
};
171+
s3.upload(params, (uerr, data) => {
172+
if(uerr) throw uerr;
173+
console.log(`Base Image uploaded at ${data.Location}`)
174+
});
175+
}
176+
});
177+
}
178+
else {
179+
console.log("Not Uploading base Image");
180+
}
181+
}
182+
183+
/**
184+
* This method downloads base images from specified bucket into the base folder as mentioned in config file.
185+
* @param accessKeyId
186+
* @param secretAccessKey
187+
* @param region
188+
* @param bucketName
189+
* @param baseImage
190+
* @returns {Promise<void>}
191+
*/
192+
193+
_download(accessKeyId, secretAccessKey, region, bucketName, baseImage) {
194+
console.log("Starting Download...");
195+
const s3 = new AWS.S3({
196+
accessKeyId: accessKeyId,
197+
secretAccessKey: secretAccessKey,
198+
region: region
199+
});
200+
const params = {
201+
Bucket: bucketName,
202+
Key: `base/${baseImage}`
203+
};
204+
return new Promise((resolve, reject) => {
205+
s3.getObject(params, (err, data) => {
206+
if(err) console.error(err);
207+
console.log(this.config.baseFolder + baseImage);
208+
fs.writeFileSync(this.config.baseFolder + baseImage, data.Body);
209+
resolve("File Downloaded Successfully");
210+
});
211+
});
112212
}
113213

114214
/**
@@ -123,6 +223,12 @@ class ResembleHelper extends Helper {
123223
options.tolerance = 0;
124224
}
125225

226+
const awsC = this.config.aws;
227+
228+
if (awsC !== undefined && options.prepareBaseImage === false) {
229+
await this._download(awsC.accessKeyId, awsC.secretAccessKey, awsC.region, awsC.bucketName, baseImage);
230+
}
231+
126232
if (options.prepareBaseImage !== undefined && options.prepareBaseImage) {
127233
await this._prepareBaseImage(baseImage);
128234
}
@@ -131,6 +237,11 @@ class ResembleHelper extends Helper {
131237

132238
this._addAttachment(baseImage, misMatch, options.tolerance);
133239

240+
if(awsC !== undefined) {
241+
let ifUpload = options.prepareBaseImage === false ? false : true;
242+
await this._upload(awsC.accessKeyId, awsC.secretAccessKey, awsC.region, awsC.bucketName, baseImage, ifUpload)
243+
}
244+
134245
this.debug("MisMatch Percentage Calculated is " + misMatch);
135246
assert(misMatch <= options.tolerance, "MissMatch Percentage " + misMatch);
136247
}
@@ -150,6 +261,12 @@ class ResembleHelper extends Helper {
150261
options.tolerance = 0;
151262
}
152263

264+
const awsC = this.config.aws;
265+
266+
if (awsC !== undefined && options.prepareBaseImage === false) {
267+
await this._download(awsC.accessKeyId, awsC.secretAccessKey, awsC.region, awsC.bucketName, baseImage);
268+
}
269+
153270
if (options.prepareBaseImage !== undefined && options.prepareBaseImage) {
154271
await this._prepareBaseImage(baseImage);
155272
}
@@ -158,6 +275,11 @@ class ResembleHelper extends Helper {
158275
const misMatch = await this._fetchMisMatchPercentage(baseImage, options);
159276

160277
this._addAttachment(baseImage, misMatch, options.tolerance);
278+
279+
if(awsC !== undefined) {
280+
let ifUpload = options.prepareBaseImage === false ? false : true;
281+
await this._upload(awsC.accessKeyId, awsC.secretAccessKey, awsC.region, awsC.bucketName, baseImage, ifUpload)
282+
}
161283

162284
this.debug("MisMatch Percentage Calculated is " + misMatch);
163285
assert(misMatch <= options.tolerance, "MissMatch Percentage " + misMatch);

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"mz": "^2.7.0",
1313
"resemblejs": "^3.0.0",
1414
"mkdirp": "^0.5.1",
15-
"path": "^0.12.7"
15+
"path": "^0.12.7",
16+
"aws-sdk": "^2.476.0"
1617
},
1718
"keywords": [
1819
"codeceptJS",

0 commit comments

Comments
 (0)