build-system/tasks/make-golden.js
/**
 * Take a screenshot of the page and diff the result against the golden image.
 *
 * @param {string} file filename for file to test
 * @param {string} dir directory path to test images
 * @param {string} host hostname
 * @param {boolean} verbose use verbose logging
 * @param {function} cb callback function
 */
Take a screenshot of the page and diff the result against the golden image.
Take a screenshot of the page and diff the result against the golden image .
function diffScreenshot_(file, dir, host, verbose, cb) {
  if (verbose) {
    util.log('Screenshot diff for ', file);
  }
  var goldenFile = 'screenshots/' + file;
  var goldenCopyFile = dir + '/' + file;
  var htmlPath = file.replace('.png', '');
  var tmpFile = dir + '/' + file.replace('.png', '.tmp.png');
  var diffFile = dir + '/' + file.replace('.png', '.diff.png');
  fs.copySync(goldenFile, goldenCopyFile, {clobber: true});
  doScreenshot(host, htmlPath, tmpFile, 'iPhone6+', verbose, function() {
    gulp.src([tmpFile])
        .pipe(imageDiff({
          referenceImage: goldenCopyFile,
          differenceMapImage: diffFile,
          logProgress: verbose
        }))
        .pipe(imageDiff.jsonReporter())
        .pipe(gulp.dest(diffFile + '.json'))
        .on('error', function(error) {
          util.log(util.colors.red('Screenshot diff failed: ', file, error));
          cb({error});
        })
        .on('end', function(res) {
          var contents = fs.readFileSync(diffFile + '.json', 'utf8');
          var json = JSON.parse(contents);
          cb(json[0]);
        });
  });
}
function diffScreenshot_ ( file , dir , host , verbose , cb ) { if ( verbose ) { util . log ( 'Screenshot diff for ' , file ) ; } var goldenFile = 'screenshots/' + file ; var goldenCopyFile = dir + '/' + file ; var htmlPath = file . replace ( '.png' , '' ) ; var tmpFile = dir + '/' + file . replace ( '.png' , '.tmp.png' ) ; var diffFile = dir + '/' + file . replace ( '.png' , '.diff.png' ) ; fs . copySync ( goldenFile , goldenCopyFile , { clobber : true } ) ; doScreenshot ( host , htmlPath , tmpFile , 'iPhone6+' , verbose , function ( ) { gulp . src ( [ tmpFile ] ) . pipe ( imageDiff ( { referenceImage : goldenCopyFile , differenceMapImage : diffFile , logProgress : verbose } ) ) . pipe ( imageDiff . jsonReporter ( ) ) . pipe ( gulp . dest ( diffFile + '.json' ) ) . on ( 'error' , function ( error ) { util . log ( util . colors . red ( 'Screenshot diff failed: ' , file , error ) ) ; cb ( { error } ) ; } ) . on ( 'end' , function ( res ) { var contents = fs . readFileSync ( diffFile + '.json' , 'utf8' ) ; var json = JSON . parse ( contents ) ; cb ( json [ 0 ] ) ; } ) ; } ) ; }
Take a screenshot of the page and diff the result against the golden image.
Take a screenshot of the page and diff the result against the golden image .
// TODO: pixelColorTolerance: 0.10