11'use strict' ;
22
3- var p = require ( 'phantom' ) ;
3+ var phantom = require ( 'phantom' ) ;
44var resemble = require ( 'resemble' ) . resemble ;
55var fs = require ( 'fs' ) ;
66var yaml = require ( 'libyaml' ) ;
77var Util = require ( './differ.utils.js' ) . Util ;
8+ var child_process = require ( 'child_process' ) ;
89
910// Export the differ module
1011var VisualDiffer = { } ;
@@ -18,10 +19,11 @@ function testCompletion(browser, cb, opts) {
1819 if ( html1 . err || html2 . err ) {
1920 browser . exit ( ) ;
2021 cb ( html1 . err || html2 . err ) ;
21- }
22- if ( html1 . done && html2 . done ) {
22+ } else if ( html1 . done && html2 . done ) {
2323 browser . exit ( ) ;
2424 cb ( ) ;
25+ } else {
26+ cb ( ) ;
2527 }
2628}
2729
@@ -86,6 +88,7 @@ VisualDiffer.takeScreenShot = function(browser, logger, cb, opts, htmlOpts) {
8688 }
8789
8890 // Save the screenshot
91+ console . log ( "Rendering" , htmlOpts . screenShot ) ;
8992 page . render ( htmlOpts . screenShot , function ( ) {
9093 logger ( htmlOpts . name + ' done!' ) ;
9194 htmlOpts . done = true ;
@@ -97,11 +100,17 @@ VisualDiffer.takeScreenShot = function(browser, logger, cb, opts, htmlOpts) {
97100 ) ;
98101 } ;
99102
100- if ( htmlOpts . injectJQuery ) {
101- page . includeJs ( 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js' , processPage ) ;
102- } else {
103- processPage ( ) ;
104- }
103+ setTimeout (
104+ function ( ) {
105+ if ( htmlOpts . injectJQuery ) {
106+ page . includeJs ( 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js' , processPage ) ;
107+ } else {
108+ processPage ( ) ;
109+ }
110+ } ,
111+ opts . screenShotDelay
112+ ) ;
113+
105114 } ) ;
106115 } ) ;
107116} ;
@@ -128,12 +137,17 @@ VisualDiffer.takeScreenshots = function(opts, logger, cb) {
128137 // Phantom doesn't like protocols in its proxy ips
129138 // But, node.js request wants http:// proxies ... so dance around all that.
130139 var proxy = ( process . env . HTTP_PROXY_IP_AND_PORT || '' ) . replace ( / ^ h t t p s ? : \/ \/ / , '' ) ;
131- p . create ( '--ssl-protocol=TLSv1' , '--proxy=' + proxy , function ( browser ) {
132- // HTML1 screenshot
133- self . takeScreenShot ( browser , logger , cb , opts , opts . html1 ) ;
140+ phantom . create ( '--debug=true' , '--ssl-protocol=TLSv1' , '--proxy=' + proxy , function ( browser ) {
141+ function ss1 ( cb1 ) {
142+ // HTML1 screenshot
143+ self . takeScreenShot ( browser , logger , cb1 , opts , opts . html1 ) ;
144+ }
134145
135- // HTML2 screenshot
136- self . takeScreenShot ( browser , logger , cb , opts , opts . html2 ) ;
146+ function ss2 ( ) {
147+ // HTML2 screenshot
148+ self . takeScreenShot ( browser , logger , cb , opts , opts . html2 ) ;
149+ }
150+ ss1 ( ss2 ) ;
137151 } ) ;
138152} ;
139153
@@ -146,14 +160,45 @@ VisualDiffer.genVisualDiff = function(opts, logger, cb) {
146160 if ( logger ) {
147161 logger ( '--screenshotting done--' ) ;
148162 }
149- if ( opts . outputSettings ) {
150- resemble . outputSettings ( opts . outputSettings ) ;
163+ if ( opts . diffEngine == "uprightdiff" ) {
164+ VisualDiffer . compareWithUprightDiff ( opts , logger , cb ) ;
165+ } else {
166+ VisualDiffer . compareWithResemble ( opts , logger , cb ) ;
151167 }
152- resemble ( opts . html1 . screenShot ) . compareTo ( opts . html2 . screenShot ) .
153- ignoreAntialiasing ( ) . // <-- muy importante
154- onComplete ( function ( data ) {
155- cb ( null , data ) ;
156- } ) ;
157168 }
158169 } ) ;
159170} ;
171+
172+ VisualDiffer . compareWithResemble = function ( opts , logger , cb ) {
173+ if ( opts . outputSettings ) {
174+ resemble . outputSettings ( opts . outputSettings ) ;
175+ }
176+
177+
178+ resemble ( opts . html1 . screenShot ) . compareTo ( opts . html2 . screenShot ) .
179+ ignoreAntialiasing ( ) . // <-- muy importante
180+ onComplete ( function ( data ) {
181+ var png_data = data . getImageDataUrl ( "" ) . replace ( / ^ d a t a : i m a g e \/ p n g ; b a s e 6 4 , / , '' ) ;
182+ var png_buffer = new Buffer ( png_data , 'base64' ) ;
183+ fs . writeFileSync ( opts . diffFile , png_buffer ) ;
184+ cb ( null , data ) ;
185+ } ) ;
186+ } ;
187+
188+ VisualDiffer . compareWithUprightDiff = function ( opts , logger , cb ) {
189+ child_process . execFile ( '/usr/local/bin/uprightdiff' ,
190+ [
191+ '--format=json' ,
192+ opts . html1 . screenShot , opts . html2 . screenShot , opts . diffFile
193+ ] ,
194+ function ( error , stdout , stderr ) {
195+ if ( error && error . code !== 0 ) {
196+ error . stderr = stderr ;
197+ logger ( "UprightDiff exited with error: " + JSON . stringify ( error ) ) ;
198+ cb ( error , null ) ;
199+ } else {
200+ cb ( null , JSON . parse ( stdout ) ) ;
201+ }
202+ }
203+ ) ;
204+ } ;
0 commit comments