Skip to content

Commit

Permalink
Naive implementation of using different endpoints in aws-sdk (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
komarov-v authored Jan 2, 2023
1 parent e0fb25d commit 26419e4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,28 @@ The resultant output image will be uploaded in a folder named "*output*" and dif
If the `prepareBaseImage` option is marked `true`, then the generated base image will be uploaded to a folder named "*base*" in the S3 bucket.
> 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.
### Other S3 Providers
The same configuration as above, but with *endpoint* field:

```json
{
"helpers": {
"ResembleHelper" : {
"require": "codeceptjs-resemblehelper",
"baseFolder": "<location of base folder>",
"diffFolder": "<location of diff folder>",
"aws": {
"accessKeyId" : "<Your AccessKeyId>",
"secretAccessKey": "<Your secretAccessKey>",
"region": "<Region of Bucket>",
"bucketName": "<Bucket Name>",
"endpoint": "<Endpoint of Bucket>"
}
}
}
}
```

### Compare with custom image
Usually, every screenshot needs to have the same filename as an existing image inside the `baseFolder` directory. To change this behavior, you can use the `compareWithImage` option and specify a different image inside the `baseFolder` directory.

Expand Down
34 changes: 32 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@ export = ResembleHelper;
* Resemble.js helper class for CodeceptJS, this allows screen comparison
* @author Puneet Kala
*/
declare class Endpoint {
/**
* Constructs a new endpoint given an endpoint URL.
*/
constructor(url: string);

/**
* The host portion of the endpoint including the port, e.g., example.com:80.
*/
host: string;
/**
* The host portion of the endpoint, e.g., example.com.
*/
hostname: string;
/**
* The full URL of the endpoint.
*/
href: string;
/**
* The port of the endpoint.
*/
port: number;
/**
* The protocol (http or https) of the endpoint URL.
*/
protocol: string;
}

declare class ResembleHelper {
constructor(config: any);
baseFolder: any;
Expand Down Expand Up @@ -58,9 +86,10 @@ declare class ResembleHelper {
* @param bucketName
* @param baseImage
* @param options
* @param {string | Endpoint } [endpoint]
* @returns {Promise<void>}
*/
_upload(accessKeyId: any, secretAccessKey: any, region: any, bucketName: any, baseImage: any, options: any): Promise<void>;
_upload(accessKeyId: any, secretAccessKey: any, region: any, bucketName: any, baseImage: any, options: any, endpoint?: string | Endpoint): Promise<void>;
/**
* This method downloads base images from specified bucket into the base folder as mentioned in config file.
* @param accessKeyId
Expand All @@ -69,9 +98,10 @@ declare class ResembleHelper {
* @param bucketName
* @param baseImage
* @param options
* @param {string | Endpoint } [endpoint]
* @returns {Promise<void>}
*/
_download(accessKeyId: any, secretAccessKey: any, region: any, bucketName: any, baseImage: any, options: any): Promise<void>;
_download(accessKeyId: any, secretAccessKey: any, region: any, bucketName: any, baseImage: any, options: any, endpoint?: string | Endpoint): Promise<void>;
/**
* Check Visual Difference for Base and Screenshot Image
* @param baseImage Name of the Base Image (Base Image path is taken from Configuration)
Expand Down
16 changes: 10 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,17 @@ class ResembleHelper extends Helper {
* @param bucketName
* @param baseImage
* @param options
* @param {string | Endpoint } [endpoint]
* @returns {Promise<void>}
*/

async _upload(accessKeyId, secretAccessKey, region, bucketName, baseImage, options) {
async _upload(accessKeyId, secretAccessKey, region, bucketName, baseImage, options, endpoint) {
console.log("Starting Upload... ");
const s3 = new AWS.S3({
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
region: region
region: region,
endpoint: endpoint
});
fs.readFile(this._getActualImagePath(baseImage), (err, data) => {
if (err) throw err;
Expand Down Expand Up @@ -279,16 +281,18 @@ class ResembleHelper extends Helper {
* @param bucketName
* @param baseImage
* @param options
* @param {string | Endpoint } [endpoint]
* @returns {Promise<void>}
*/

_download(accessKeyId, secretAccessKey, region, bucketName, baseImage, options) {
_download(accessKeyId, secretAccessKey, region, bucketName, baseImage, options, endpoint) {
console.log("Starting Download...");
const baseImageName = this._getBaseImageName(baseImage, options);
const s3 = new AWS.S3({
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
region: region
region: region,
endpoint: endpoint
});
const params = {
Bucket: bucketName,
Expand Down Expand Up @@ -337,7 +341,7 @@ class ResembleHelper extends Helper {
if (this._getPrepareBaseImage(options)) {
await this._prepareBaseImage(baseImage, options);
} else if (awsC !== undefined) {
await this._download(awsC.accessKeyId, awsC.secretAccessKey, awsC.region, awsC.bucketName, baseImage, options);
await this._download(awsC.accessKeyId, awsC.secretAccessKey, awsC.region, awsC.bucketName, baseImage, options, awsC.endpoint);
}

// BoundingBox for Playwright not necessary
Expand All @@ -348,7 +352,7 @@ class ResembleHelper extends Helper {
await this._addAttachment(baseImage, misMatch, options);
await this._addMochaContext(baseImage, misMatch, options);
if (awsC !== undefined) {
await this._upload(awsC.accessKeyId, awsC.secretAccessKey, awsC.region, awsC.bucketName, baseImage, options)
await this._upload(awsC.accessKeyId, awsC.secretAccessKey, awsC.region, awsC.bucketName, baseImage, options, awsC.endpoint)
}

this.debug("MisMatch Percentage Calculated is " + misMatch + " for baseline " + baseImage);
Expand Down

0 comments on commit 26419e4

Please sign in to comment.