Skip to content

Commit f38d152

Browse files
authored
Merge pull request #28 from Kartikeya99/cloudSupport
Cloud support
2 parents cc0375c + efefbf3 commit f38d152

File tree

4 files changed

+159
-4
lines changed

4 files changed

+159
-4
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

+29-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,32 @@ Scenario('Compare CPU Usage Images', async (I) => {
102102
});
103103
```
104104

105+
AWS S3 support to upload and download various images is also provided.
106+
It can be used by adding the *aws* code inside `"ResembleHelper"` in the `"helpers"` section in config file. The final result should look like:
107+
```json
108+
{
109+
"helpers": {
110+
"ResembleHelper" : {
111+
"require": "codeceptjs-resemblehelper",
112+
"screenshotFolder" : "<location of output folder>",
113+
"baseFolder": "<location of base folder>",
114+
"diffFolder": "<location of diff folder>",
115+
"aws": {
116+
"accessKeyId" : "<Your AccessKeyId>",
117+
"secretAccessKey": "<Your secretAccessKey>",
118+
"region": "<Region of Bucket>",
119+
"bucketName": "<Bucket Name>"
120+
}
121+
}
122+
}
123+
}
124+
```
125+
When this option has been provided, the helper will download the base image from the S3 bucket.
126+
This base image has to be located inside a folder named "*base*".
127+
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.
128+
If the `prepareBaseImage` option is marked `true`, then the generated base image will be uploaded to a folder named "*base*" in the S3 bucket.
129+
>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.
130+
105131
### Known Issues:
106132

107-
> Issue in Windows where the image comparison is not carried out, and therefore no Mismatch Percentage is shown. See 'loadImageData' function in resemble.js
133+
> 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

+125
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const fs = require('fs');
33
const assert = require('assert');
44
const mkdirp = require('mkdirp');
55
const getDirName = require('path').dirname;
6+
const AWS = require('aws-sdk');
67

78
/**
89
* Resemble.js helper class for CodeceptJS, this allows screen comparison
@@ -91,6 +92,106 @@ class ResembleHelper extends Helper {
9192
else throw new Error("Method only works with Puppeteer");
9293
}
9394

95+
/**
96+
* This method uploads the diff and screenshot images into the bucket with diff image under bucketName/diff/diffImage and the screenshot image as
97+
* bucketName/output/ssImage
98+
* @param accessKeyId
99+
* @param secretAccessKey
100+
* @param region
101+
* @param bucketName
102+
* @param baseImage
103+
* @param ifBaseImage - tells if the prepareBaseImage is true or false. If false, then it won't upload the baseImage.
104+
* @returns {Promise<void>}
105+
*/
106+
107+
async _upload(accessKeyId, secretAccessKey, region, bucketName, baseImage, ifBaseImage) {
108+
console.log("Starting Upload... ");
109+
const s3 = new AWS.S3({
110+
accessKeyId: accessKeyId,
111+
secretAccessKey: secretAccessKey,
112+
region: region
113+
});
114+
fs.readFile(this.config.screenshotFolder + baseImage, (err, data) => {
115+
if(err) throw err;
116+
let base64data = new Buffer(data, 'binary');
117+
const params = {
118+
Bucket: bucketName,
119+
Key: `output/${baseImage}`,
120+
Body: base64data
121+
};
122+
s3.upload(params, (uerr, data) => {
123+
if(uerr) throw uerr;
124+
console.log(`Screenshot Image uploaded successfully at ${data.Location}`);
125+
});
126+
});
127+
fs.readFile(this.config.diffFolder + "Diff_" + baseImage, (err, data) => {
128+
if(err) console.log("Diff image not generated");
129+
else {
130+
let base64data = new Buffer(data, 'binary');
131+
const params = {
132+
Bucket: bucketName,
133+
Key: `diff/Diff_${baseImage}`,
134+
Body: base64data
135+
};
136+
s3.upload(params, (uerr, data) => {
137+
if(uerr) throw uerr;
138+
console.log(`Diff Image uploaded successfully at ${data.Location}`)
139+
});
140+
}
141+
});
142+
if(ifBaseImage) {
143+
fs.readFile(this.config.baseFolder + baseImage, (err, data) => {
144+
if(err) throw err;
145+
else {
146+
let base64data = new Buffer(data, 'binary');
147+
const params = {
148+
Bucket: bucketName,
149+
Key: `base/${baseImage}`,
150+
Body: base64data
151+
};
152+
s3.upload(params, (uerr, data) => {
153+
if(uerr) throw uerr;
154+
console.log(`Base Image uploaded at ${data.Location}`)
155+
});
156+
}
157+
});
158+
}
159+
else {
160+
console.log("Not Uploading base Image");
161+
}
162+
}
163+
164+
/**
165+
* This method downloads base images from specified bucket into the base folder as mentioned in config file.
166+
* @param accessKeyId
167+
* @param secretAccessKey
168+
* @param region
169+
* @param bucketName
170+
* @param baseImage
171+
* @returns {Promise<void>}
172+
*/
173+
174+
_download(accessKeyId, secretAccessKey, region, bucketName, baseImage) {
175+
console.log("Starting Download...");
176+
const s3 = new AWS.S3({
177+
accessKeyId: accessKeyId,
178+
secretAccessKey: secretAccessKey,
179+
region: region
180+
});
181+
const params = {
182+
Bucket: bucketName,
183+
Key: `base/${baseImage}`
184+
};
185+
return new Promise((resolve, reject) => {
186+
s3.getObject(params, (err, data) => {
187+
if(err) console.error(err);
188+
console.log(this.config.baseFolder + baseImage);
189+
fs.writeFileSync(this.config.baseFolder + baseImage, data.Body);
190+
resolve("File Downloaded Successfully");
191+
});
192+
});
193+
}
194+
94195
/**
95196
* Check Visual Difference for Base and Screenshot Image
96197
* @param baseImage Name of the Base Image (Base Image path is taken from Configuration)
@@ -103,11 +204,23 @@ class ResembleHelper extends Helper {
103204
options.tolerance = 0;
104205
}
105206

207+
const awsC = this.config.aws;
208+
209+
if (awsC !== undefined && options.prepareBaseImage === false) {
210+
await this._download(awsC.accessKeyId, awsC.secretAccessKey, awsC.region, awsC.bucketName, baseImage);
211+
}
212+
106213
if (options.prepareBaseImage !== undefined && options.prepareBaseImage) {
107214
await this._prepareBaseImage(baseImage);
108215
}
109216

110217
const misMatch = await this._fetchMisMatchPercentage(baseImage, options);
218+
219+
if(awsC !== undefined) {
220+
let ifUpload = options.prepareBaseImage === false ? false : true;
221+
await this._upload(awsC.accessKeyId, awsC.secretAccessKey, awsC.region, awsC.bucketName, baseImage, ifUpload)
222+
}
223+
111224
this.debug("MisMatch Percentage Calculated is " + misMatch);
112225
assert(misMatch <= options.tolerance, "MissMatch Percentage " + misMatch);
113226
}
@@ -127,12 +240,24 @@ class ResembleHelper extends Helper {
127240
options.tolerance = 0;
128241
}
129242

243+
const awsC = this.config.aws;
244+
245+
if (awsC !== undefined && options.prepareBaseImage === false) {
246+
await this._download(awsC.accessKeyId, awsC.secretAccessKey, awsC.region, awsC.bucketName, baseImage);
247+
}
248+
130249
if (options.prepareBaseImage !== undefined && options.prepareBaseImage) {
131250
await this._prepareBaseImage(baseImage);
132251
}
133252

134253
options.boundingBox = await this._getBoundingBox(selector);
135254
const misMatch = await this._fetchMisMatchPercentage(baseImage, options);
255+
256+
if(awsC !== undefined) {
257+
let ifUpload = options.prepareBaseImage === false ? false : true;
258+
await this._upload(awsC.accessKeyId, awsC.secretAccessKey, awsC.region, awsC.bucketName, baseImage, ifUpload)
259+
}
260+
136261
this.debug("MisMatch Percentage Calculated is " + misMatch);
137262
assert(misMatch <= options.tolerance, "MissMatch Percentage " + misMatch);
138263
}

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)