|
| 1 | +// Run a visual regression test on visualize.html |
| 2 | +var htmlPath = "http://localhost:8003/visualize.html" |
| 3 | + |
| 4 | +var fs = require('fs'); |
| 5 | +var path = fs.absolute(fs.workingDirectory + '/phantomcss.js'); |
| 6 | +var phantomcss = require(path); |
| 7 | + |
| 8 | +casper.test.begin('Testing ' + htmlPath, function (test) { |
| 9 | + // boring setup code taken from PhantomCSS demo |
| 10 | + phantomcss.init({ |
| 11 | + rebase: casper.cli.get( "rebase" ), |
| 12 | + // SlimerJS needs explicit knowledge of this Casper, and lots of absolute paths |
| 13 | + casper: casper, |
| 14 | + libraryRoot: fs.absolute( fs.workingDirectory + '' ), |
| 15 | + screenshotRoot: fs.absolute( fs.workingDirectory + '/screenshots' ), |
| 16 | + failedComparisonsRoot: fs.absolute( fs.workingDirectory + '/demo/failures' ), |
| 17 | + addLabelToFailedImage: false, |
| 18 | + }); |
| 19 | + |
| 20 | + casper.on('remote.message', function(msg) {this.echo(msg);}); |
| 21 | + casper.on('error', function (err) {this.die( "PhantomJS has errored: " + err );}); |
| 22 | + casper.on('resource.error', function (err) {casper.log( 'Resource load error: ' + err, 'warning' );}); |
| 23 | + |
| 24 | + |
| 25 | + // start with a baseline image |
| 26 | + casper.start(htmlPath); |
| 27 | + casper.viewport(1440, 900, function () { |
| 28 | + phantomcss.screenshot('#pyInputPane', 'pyInputPane'); |
| 29 | + }); |
| 30 | + |
| 31 | + // click on one example code for each language |
| 32 | + casper.then(function() { |
| 33 | + casper.click("#aliasExampleLink"); |
| 34 | + phantomcss.screenshot('#pyInputPane', 'pyInputPane'); |
| 35 | + }); |
| 36 | + |
| 37 | + casper.then(function() { |
| 38 | + casper.click("#tortureLink"); |
| 39 | + phantomcss.screenshot('#pyInputPane', 'pyInputPane'); |
| 40 | + }); |
| 41 | + |
| 42 | + casper.then(function() { |
| 43 | + casper.click("#javaVarLink"); |
| 44 | + phantomcss.screenshot('#pyInputPane', 'pyInputPane'); |
| 45 | + }); |
| 46 | + |
| 47 | + casper.then(function() { |
| 48 | + casper.click("#jsDatatypesExLink"); |
| 49 | + phantomcss.screenshot('#pyInputPane', 'pyInputPane'); |
| 50 | + }); |
| 51 | + |
| 52 | + casper.then(function() { |
| 53 | + casper.click("#tsGreeterExLink"); |
| 54 | + phantomcss.screenshot('#pyInputPane', 'pyInputPane'); |
| 55 | + }); |
| 56 | + |
| 57 | + casper.then(function() { |
| 58 | + casper.click("#rubyConstantsLink"); |
| 59 | + phantomcss.screenshot('#pyInputPane', 'pyInputPane'); |
| 60 | + }); |
| 61 | + |
| 62 | + casper.then(function() { |
| 63 | + casper.click("#cMengThesisLink"); |
| 64 | + phantomcss.screenshot('#pyInputPane', 'pyInputPane'); |
| 65 | + }); |
| 66 | + |
| 67 | + casper.then(function() { |
| 68 | + casper.click("#cppFirstLink"); |
| 69 | + phantomcss.screenshot('#pyInputPane', 'pyInputPane'); |
| 70 | + }); |
| 71 | + |
| 72 | + |
| 73 | + // now test the visualize mode: |
| 74 | + casper.then(function() { |
| 75 | + casper.click("#aliasExampleLink"); |
| 76 | + // brief wait for code to load |
| 77 | + this.wait(250, function() { |
| 78 | + casper.click("#executeBtn"); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + // after clicking on executeBtn, wait for the dataViz div to appear |
| 83 | + // because that means the visualizer has rendered: |
| 84 | + casper.waitFor(function check() { |
| 85 | + return this.evaluate(function() { |
| 86 | + return document.querySelectorAll("#dataViz").length > 0; |
| 87 | + }); |
| 88 | + }, function then() { |
| 89 | + phantomcss.screenshot('.visualizer', 'visualizer'); |
| 90 | + }); |
| 91 | + |
| 92 | + // this example has 31 steps: |
| 93 | + for (var i = 0; i < 31; i++) { |
| 94 | + casper.then(function() { |
| 95 | + casper.click("#jmpStepFwd"); |
| 96 | + // slight pause for vis to settle |
| 97 | + this.wait(200, function() { |
| 98 | + phantomcss.screenshot('.visualizer', 'visualizer'); |
| 99 | + }); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + // for the remaining examples, be brief and take only a snapshot of |
| 104 | + // the FINAL state of the visualization |
| 105 | + |
| 106 | + |
| 107 | + // test instruction limit reached |
| 108 | + casper.then(function() { |
| 109 | + casper.click("#genPrimesLink"); |
| 110 | + // brief wait for code to load |
| 111 | + this.wait(250, function() { |
| 112 | + casper.click("#executeBtn"); |
| 113 | + |
| 114 | + casper.waitFor(function check() { |
| 115 | + return this.evaluate(function() { |
| 116 | + return document.querySelectorAll("#dataViz").length > 0; |
| 117 | + }); |
| 118 | + }, function then() { |
| 119 | + casper.click('#jmpLastInstr'); |
| 120 | + // slight pause for vis to settle |
| 121 | + this.wait(200, function() { |
| 122 | + phantomcss.screenshot('.visualizer', 'visualizer_instr_limit_reached'); |
| 123 | + }); |
| 124 | + }); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + // test exception |
| 129 | + casper.then(function() { |
| 130 | + casper.click("#pwTryFinallyLink"); |
| 131 | + // brief wait for code to load |
| 132 | + this.wait(250, function() { |
| 133 | + casper.click("#executeBtn"); |
| 134 | + |
| 135 | + casper.waitFor(function check() { |
| 136 | + return this.evaluate(function() { |
| 137 | + return document.querySelectorAll("#dataViz").length > 0; |
| 138 | + }); |
| 139 | + }, function then() { |
| 140 | + casper.click('#jmpLastInstr'); |
| 141 | + // slight pause for vis to settle |
| 142 | + this.wait(200, function() { |
| 143 | + phantomcss.screenshot('.visualizer', 'visualizer_exception'); |
| 144 | + }); |
| 145 | + }); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + |
| 150 | + // for these examples, just snapshot dataViz to be even briefer: |
| 151 | + var exampleTestLinks = ['#tutorialExampleLink', '#ll2Link', '#inheritanceExampleLink', '#aliasing2Link', '#aliasing3Link', '#aliasing7Link', '#closure5Link']; |
| 152 | + |
| 153 | + exampleTestLinks.forEach(function(e, i) { |
| 154 | + // reset all toggles to test regular visualizer |
| 155 | + casper.thenEvaluate(function() { |
| 156 | + document.querySelector('#cumulativeModeSelector').value = "false"; |
| 157 | + document.querySelector('#heapPrimitivesSelector').value = "false"; |
| 158 | + document.querySelector('#textualMemoryLabelsSelector').value = "false"; |
| 159 | + }); |
| 160 | + |
| 161 | + casper.wait(250, function() { |
| 162 | + casper.click(e); |
| 163 | + // brief wait for code to load |
| 164 | + this.wait(250, function() { |
| 165 | + casper.click("#executeBtn"); |
| 166 | + |
| 167 | + casper.waitFor(function check() { |
| 168 | + return this.evaluate(function() { |
| 169 | + return document.querySelectorAll("#dataViz").length > 0; |
| 170 | + }); |
| 171 | + }, function then() { |
| 172 | + casper.click('#jmpLastInstr'); |
| 173 | + // slight pause for vis to settle |
| 174 | + this.wait(200, function() { |
| 175 | + phantomcss.screenshot('#dataViz', 'dataViz' + e); |
| 176 | + }); |
| 177 | + }); |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + |
| 182 | + // simulate what's done for composingprograms |
| 183 | + casper.thenEvaluate(function() { |
| 184 | + document.querySelector('#cumulativeModeSelector').value = "true"; |
| 185 | + document.querySelector('#heapPrimitivesSelector').value = "false"; |
| 186 | + document.querySelector('#textualMemoryLabelsSelector').value = "false"; |
| 187 | + }); |
| 188 | + |
| 189 | + casper.wait(250, function() { |
| 190 | + casper.click(e); |
| 191 | + // brief wait for code to load |
| 192 | + this.wait(250, function() { |
| 193 | + casper.click("#executeBtn"); |
| 194 | + casper.waitFor(function check() { |
| 195 | + return this.evaluate(function() { |
| 196 | + return document.querySelectorAll("#dataViz").length > 0; |
| 197 | + }); |
| 198 | + }, function then() { |
| 199 | + casper.click('#jmpLastInstr'); |
| 200 | + // slight pause for vis to settle |
| 201 | + this.wait(200, function() { |
| 202 | + phantomcss.screenshot('#dataViz', 'dataViz_CUMULATIVE_' + e); |
| 203 | + }); |
| 204 | + }); |
| 205 | + }); |
| 206 | + }); |
| 207 | + |
| 208 | + |
| 209 | + // simulate what's done for csc108h |
| 210 | + casper.thenEvaluate(function() { |
| 211 | + document.querySelector('#cumulativeModeSelector').value = "false"; |
| 212 | + document.querySelector('#heapPrimitivesSelector').value = "true"; |
| 213 | + document.querySelector('#textualMemoryLabelsSelector').value = "true"; |
| 214 | + }); |
| 215 | + |
| 216 | + casper.wait(250, function() { |
| 217 | + casper.click(e); |
| 218 | + // brief wait for code to load |
| 219 | + this.wait(250, function() { |
| 220 | + casper.click("#executeBtn"); |
| 221 | + casper.waitFor(function check() { |
| 222 | + return this.evaluate(function() { |
| 223 | + return document.querySelectorAll("#dataViz").length > 0; |
| 224 | + }); |
| 225 | + }, function then() { |
| 226 | + casper.click('#jmpLastInstr'); |
| 227 | + // slight pause for vis to settle |
| 228 | + this.wait(200, function() { |
| 229 | + phantomcss.screenshot('#dataViz', 'dataViz_TEXTLABELS_' + e); |
| 230 | + }); |
| 231 | + }); |
| 232 | + }); |
| 233 | + }); |
| 234 | + |
| 235 | + }); // end forEach |
| 236 | + |
| 237 | + |
| 238 | + // run all tests: |
| 239 | + casper.then(function now_check_the_screenshots() { |
| 240 | + phantomcss.compareAll(); // compare screenshots |
| 241 | + }); |
| 242 | + |
| 243 | + casper.run(function() { |
| 244 | + //phantomcss.getExitStatus() // pass or fail? |
| 245 | + casper.test.done(); |
| 246 | + }); |
| 247 | +}); |
0 commit comments