Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remote hosted screenshot testing #12

Merged
merged 11 commits into from
Mar 30, 2018
Merged

Conversation

patrickrodee
Copy link
Contributor

Remove repo screenshots in favor of Google Cloud Storage-hosted images. Add golden.json file to keep track of the golden image per tested URL. Increase line length to 120. Remove image upload step from Travis CI.

Copy link

@acdvorak acdvorak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really good overall!

{
"temporary-package/index.html": "57de91e42bd22a845074bae80f71eca3902ab7c6719f129960bc9018c79db95a",
"temporary-package/foo.html": "91f0795700eaba345092dd7507e046daa0d33e1136bad7992011ab454ddb1faf"
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Add trailing newline to end of file (and probably update your editor config to do this automatically)

package.json Outdated
@@ -5,7 +5,7 @@
"scripts": {
"start": "webpack-dev-server --config test/screenshot/webpack.config.js --content-base test/screenshot",
"stop": "./test/screenshot/stop.sh",
"capture": "./node_modules/.bin/mocha --compilers js:babel-core/register --ui tdd test/screenshot/capture-suite.js",
"capture": "COMMIT_HASH=$(git rev-parse --short HEAD) BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) mocha --compilers js:babel-core/register --ui tdd --timeout 30000 test/screenshot/capture-suite.js",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want this to run on Windows, we'll need to use cross-env (see MDC Web's package.json for examples).

Also, keep in mind that there may be unstaged changes in the working directory that won't be reflected in COMMIT_HASH. This may end up biting us down the road if we forget that fact and assume that screenshots with a particular commit hash are from that exact commit with no other changes.


import comparisonOptions from './screenshot-comparison-options';

const readFilePromise = promisify(readFile);
const writeFilePromise = promisify(writeFile);

const SERVICE_ACCOUNT_KEY = process.env.SERVICE_ACCOUNT_KEY;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest prefixing all of our env vars with MDC_. This makes it easier to tell which part of the stack is using the var, and makes it easier to search/replace.

Also, we might want to rename this var to indicate that it's used by GCP.

E.g., MDC_GCP_SERVICE_ACCOUNT_KEY or MDC_GCLOUD_SERVICE_ACCOUNT_KEY.

credentials = JSON.parse(SERVICE_ACCOUNT_KEY);
} catch (err) {
console.error('Service account key could not be parsed.');
process.exit(1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break any future unit tests. Maybe just rethrow the error instead?

* @private
*/
generateImageID_(imageBuffer) {
return createHash('sha256').update(imageBuffer).digest('hex');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not sha512?

Changing your hashing algorithm in the future is painful (just ask git or npm), so we should pick the "right" one from the start.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason, I just chose the first one that came to mind


case SNAPSHOT:
case DIFF:
return `${this.urlPath_}/${COMMIT_HASH}/${imageId}.${imageType}.png`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Indent

* Saves the given image to Google Cloud Storage with optional metadata
* @param {string} imagePath The path to the image
* @param {!Buffer} imageBuffer The image buffer
* @param {Object} customMetadata Optional custom metadata

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: @param {!Object=}

const url = `http://localhost:8080/${this.urlPath_}`;
const imagePath = `./test/screenshot/${this.imagePath_}`;
await this.createScreenshotTask_(url, imagePath);
const golden = await this.createScreenshotTask_();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to goldenTask or screenshotTask?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could rename the method to takeScreenshot_ since it returns a Buffer, not a task.

}

/**
* Captures a screenshot of the test URL

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Captures a screenshot of the test URL and marks it as the new golden image


/**
* Captures a screenshot of the test URL
*/
capture() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name capture is misleading. It would be clearer to name it something like updateGolden.


import comparisonOptions from './screenshot-comparison-options';

const readFilePromise = promisify(readFile);
const writeFilePromise = promisify(writeFile);

const SERVICE_ACCOUNT_KEY = process.env.MDC_GCLOUD_SERVICE_ACCOUNT_KEY;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont need all caps constants

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure?

The Google style guide says that constants should be all uppercase.

/**
* Returns the golden hash
* @return {string|undefined}
* @private
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sort private methods to the bottom of the file

}

/**
* Saves the golden hash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hehehe, golden hash

/**
* Returns the correct image path
* @param {string} imageHash The image hash
* @param {string} imageType The image type

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make an @enum for this? It can have string values so that your existing usages will continue to work.

this.saveImage_(diffPath, diff, metadata),
]);

return assert.isBelow(Number(data.misMatchPercentage), 0.01);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make a constant for .01.

// Create a new stream from the image buffer
let stream = new Readable();
stream.push(imageBuffer);
stream.push(null);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment on this line explaining that null indicates EOF

@@ -1,59 +0,0 @@
/**

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to remove or update npm run upload:screenshots in package.json

Copy link

@acdvorak acdvorak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few minor comments, otherwise LGTM!

@moog16 moog16 merged commit 98bcdfe into master Mar 30, 2018
@moog16 moog16 deleted the experimental/gcp-upload branch July 17, 2018 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants