-
Notifications
You must be signed in to change notification settings - Fork 227
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
Conversation
There was a problem hiding this 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!
test/screenshot/golden.json
Outdated
{ | ||
"temporary-package/index.html": "57de91e42bd22a845074bae80f71eca3902ab7c6719f129960bc9018c79db95a", | ||
"temporary-package/foo.html": "91f0795700eaba345092dd7507e046daa0d33e1136bad7992011ab454ddb1faf" | ||
} |
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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.
test/screenshot/screenshot.js
Outdated
|
||
import comparisonOptions from './screenshot-comparison-options'; | ||
|
||
const readFilePromise = promisify(readFile); | ||
const writeFilePromise = promisify(writeFile); | ||
|
||
const SERVICE_ACCOUNT_KEY = process.env.SERVICE_ACCOUNT_KEY; |
There was a problem hiding this comment.
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
.
test/screenshot/screenshot.js
Outdated
credentials = JSON.parse(SERVICE_ACCOUNT_KEY); | ||
} catch (err) { | ||
console.error('Service account key could not be parsed.'); | ||
process.exit(1); |
There was a problem hiding this comment.
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?
test/screenshot/screenshot.js
Outdated
* @private | ||
*/ | ||
generateImageID_(imageBuffer) { | ||
return createHash('sha256').update(imageBuffer).digest('hex'); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
test/screenshot/screenshot.js
Outdated
|
||
case SNAPSHOT: | ||
case DIFF: | ||
return `${this.urlPath_}/${COMMIT_HASH}/${imageId}.${imageType}.png`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Indent
test/screenshot/screenshot.js
Outdated
* 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: @param {!Object=}
test/screenshot/screenshot.js
Outdated
const url = `http://localhost:8080/${this.urlPath_}`; | ||
const imagePath = `./test/screenshot/${this.imagePath_}`; | ||
await this.createScreenshotTask_(url, imagePath); | ||
const golden = await this.createScreenshotTask_(); |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
test/screenshot/screenshot.js
Outdated
} | ||
|
||
/** | ||
* Captures a screenshot of the test URL |
There was a problem hiding this comment.
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
test/screenshot/screenshot.js
Outdated
|
||
/** | ||
* Captures a screenshot of the test URL | ||
*/ | ||
capture() { |
There was a problem hiding this comment.
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
.
test/screenshot/screenshot.js
Outdated
|
||
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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
test/screenshot/screenshot.js
Outdated
/** | ||
* Returns the golden hash | ||
* @return {string|undefined} | ||
* @private |
There was a problem hiding this comment.
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
test/screenshot/screenshot.js
Outdated
} | ||
|
||
/** | ||
* Saves the golden hash |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 @@ | |||
/** |
There was a problem hiding this comment.
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
There was a problem hiding this 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!
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.