@@ -3,6 +3,7 @@ const fs = require('fs');
3
3
const assert = require ( 'assert' ) ;
4
4
const mkdirp = require ( 'mkdirp' ) ;
5
5
const getDirName = require ( 'path' ) . dirname ;
6
+ const AWS = require ( 'aws-sdk' ) ;
6
7
7
8
/**
8
9
* Resemble.js helper class for CodeceptJS, this allows screen comparison
@@ -91,6 +92,106 @@ class ResembleHelper extends Helper {
91
92
else throw new Error ( "Method only works with Puppeteer" ) ;
92
93
}
93
94
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
+
94
195
/**
95
196
* Check Visual Difference for Base and Screenshot Image
96
197
* @param baseImage Name of the Base Image (Base Image path is taken from Configuration)
@@ -103,11 +204,23 @@ class ResembleHelper extends Helper {
103
204
options . tolerance = 0 ;
104
205
}
105
206
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
+
106
213
if ( options . prepareBaseImage !== undefined && options . prepareBaseImage ) {
107
214
await this . _prepareBaseImage ( baseImage ) ;
108
215
}
109
216
110
217
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
+
111
224
this . debug ( "MisMatch Percentage Calculated is " + misMatch ) ;
112
225
assert ( misMatch <= options . tolerance , "MissMatch Percentage " + misMatch ) ;
113
226
}
@@ -127,12 +240,24 @@ class ResembleHelper extends Helper {
127
240
options . tolerance = 0 ;
128
241
}
129
242
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
+
130
249
if ( options . prepareBaseImage !== undefined && options . prepareBaseImage ) {
131
250
await this . _prepareBaseImage ( baseImage ) ;
132
251
}
133
252
134
253
options . boundingBox = await this . _getBoundingBox ( selector ) ;
135
254
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
+
136
261
this . debug ( "MisMatch Percentage Calculated is " + misMatch ) ;
137
262
assert ( misMatch <= options . tolerance , "MissMatch Percentage " + misMatch ) ;
138
263
}
0 commit comments